在 phonegap 应用程序和本机 java 应用程序之间共享 SQl Lite 数据库
shared SQl Lite db between phonegap app and native java app
我有两个 android 应用程序(一个是用 js 编写的(使用 phonegap),另一个是 java)。两者都需要访问一个 SQLite 数据库。 Yes it's possible.
在我的 js 文件中,我使用 Cordova-sqlite-storage 创建数据并将其插入数据库:
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);
});
});
然后我使用 this answer 尝试将 java 应用程序连接到我先前存在的数据库:
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 应用程序中使用此代码:
DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();
尝试获取其上下文。 (但显然我不能,因为这个代码^是java)。所以我尝试使用 this answer.(Context context=this.cordova.getActivity().getApplicationContext();
) 获取上下文,但我不确定在哪里添加它,我什至不确定这段代码是否能正常工作。
我的问题是:
- 我应该在申请中的什么地方添加 this code?
- 我走的路对吗?
- 将 js 应用程序和 Java 应用程序连接到 Android 上的同一 SQLite 数据库的最佳方法是什么? (例子会很有帮助)
信息:
Android 5.1,
科尔多瓦 5.0
更新:
我已经在两个应用程序中 android:sharedUserId="my.app"
。
两者使用的是不同的数据库,这种情况我推荐使用以下插件:
https://github.com/brodysoft/Cordova-SQLitePlugin
但是,SQLite 是一个单用户数据库。如果你走 "copy the database" 路线,那么你就有丢失数据的风险。如果桌面用户添加数据,iOS用户添加数据,复制文件时,某人的数据将丢失。
您可以实施自己的 "syncing" 算法来识别数据库之间的变化并使它们保持同步。这可能会很痛苦,但这是一个常见的解决方案,也是我推荐的。我已经为 desktop/iOS 应用程序这样做了。
您可以选择使用数据库服务器,但这需要网络访问权限才能连接到它。
1) 这取决于您的应用程序,请阅读一些有关 android 的书籍,并且能够将代码放在您需要的地方。您还可以使用 GreenDAO 来更简单地访问 sqlite
3) 您可以使用相同的证书签署 2 个不同的应用程序,这样两个应用程序将被识别为 "same user id" 并且可以共享私人数据
2) 这是一种方法,但一个好方法(最好的方法)是在 android 上公开两个应用程序之间的数据是使用 content provider
希望对你有所帮助
我有两个 android 应用程序(一个是用 js 编写的(使用 phonegap),另一个是 java)。两者都需要访问一个 SQLite 数据库。 Yes it's possible.
在我的 js 文件中,我使用 Cordova-sqlite-storage 创建数据并将其插入数据库:
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);
});
});
然后我使用 this answer 尝试将 java 应用程序连接到我先前存在的数据库:
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 应用程序中使用此代码:
DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();
尝试获取其上下文。 (但显然我不能,因为这个代码^是java)。所以我尝试使用 this answer.(Context context=this.cordova.getActivity().getApplicationContext();
) 获取上下文,但我不确定在哪里添加它,我什至不确定这段代码是否能正常工作。
我的问题是:
- 我应该在申请中的什么地方添加 this code?
- 我走的路对吗?
- 将 js 应用程序和 Java 应用程序连接到 Android 上的同一 SQLite 数据库的最佳方法是什么? (例子会很有帮助)
信息:
Android 5.1, 科尔多瓦 5.0
更新:
我已经在两个应用程序中 android:sharedUserId="my.app"
。
两者使用的是不同的数据库,这种情况我推荐使用以下插件:
https://github.com/brodysoft/Cordova-SQLitePlugin
但是,SQLite 是一个单用户数据库。如果你走 "copy the database" 路线,那么你就有丢失数据的风险。如果桌面用户添加数据,iOS用户添加数据,复制文件时,某人的数据将丢失。
您可以实施自己的 "syncing" 算法来识别数据库之间的变化并使它们保持同步。这可能会很痛苦,但这是一个常见的解决方案,也是我推荐的。我已经为 desktop/iOS 应用程序这样做了。
您可以选择使用数据库服务器,但这需要网络访问权限才能连接到它。
1) 这取决于您的应用程序,请阅读一些有关 android 的书籍,并且能够将代码放在您需要的地方。您还可以使用 GreenDAO 来更简单地访问 sqlite
3) 您可以使用相同的证书签署 2 个不同的应用程序,这样两个应用程序将被识别为 "same user id" 并且可以共享私人数据
2) 这是一种方法,但一个好方法(最好的方法)是在 android 上公开两个应用程序之间的数据是使用 content provider
希望对你有所帮助