Java - 将 Android 游标的内容复制到它自己的新 SQLite 数据库中的最有效方法是什么?
Java - What is the most efficient way to copy the contents of an Android Cursor into a new SQLite database of its own?
你会怎么做?经过相当多的谷歌搜索后,我还没有找到任何好的资源或解决方案。
首先,确保创建一个新数据库 class,其中包含您需要的表。之后,您可以使用 insertCursorInDb
方法。也调用 open()
一次,这会让你写入数据库。
public class YourDatabase extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "yourdatabase.db";
private SQLiteDatabase database;
public YourDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void open() throws SQLException {
database = this.getWritableDatabase();
}
private void create(SQLiteDatabase database) {
database.execSQL("CREATE TABLE contacts (TEXT `name`, TEXT `number`)");
}
@Override
public void onCreate(SQLiteDatabase database) {
create(database);
}
// this function depends on the values in your cursor
public void insertCursorInDb(Cursor cursor) {
if (cursor != null) {
while (cursor.moveToNext()) {
database.execSQL("INSERT INTO contacts(`name`, `number`) VALUES (?, ?)", new String[]{cursor.getString(0), cursor.getString(1)});
}
cursor.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
按照提示,你也可以使用ContentValues
,那么就会变成这样:
// this function depends on the values in your cursor
public void insertCursorInDb(Cursor cursor) {
if (cursor != null) {
while (cursor.moveToNext()) {
ContentValues values = new ContentValues();
values.put("name", cursor.getString(0));
values.put("number", cursor.getString(1));
database.insertOrThrow("contacts", null, values);
}
cursor.close();
}
}
你会怎么做?经过相当多的谷歌搜索后,我还没有找到任何好的资源或解决方案。
首先,确保创建一个新数据库 class,其中包含您需要的表。之后,您可以使用 insertCursorInDb
方法。也调用 open()
一次,这会让你写入数据库。
public class YourDatabase extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "yourdatabase.db";
private SQLiteDatabase database;
public YourDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void open() throws SQLException {
database = this.getWritableDatabase();
}
private void create(SQLiteDatabase database) {
database.execSQL("CREATE TABLE contacts (TEXT `name`, TEXT `number`)");
}
@Override
public void onCreate(SQLiteDatabase database) {
create(database);
}
// this function depends on the values in your cursor
public void insertCursorInDb(Cursor cursor) {
if (cursor != null) {
while (cursor.moveToNext()) {
database.execSQL("INSERT INTO contacts(`name`, `number`) VALUES (?, ?)", new String[]{cursor.getString(0), cursor.getString(1)});
}
cursor.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
按照提示,你也可以使用ContentValues
,那么就会变成这样:
// this function depends on the values in your cursor
public void insertCursorInDb(Cursor cursor) {
if (cursor != null) {
while (cursor.moveToNext()) {
ContentValues values = new ContentValues();
values.put("name", cursor.getString(0));
values.put("number", cursor.getString(1));
database.insertOrThrow("contacts", null, values);
}
cursor.close();
}
}