这是我的日志:
08-10 23:11:01.644 2341-2341 / com.projects.learnwords.app E / Database:插入错误> CORRECT_ENGLISH_WORDS = 0 SECOND_WORD =собакаCORRECT_RUSSIANS_WORDS= 0 FIRST_WORD = dog android.database.sqlite.SQLiteException:没有这样的表:vv :,同时编译:INSERT INTO vv(CORRECT_ENGLISH_WORDS,SECOND_WORD,CORRECT_RUSSIANS_WORDS,FIRST_WORD)VALUES(?,?,?,>?);
我的db class:
public class DbControl {
private static final String DB_NAME = "LearnWordsDB.db";
private final String TABLE_NAME;
private final int DB_VERSION;
static final String FIRST_WORD = "FIRST_WORD";
static final String SECOND_WORD = "SECOND_WORD";
static final String CORRECT_FIRST_WORDS = "CORRECT_ENGLISH_WORDS";
static final String CORRECT_SECOND_WORDS = "CORRECT_RUSSIANS_WORDS";
private Cursor cursor;
private SQLiteDatabase database;
private DbOpenHelper dbOpenHelper;
private Context context;
public DbControl(Context context, final String TABLE_NAME, int DB_VERSION){
super();
this.context = context;
this.TABLE_NAME = TABLE_NAME;
this.DB_VERSION =DB_VERSION;
}
public void open() throws SQLException {
dbOpenHelper = new DbOpenHelper(context, DB_NAME, null, DB_VERSION);
database = dbOpenHelper.getWritableDatabase();
}
public void close(){
dbOpenHelper.close();
}
public void insert(final String FIRST_WORD, final String SECOND_WORD,
final int CORRECT_FIRST_WORDS, final int CORRECT_SECOND_WORDS) {
ContentValues values = new ContentValues();
values.put(Dictionary.FIRST_WORD, FIRST_WORD);
values.put(Dictionary.SECOND_WORD, SECOND_WORD);
values.put(Dictionary.CORRECT_FIRST_WORDS, CORRECT_FIRST_WORDS);
values.put(Dictionary.CORRECT_SECOND_WORDS, CORRECT_SECOND_WORDS);
database.insert(TABLE_NAME, null, values);
}
private class DbOpenHelper extends SQLiteOpenHelper{
public DbOpenHelper(Context context, final String DB_NAME, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DB_NAME, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( _id INTEGER PRIMARY KEY AUTOINCREMENT, " + FIRST_WORD + " TEXT, " +
SECOND_WORD + " TEXT, " + CORRECT_FIRST_WORDS + " INTEGER, " + CORRECT_SECOND_WORDS + " INTEGER )";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(DROP_TABLE);
onCreate(db);
}
}
}
这里我试图插入:
private void writeToDictionary(String contents, String dictName){
DbControl dict = new DbControl(getApplicationContext(), dictName, 1);
DictionaryRow[] dictRows = parseWords(contents);
try {
dict.open();
for(DictionaryRow dr: dictRows) {
dict.insert(dr.getFirstWord(), dr.getSecondWord(), dr.getCorrectFirstWords(), dr.getCorrectSecondWords());
}
} catch (SQLException e)
{
Log.e("CREATE_OR_OPEN_ERROR", e.toString());
Toast.makeText(getApplicationContext(), "Ошибка", Toast.LENGTH_LONG).show();
}
}
我的手机API级别10。 我该如何解决这个问题?
答案 0 :(得分:1)
我认为问题是你将TABLE_NAME作为构造函数的参数,期望它在打开现有数据库时创建这个新表,这是行不通的。您应该在onCreate
初始化所需的所有表,因为每个数据库只发生一次。
鉴于此,我认为你不应该有那个构造函数参数,我认为你不应该在每次调用DbHelper
时创建open()
的新实例。我可能会更进一步,让DbControl
成为一个单例,它只包装DbHelper
并公开你创建的方法。