我有一些初始数据要插入我的数据库中。我已将它添加到我的自定义SQLiteOpenHelper类中。
package com.company.database;
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public static final String TABLE_NAMES = "names";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
private static final String DATABASE_NAME = "my.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAMES_CREATE = "CREATE TABLE " + TABLE_NAMES + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NAME + " VARCHAR NOT NULL);";
private static final String TABLE_NAMES_INSERT = "INSERT INTO " + TABLE_NAMES
+ "(" + COLUMN_NAME + ")" + " VALUES "
+ "('Antony'),"
+ "('Benny'),"
+ "('Carol'),"
+ "('Daniel'),"
// ... AND SO ON ...
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_NAMES_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
dropTable(database);
onCreate(database);
}
private void dropTable(SQLiteDatabase database) {
database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAMES);
} }
请注意插入声明。名字的数量会增加,我不想保持这个混乱。
我该怎么做才能解决它?
答案 0 :(得分:1)
您可以将数据库与应用程序一起“发送”。这样,您可以预设详细信息,而无需在用户下载时提交。但请注意,应用程序文件大小可能会增加,具体取决于数据的大小。可用于实现此目的的库是SQLite资产助手(链接:https://github.com/jgilfelt/android-sqlite-asset-helper)
答案 1 :(得分:0)
您可以在计算机上创建数据库,将其包含在APK中,并在第一次运行时将其加载到应用中。
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/