PhoneGap的应用和原生的Java应用程序之间共享SQL精简版数据库 [英] shared SQl Lite db between phonegap app and native java app
问题描述
我有两个Android应用(一个写在JS(使用PhoneGap的)其他的在Java)。两者都需要访问一个SQLite的DB。 是的,它是可能的。
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.
在我的js文件我用科尔多瓦-sqlite的存储来创建和插入数据进入DB:
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);
});
});
然后我用这个答案以尝试将Java应用程序连接到我的preexisting DB:
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();
不过,我需要在我的js应用程序来使用这个code:
However I am required to use this code in my js app:
DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();
要设法得到它的上下文。 (但很明显我不能因为这个code ^是用java)。所以,我尝试使用这个答案得到的上下文。(上下文的背景下= this.cordova.getActivity ().getApplicationContext();
),但我不知道在哪里添加此,我甚至不知道这code将反正工作。
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.
我的问题是:
- 我在哪里可以添加在我的应用程序这个code ?
- 我在正确的道路上?
- 什么是一个JS应用程序和的Java应用程序连接到同一SQLite的Dtatabase在Android的最佳途径? (例子将是非常有益的)
信息:
是Android 5.1,
科尔多瓦5.0
Android 5.1, Cordova 5.0
更新:
我已经有的android:sharedUserId =my.app
在这两个应用
推荐答案
1),这取决于您的应用程序,请阅读一些书籍在Android和将能够把code,你所需要的。您还可以使用GreenDAO一个更简单的访问sqlite的
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)可以签订相同的证书2个不同的应用程序,这样的两个应用程序被认定为同一个用户ID和可以共享私有数据
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),这是一种方式,但一个好办法(最好的方式)是两个应用程序在Android上的公开数据是使用的内容提供商
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
我希望这可以帮助您
这篇关于PhoneGap的应用和原生的Java应用程序之间共享SQL精简版数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!