我正在尝试在 Android 中首次使用 Sqlite,但结果好坏参半。
在我的第一个活动中,我使用这样的代码将许多行加载到表中
public void addTableStatus(String tableId, String name) {
Log.d("DB", "Before Insert :: " + "Id=[" + tableId + "]");
if (sd == null) {
Log.d("DB", "Get Database");
sd = getWritableDatabase();
}
ContentValues cv = new ContentValues();
cv.put(V2VocabsTable.ID, tableId);
cv.put(V2VocabsTable.NAME, name);
cv.put(V2VocabsTable.LOADED, "NO");
long result = sd.insert(V2VocabsTable.TABLE_NAME, null, cv);
Log.d("DB", "After Insert :: " + result);
String query = "SELECT * from " + V2VocabsTable.TABLE_NAME ;
Log.i("DB2", "Query : " + query);
Cursor mCursor = sd.rawQuery(query, null);
Log.i("DB2", "Result : " + mCursor.getCount());
}
日志文件显示的数据如下:
01-13 21:54:09.788: D/DB(22744): Before Insert :: Id=[ZU055]
01-13 21:54:09.798: D/DB(22744): After Insert :: 129
01-13 21:54:09.798: I/DB2(22744): Query : SELECT * from v2TableStatus
01-13 21:54:09.798: I/DB2(22744): Result : 129
01-13 21:54:09.798: D/DB(22744): ZU055 :: WaitingListType-v1.0.xml
01-13 21:54:09.798: D/DB(22744): Before Insert :: Id=[ZU056]
01-13 21:54:09.798: D/DB(22744): After Insert :: 130
01-13 21:54:09.798: I/DB2(22744): Query : SELECT * from v2TableStatus
01-13 21:54:09.798: I/DB2(22744): Result : 130
01-13 21:54:09.798: D/DB(22744): ZU056 :: DCRConsentToShareIndicator-v1.0.xml
但是,当我尝试在另一个活动中读取表格时,表格中似乎没有任何数据。我执行相同的查询(作为测试)并且总是得到 0 行。
public void test(String tableName) {
if (sd == null) {
sd = getWritableDatabase();
}
tableName = tableName.substring(5).trim();
String query;
Log.i("DB_", "Test for : [" + tableName + "]");
query = "SELECT * from " + V2VocabsTable.TABLE_NAME ;
Log.i("DB2", "Query : " + query);
Cursor mCursor = sd.rawQuery(query, null);
Log.i("DB2", "Result : " + mCursor.getCount());
}
01-13 21:54:18.838: I/DB_(22744): Test for : [0136]
01-13 21:54:18.838: I/DB2(22744): Query : SELECT * from v2TableStatus
01-13 21:54:18.838: I/DB2(22744): Result : 0