如何发布预填充 table 的应用程序?
How to ship app with pre-filled table?
我正在开发一个应用程序,我在其中创建了一个包含多个 table 的数据库:
public class Database extends SQLiteOpenHelper {
private static final String DB_NAME = "db";
private static final int DB_VERSION = 1;
public Database(Context context) {
super(context,DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String table = "CREATE TABLE users(";
table += "_id TEXT PRIMARY KEY,";
table += "cat TEXT NOT NULL,";
table += "city TEXT NOT NULL,";
table += "country TEXT NOT NULL,";
table += "addr TEXT,";
String table2 = "CREATE TABLE special(";
table2 += "idspec TEXT PRIMARY KEY);";
String table3 = "CREATE TABLE places(";
table3 += "id TEXT PRIMARY KEY,";
table3 += "text TEXT NOT NULL,";
table3 += "data TEXT NOT NULL,";
table3 += "user TEXT REFERENCES users(_id));";
String table4 = "CREATE TABLE average(";
table4 += "user TEXT PRIMARY KEY,";
table4 += "place INTEGER NOT NULL,";
table4 += "avg REAL NOT NULL);";
db.execSQL(table);
db.execSQL(table2);
db.execSQL(table3);
db.execSQL(table4);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
我想进一步添加 table,其中包含大约 70 个条目,我将在其他 类 中阅读这些条目。那么,如何添加预填table呢?
也许,有一个最好的方法可以达到同样的目的,如果我不知道,抱歉。
您可以使用 SQLiteAssetHelper 来运送您的带有预填充数据的应用程序,这里是 link 存储库 https://github.com/jgilfelt/android-sqlite-asset-helper
在您的应用中,只需将构造函数调用为
public Yourclassname(Context context)
{
super(context,Database_Name,null, Database_Version);
}
在您的 onCreate() 方法中,只需检查是否没有记录。
即:执行如下查询:
SELECT * FROM users
并检查记录数是否为0
int count = cur.getCount();
如果计数为 0,则在事务下执行您的插入。
注意:我将上下文传递给我的函数,我称之为 ctx
这是功能的核心部分
SQLiteDatabase db = null;
try
{
db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
// Start the transaction
db.beginTransaction();
// Fill the table
db.execSQL("INSERT INTO users () values ()");
// ... more inserts...
db.setTransactionSuccessful();
}
catch (final SQLiteException se)
{
System.out.println
(
ctx.getClass().getSimpleName() + "\n" +
"Could not import to the database"
);
se.printStackTrace();
result = 0;
}
finally
{
// End the transaction
db.endTransaction();
db.close();
}
[编辑]
然后我读到:I want to add further table
。
这(改变现有的数据库结构)必须在 onUpgrade()
方法中完成,将 DATABASE_VERSION 常量设置为更高的值(在为了让 onUpgrade()
方法触发)。
我正在开发一个应用程序,我在其中创建了一个包含多个 table 的数据库:
public class Database extends SQLiteOpenHelper {
private static final String DB_NAME = "db";
private static final int DB_VERSION = 1;
public Database(Context context) {
super(context,DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String table = "CREATE TABLE users(";
table += "_id TEXT PRIMARY KEY,";
table += "cat TEXT NOT NULL,";
table += "city TEXT NOT NULL,";
table += "country TEXT NOT NULL,";
table += "addr TEXT,";
String table2 = "CREATE TABLE special(";
table2 += "idspec TEXT PRIMARY KEY);";
String table3 = "CREATE TABLE places(";
table3 += "id TEXT PRIMARY KEY,";
table3 += "text TEXT NOT NULL,";
table3 += "data TEXT NOT NULL,";
table3 += "user TEXT REFERENCES users(_id));";
String table4 = "CREATE TABLE average(";
table4 += "user TEXT PRIMARY KEY,";
table4 += "place INTEGER NOT NULL,";
table4 += "avg REAL NOT NULL);";
db.execSQL(table);
db.execSQL(table2);
db.execSQL(table3);
db.execSQL(table4);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
我想进一步添加 table,其中包含大约 70 个条目,我将在其他 类 中阅读这些条目。那么,如何添加预填table呢? 也许,有一个最好的方法可以达到同样的目的,如果我不知道,抱歉。
您可以使用 SQLiteAssetHelper 来运送您的带有预填充数据的应用程序,这里是 link 存储库 https://github.com/jgilfelt/android-sqlite-asset-helper 在您的应用中,只需将构造函数调用为
public Yourclassname(Context context)
{
super(context,Database_Name,null, Database_Version);
}
在您的 onCreate() 方法中,只需检查是否没有记录。
即:执行如下查询:
SELECT * FROM users
并检查记录数是否为0
int count = cur.getCount();
如果计数为 0,则在事务下执行您的插入。
注意:我将上下文传递给我的函数,我称之为 ctx
这是功能的核心部分
SQLiteDatabase db = null;
try
{
db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
// Start the transaction
db.beginTransaction();
// Fill the table
db.execSQL("INSERT INTO users () values ()");
// ... more inserts...
db.setTransactionSuccessful();
}
catch (final SQLiteException se)
{
System.out.println
(
ctx.getClass().getSimpleName() + "\n" +
"Could not import to the database"
);
se.printStackTrace();
result = 0;
}
finally
{
// End the transaction
db.endTransaction();
db.close();
}
[编辑]
然后我读到:I want to add further table
。
这(改变现有的数据库结构)必须在 onUpgrade()
方法中完成,将 DATABASE_VERSION 常量设置为更高的值(在为了让 onUpgrade()
方法触发)。