我遇到SQLiteAssetHelper(https://github.com/jgilfelt/android-sqlite-asset-helper)的问题:我无法正确打开数据库中的表格。
数据库是使用sqlitebrowser制作的 我有一个数据库,其中两个表填充了我想用Cursor访问的字符串,但是当我尝试访问它们时,它给了我一个错误:
android.database.sqlite.SQLiteException: no such table: Verbs (code 1): , while compiling: SELECT LatinVerbs FROM Verbs WHERE _rowid_=?
我的数据库合同:
import android.provider.BaseColumns;
public class DatabaseContract {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "verbtable.db";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private DatabaseContract(){}
public static abstract class Table1 implements BaseColumns{
public static final String TABLE_NAME = "Verbs";
public static final String COLUMN_NAME_TITLE_COL1 = "LatinVerbs";
public static final String COLUMN_NAME_TITLE_COL2 = "Translation";
public static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME_TITLE_COL1 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL2 + TEXT_TYPE + COMMA_SEP +
" )";
public static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
}
public static abstract class Table2 implements BaseColumns{
public static final String TABLE_NAME = "Structure";
public static final String COLUMN_NAME_TITLE_COL1 = "infinitive";
public static final String COLUMN_NAME_TITLE_COL2 = "pers_id";
public static final String COLUMN_NAME_TITLE_COL3 = "ind_praes_act";
public static final String COLUMN_NAME_TITLE_COL4 = "ind_praes_pass";
public static final String COLUMN_NAME_TITLE_COL5 = "ind_impf_act";
public static final String COLUMN_NAME_TITLE_COL6 = "ind_impf_pass";
public static final String COLUMN_NAME_TITLE_COL7 = "ind_fut_act";
public static final String COLUMN_NAME_TITLE_COL8 = "ind_fut_pass";
public static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME_TITLE_COL1 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL2 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL3 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL4 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL5 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL6 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL7 + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_TITLE_COL8 + TEXT_TYPE + COMMA_SEP +
" )";
public static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
}
}
我的游标:
Cursor c = db.query(DatabaseContract.Table1.TABLE_NAME, columns, "_rowid_=?", selectionArgsRoot, null, null, null);
int index = c.getColumnIndexOrThrow(columns[0]);
c.moveToFirst();
do {
rootResult = c.getString(index);
} while (c.moveToNext());
c.close();
c = db.query(DatabaseContract.Table2.TABLE_NAME, columnSuffix, "pers_id=?", selectionArgsSuffix, null, null, null);
index = c.getColumnIndexOrThrow(columnSuffix[0]);
c.moveToFirst();
do{
suffix = c.getString(index);
}while(c.moveToNext());
c.close();
我尝试更新数据库版本并重新启动我运行代码的模拟器,但错误仍然存在。
答案 0 :(得分:0)
您以前是否曾使用数据库创建程序运行该程序?如果有,则需要增加数据库版本号。这需要在添加时完成。
您还可以尝试在模拟器中卸载应用并重新运行全新安装。
答案 1 :(得分:0)
两个表都没有创建 事实上,您在每个表格的最后一列之后添加了额外逗号。
下面:
COLUMN_NAME_TITLE_COL2 + TEXT_TYPE + COMMA_SEP +
" )";
在这里:
COLUMN_NAME_TITLE_COL8 + TEXT_TYPE + COMMA_SEP +
" )";
这些行实际上应该是:
COLUMN_NAME_TITLE_COL2 + TEXT_TYPE + ")";
和
COLUMN_NAME_TITLE_COL8 + TEXT_TYPE + ")";
分别