我想知道是否有人可以看到我如何在 SQLite 数据库中设置 2 个表的错误?我在这里做错了吗?我从来没有为 sqlite 做过不止一张桌子。
我的 logcat stacktrace 显示错误是请求索引 0,大小为 0,我试图消除我如何在一个数据库中为 SQLite 设置 2 个表作为导致此错误的原因的任何问题,有什么想法吗?
这是下面显示的代码,其中创建了 2 个表
public class Database {
public static final String DATABASE_NAME = "allert_database";
public static final String DATABASE_TABLE = "allert_table";
public static final String DATABASE_TABLE2 = "allert_table2";
public static final int DATABASE_VERSION = 1;
public static final String _ID = "_id";
public static final String KEY_SENTENCE = "sentance";
public static final String KEY_DESCRIPTION = "description";
//create table DATABASE_TABLE (_ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_TABLE =
"CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_SENTENCE + " TEXT);";
// create the second table DATABASE_TABLE2
private static final String SCRIPT_CREATE_TABLE2 =
"CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE2 + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_DESCRIPTION + " TEXT);";
SQLiteDatabase sqLiteDatabase;
SQLiteHelper sqLiteHelper;
Context context;
public Database(Context c){
context = c;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
// onCreate of the SQLiteOpenhelper only called if the database does not already exist
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_TABLE);
db.execSQL(SCRIPT_CREATE_TABLE2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
onCreate(db);
}
}
编辑:只是为了好玩,我在这里添加了堆栈跟踪显示错误的查询方法是:
// get sentence from table one
public String getSentenceFromTableOne(int rowNum){
String status = " ";
String[] columns = new String[]{KEY_SENTENCE};
String selection = _ID + " = '" + rowNum + "';";
Cursor cursor = sqLiteDatabase.query(DATABASE_TABLE, columns,
selection, null, null, null, null);
if (cursor != null){
cursor.moveToFirst();
}
status = cursor.getString(cursor.getColumnIndex(KEY_SENTENCE));
cursor.close();
return status;
}
这是主要活动中的以下方法,它从数据库类调用上面显示的数据库查询方法
public String getSentenceTableOne(int enterNum){
Database db = new Database(context);
db.openToWrite();
String returnStr = db.getSentenceFromTableOne(enterNum);
db.close();
return returnStr;
}
编辑 2:我将进一步调查,但是为了完整起见,我在这里添加了我的 sqlite 助手类的实例化,或者它是如何完成的
SQLiteDatabase sqLiteDatabase;
SQLiteHelper sqLiteHelper;
Context context;
public Database(Context c){
context = c;
}
public void openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
}
public void openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
}
最终编辑:问这个问题的最初原因,如果我在数据库中执行多个表时有任何问题。这得到了回答,但是我发现真正的错误是从主类调用错误的方法,我是从主类的插入方法调用数据库类中的更新方法。这就是为什么没有新信息被插入数据库的原因。