1

I tried to use an HTML in android to create a table in SQLite, but it is not working.

Sample HTML:

<!doctype html>
<html>
  <head>
    <title>Contact Example</title>

    <script
      type="text/javascript"
      charset="utf-8"
      src="phonegap-1.0.0.js"
    ></script>
    <script type="text/javascript" charset="utf-8">
      // Wait for PhoneGap to load
      //
      //document.addEventListener("deviceready", onDeviceReady, false);

      // PhoneGap is ready
      //
      function onDeviceReady() {
        var db = openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(populateDB, errorCB, successCB);
      }

      // Populate the database
      //
      function populateDB(tx) {
        tx.executeSql("DROP TABLE IF EXISTS DEMO");
        tx.executeSql("CREATE TABLE IF NOT EXISTS DEMO (id unique, data)");
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
      }

      // Transaction error callback
      //
      function errorCB(tx, err) {
        alert("Error processing SQL: " + err);
      }

      // Transaction success callback
      //
      function successCB() {
        alert("success!");
      }
    </script>
  </head>
  <body onload="onDeviceReady()">
    <h1>Example</h1>
    <p>Database</p>
  </body>
</html>

I am not able to see any database created in android 2.2.

0

3 Answers 3

4

I think your database created in android's cache directory, that's why to are unable to see it. So use WebView's setting method and make cache enable also give WebView's SQLite database path to your cache directory, then use your database.


  1. Create a WebView,

  2. Get the webSettings, and set the Database Path. The path should be the path to your databases, which will be /path/path/your_package_name/.

    appView.setWebChromeClient(new WebClient(this));
    WebSettings settings = appView.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setJavaScriptCanOpenWindowsAutomatically(true);
                settings.setDatabaseEnabled(true);
                settings.setDatabasePath("/data/data/<database path>/app_database");
    
  3. Create a WebChromeClient, and override the onExceededDatabaseQuota method. This is called when you first open the application, because there’s initially no database setup. This allows you to set the quota in Java. Since this was a test code, I just threw an arbitrary value in here.

    final class WebClient extends WebChromeClient {
        Context mCtx;
        WebClient(Context ctx)
        {
            mCtx = ctx;
        }
    
        public void onExceededDatabaseQuota(String url, String databaseIdentifier,
            long currentQuota, long estimatedSize,
            long totalUsedQuota,WebStorage.QuotaUpdater quotaUpdater)
        {
            Log.d(LOG_TAG, "We exceeded the quota");
            quotaUpdater.updateQuota(100000);
        }
    }
    

For more details:

Sign up to request clarification or add additional context in comments.

Comments

0

On rooted device go to :

  1. DDMS
  2. File Explorer
  3. data ->data
  4. package name
  5. app_databases

Here "app_databases" contains "Databases.db" file that contains other databases files .

Comments

-2

WebView has the ability to invoke java code through javascript code. How about trying this solution?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.