在首次执行时构建应用数据库架构的最佳实践是什么?
换句话说,我想找出的是:
SQLite 不支持将逗号分隔的查询作为一个语句批处理执行。如何替换它以使代码面向 future ? (我不认为自己将所有创建语句按顺序链接到 tx.executeSQL("") 中,这会将我的代码变成一段可怕的 cr*p)。
最佳答案
我在 native 代码以及 Sencha/Phonegap 中所做的就是使用我引用的 DatabaseHelper 类。在该类中,您可以使用以下命令查看数据库的版本:
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 2);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
openDataBase();
int cVersion = myDataBase.getVersion();
if(cVersion != 2){
onUpgrade(myDataBase, myDataBase.getVersion(), 2);}
close();
} ....}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
this.getReadableDatabase();
try{
db.execSQL("ADD SQL QUERY HERE TO ADD TABLE");
}
catch(Exception e){}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
虽然这很乏味,但它可以让您的数据库面向 future ,并且查询在运行时调用。这满足了您的需求。
我希望这有帮助:)
关于javascript - Phonegap 应用程序 SQLite 数据库初始设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11773737/