我有一个带有多个表的SQLite数据库,但有些表是链接的,有些不是,但我想要的是从一个表读取并同时写入另一个表...所以我的问题在这里,我可以有两个不同光标指向各自的表或有任何其他方式...请让我知道...建议表示赞赏
答案 0 :(得分:1)
查看AsyncQueryHandler类。它将帮助您使ContentProvider进行异步/并发查询
答案 1 :(得分:0)
您只需要游标从表中读取数据,而不是写入。在数据库中设置方法,将所需的信息写入正确的表格,并在活动中调用方法以调用游标并从中获取数据,然后设置完毕。
db class
public class TestDB {
// *******************************************************************
// DB info
// *******************************************************************
private static final String TEST_DATABASE_NAME = "TestDB";
private static final int TEST_DATABASE_VERSION = 3;
// *******************************************************************
// list table
// *******************************************************************
public static final String FIRST_TABLE = "first";
public static final String FIRST_ROWID = "_id";
public static final String FIRST_NAME = "first_name";
private static final String CREATE_FIRST_TABLE = "create table "
+ LIST_TABLE + " (_id integer primary key autoincrement,"
+ "first_name text not null unique);";
// *******************************************************************
// category table
// *******************************************************************
public static final String SECOND_TABLE = "second";
public static final String SECOND_ROWID = "_id";
public static final String SECOND_NAME = "second_name";
private static final String CREATE_SECOND_TABLE = "create table "
+ CATEGORY_TABLE + " (_id integer primary key autoincrement,"
+ "second_name text not null unique);";
// *******************************************************************
// control variables
// *******************************************************************
private DBHelper mDBHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static class DBHelper extends SQLiteOpenHelper {
DBHelper(Context context) {
super(context, TEST_DATABASE_NAME, null,
TEST_DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_FIRST_TABLE);
db.execSQL(CREATE_SECOND_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("TestDB", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + FIRST_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + SECOND_TABLE);
onCreate(db);
}
}
public GroceryDB(Context ctx) {
this.mCtx = ctx;
}
public GroceryDB open() throws SQLException {
mDBHelper = new DBHelper(mCtx);
mDb = mDBHelper.getWritableDatabase();
return this;
}
public void close() {
mDBHelper.close();
}
// *******************************************************************
// Record creation methods
// *******************************************************************
public long createFirst(String name) {
ContentValues initialValues = new ContentValues();
initialValues.put(FIRST_NAME, name);
return mDb.insertWithOnConflict(FIRST_TABLE, null, initialValues,
SQLiteDatabase.CONFLICT_IGNORE);
}
public long createSecond(String name) {
ContentValues initialValues = new ContentValues();
initialValues.put(Second_NAME, name);
return mDb.insertWithOnConflict(CATEGORY_TABLE, null, initialValues,
SQLiteDatabase.CONFLICT_IGNORE);
}
// *******************************************************************
// Fetch all records methods
// *******************************************************************
public Cursor fetchAllFirst() {
return mDb.query(FIRST_TABLE, new String[] { FIRST_ROWID, FIRST_NAME },
null, null, null, null, null);
}
public Cursor fetchAllSecond() {
return mDb.query(SECOND_TABLE, new String[] { SECOND_ROWID,
SECOND_NAME }, null, null, null, null, null);
}
}
然后,假设您要将第一项从FIRST_TABLE复制到SECOND_TABLE,在您的活动类中,您可以执行以下操作:
private Cursor firstCursor;
private TestDB mDbHelper;
mDbHelper = new TestDB(getActivity());
mDbHelper.open();
firstCursor = mDbHelper.fetchAllFirst();
getActivity().startManagingCursor(itemCursor);
firstCursor.moveToFirst;
mDbHelper.createSecond(firstCursor.getString(1));
mDbHelper.close();
我遗漏了几件事(比如检查确保你的返回光标不为空,向用户显示光标内容以选择复制的内容等)但是这应该展示单向的基础知识从一张桌子复制到另一张桌子。