1

I have two app which both of them use SQL Database in webview.

One of them works very well on my device, but the other raise this error when trying to open database.

Uncaught Error: SECURITY_ERR: DOM Exception 18

Everything is similar in both project, I tried many thing but didn't find the reason.

I started a project from scratch, it works fine.

Ali
  • 21,572
  • 15
  • 83
  • 95
  • http://stackoverflow.com/questions/8390985/android-4-0-1-breaks-webview-html-5-local-storage – konmik Nov 07 '14 at 11:54

3 Answers3

4

This took me a full day to figure out, so I'd like to give full documentation about how to solve this problem on Android 4.2 and 4.3.

You can use Web SQL from a file:// URL in WebView. The Cordova folks managed to do it. There are just three things you must do:

1) Call setDatabaseEnabled() (duh):

webView.getSettings().setDatabaseEnabled(true);

2) Set the database file path. Yes, this is deprecated in Android 4.4, but it is required if you want to avoid the DOM Exception 18 in pre-Kitkat:

String databasePath = getContext().getApplicationContext().getDir(
  "database", Context.MODE_PRIVATE).getPath();
webView.getSettings().setDatabasePath(databasePath);

3) Set the onExceededDatabaseQuota handler. Yes, it's deprecated in Android 4.4, blah blah blah.

    webView.setWebChromeClient(new WebChromeClient() {

      @Override
      public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
                                          long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
        quotaUpdater.updateQuota(estimatedSize * 2);
      }
    });

If you skip any one of these 3 steps, then you will get the DOM Exception 18 error and Web SQL will not work. You've been warned.

nlawson
  • 11,510
  • 4
  • 40
  • 50
  • Solved my problem. I was mising the onExceededDatabaseQuota code; now that I've added it my problems seems to have gone away. Thanks. – Jo Jo Jun 05 '15 at 17:25
  • This is absolutely the answer for < 4.3 Android devices. I have a Sencha Touch app wrapped in a native Android app and running in a webview. I released an update that implements WebSql and a number of my users with older devices got nothing more than a blank white screen. After adding the onExceedDatabaseQuota everything seemed to work properly. Thanks @nlawson! – i2097i Oct 15 '15 at 02:27
3

I solved the "SECURITY_ERR: DOM Exception 18" by overriding the onExceededDatabaseQuota method.

webView = (WebView) findViewById(R.id.webView1);
webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
        long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
        quotaUpdater.updateQuota(estimatedSize * 2);
    }
});
cyberflohr
  • 789
  • 5
  • 10
  • If you are using `loadUrl("file://...")`, then this is the right solution to get `openDatabase` working on Android 4.2. Thanks a million! – nlawson Oct 08 '14 at 21:34
1

I was facing that problem. but I don't know my suggestion will help you or no

I had create an database like that:

db = window.openDatabase("myDatabase", "1.0", "my.Database", 1024);

on Android 4.0.3 there was no problem, The problem was on Android 4.1 it appears ( SECURITY_ERR: DOM Exception 18 )

Then I modified my database name by removing "dotes" and the strange characters, to be like that:

db = window.openDatabase("myDatabase", "1.0", "myDatabase", 1024);

Then my problem was solved :)

I was googling for a solution for that problem then i've solved it now :D

Hope that it was the solution of your problem ( As The Question asked 1 month ago )

Ahmed Sabry
  • 434
  • 1
  • 8
  • 17