简体   繁体   中英

shared SQl Lite db between phonegap app and native java app

I have two android apps (one written in js(using phonegap) the other in java). Both need to access one SQLite DB. Yes it's possible.

In my js file I use Cordova-sqlite-storage to create and insert data into a db:

var db = window.sqlitePlugin.openDatabase({name: "CAC.db", location: 1});

                                    db = sqlitePlugin.openDatabase({name: "CAC.db", location: 2, createFromLocation: 1});


                                          db.transaction(function(tx) {
                                            tx.executeSql('DROP TABLE IF EXISTS test_table');
                                            tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

                                            // demonstrate PRAGMA:
                                            db.executeSql("pragma table_info (test_table);", [], function(res) {
                                              console.log("PRAGMA res: " + JSON.stringify(res));
                                            });

                                            tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["MY ID", 100], function(tx, res) {


                                              db.transaction(function(tx) {
                                                  tx.executeSql("select data as dt from test_table;", [], function(tx, res) {
                                                  var id = res.rows.item(0).dt;
                                                  console.log("res.rows.item(0).cnt: " + id);

                                                });
                                              });

                                            }, function(e) {
                                              console.log("ERROR: " + e.message);
                                            });
                                          });

Then I use this answer to try to connect the java app to my preexisting db:

 Context sharedContext = null;
                        try {
                            sharedContext = this.createPackageContext("com.my.app", Context.CONTEXT_INCLUDE_CODE);
                            if (sharedContext == null) {
                                return;
                            }
                        } catch (Exception e) {
                            String error = e.getMessage();
                            return;
                        }

                        DbAdapter sharedDBadapter = new PerformerDbAdapter(sharedContext);
                        sharedDBadapter.open();

However I am required to use this code in my js app:

DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();

to try to get its context. (But obviously I can't because this code^ is java). So I tried to get the context using this answer. ( Context context=this.cordova.getActivity().getApplicationContext(); ) But am not sure where to add this, and I am not even sure if this code would work anyways.

My questions are:

  1. Where do I add this code in my application?
  2. Am I on the right path?
  3. What is the best way to connect a js app and Java app to the same SQLite Dtatabase on Android? (Examples would be very helpful)

INFO:

Android 5.1, Cordova 5.0

UPDATE:

I already have android:sharedUserId="my.app" in both apps.

1) it depends from your application, please read some book on android and will able to put the code where you need. You can use also GreenDAO for a more simple access to sqlite

3) you can sign 2 different app with the same certificate, in this way the two apps are recognised as "same user id" and can share private data

2) this is a way, but a good way (the best way) is to expose data between two app on android is to use content provider

I hope this help you

Both are using different database, in this situation I recommend to use the following plug-in:

https://github.com/brodysoft/Cordova-SQLitePlugin

But, SQLite is a single-user database. If you go the "copy the database" route, then you risk losing data. If the desktop user added data and the iOS user added data, someone's data will be lost when you copy the file.

You can implement your own "syncing" algorithm that identifies changes between the databases and keeps them in sync. This can be a pain, but is a common solution and is what I would recommend. I've done it this way for a desktop/iOS app.

You can choose to use a database server, but this will require network access to connect to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM