不能在我的sqlite数据库中插入值

时间:2016-11-19 11:09:47

标签: java database sqlite android-studio

我对数据库很新,所以我在线查看一些指南并开始尝试创建自己的数据库。出于某种原因,我的添加方法无法正常工作,我无法弄清楚为什么

这是我的数据库类

public class DBHandler extends SQLiteOpenHelper{

// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "DictionaryInfo";
// Contacts table name
private static final String TABLE_WORDS = "Words";
// Shops Table Columns names
private static final String KEY_ID ="id";
private static final String KEY_WORD = "word";
private static final String KEY_MEANING = "meaning";
private static final String CREATE_WORDS_TABLE = "CREATE TABLE " + TABLE_WORDS + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_WORD + " TEXT," + KEY_MEANING + " TEXT " + ")";
public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_WORDS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORDS);

    onCreate(db);
}

public void addWord(Word word){

    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_ID,word.getId());
    values.put(KEY_WORD,word.getWord());
    values.put(KEY_MEANING,word.getMeaning());

    db.insert(TABLE_WORDS,null,values);
    db.close();
}

public Word getWord(int id){

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WORDS,null,KEY_ID + " = ?",new String[]{String.valueOf(id)},null,null,null);
    if (cursor != null)
        cursor.moveToFirst();

    Word word = new Word(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2));
    return word;

}

}

这是我的"字"类

public class Word  {
int _id;
String word;
String meaning;


public Word(int id,String word, String meaning)
{
    this._id = id;
    this.word = word;
    this.meaning = meaning;
}


public int getId() {
    return this._id;
}

public void setId(int id) {
    this._id = id;
}

public String getMeaning() {
    return this.meaning;
}

public void setMeaning(String meaning) {
    this.meaning = meaning;
}

public String getWord() {
    return this.word;
}

public void setWord(String word) {
    this.word = word;
}

}

这是我的mainActivity

public class MainActivity extends AppCompatActivity {

TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
Button btn1;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView)findViewById(R.id.tv1);
    tv2 = (TextView)findViewById(R.id.tv2);
    tv3 = (TextView)findViewById(R.id.tv3);
    tv4 = (TextView)findViewById(R.id.tv4);
    btn1 = (Button)findViewById(R.id.btn1);

    DBHandler db = new DBHandler(this);
    db.addWord(new Word(1,"abc","def"));
    Word word = db.getWord(1);
    tv1.setText(word.getWord());
    tv2.setText(word.getMeaning());

}

}

我尝试从数据库中取出值并将它们插入到textview中以检查它是否正常工作但是通过logcat我可以明显地看到它在插入过程中的某个位置。

我的logcat

  

com.example.android.psydictionary E / SQLiteLog:(1555)10 in in [INSERT INTO Words(含义,含义,id)VALUES(?,?,?)]:UNIQUE约束失败:Words.id   11-19 12:34:44.875 17853-17853 / com.example.android.psydictionary E / SQLiteDatabase:插入含义错误= def word = abc id = 1                                                                                      android.database.sqlite.SQLiteConstraintException:UNIQUE约束失败:Words.id(代码1555)                                                                                          在android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(本机方法)                                                                                          在android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:784)                                                                                          在android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)                                                                                          在android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)                                                                                          在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)                                                                                          在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)                                                                                          在com.example.android.psydictionary.DBHandler.addWord(DBHandler.java:51)                                                                                          在com.example.android.psydictionary.MainActivity.onCreate(MainActivity.java:31)                                                                                          在android.app.Activity.performCreate(Activity.java:5990)                                                                                          在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)                                                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)                                                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)                                                                                          在android.app.ActivityThread.access $ 900(ActivityThread.java:154)                                                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)                                                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                          在android.os.Looper.loop(Looper.java:135)                                                                                          在android.app.ActivityThread.main(ActivityThread.java:5294)                                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)                                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

1 个答案:

答案 0 :(得分:1)

UNIQUE constraint failed说出问题所在。您对列有一个UNIQUE约束(意思是单词, id )并尝试在该列中插入两次相同的值。

很可能是id失败了。也许你可以离开它并在插入过程中计算它。否则,请检查您的testdata。