在我的应用程序中,当我转到名为“编辑单词”的屏幕,然后转到“添加单词”时
当我输入一个单词时,它不会被添加到我的database
中。
我认为问题出在我的“ DBHelper
”中的“插入单词”中。
非常感谢您的帮助。
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String WORDS_TABLE_NAME = "words";
public static final String WORDS_COLUMN_ID = "id";
public static final String WORDS_COLUMN_NAME = "word";
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table words " +
"(id integer primary key, word text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS words");
onCreate(db);
}
public boolean insertWord (String word) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("word", word);
db.insert("words", null, contentValues);
return true;
}
/* public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from words where id="+id+"",
null );
return res;
}
*/
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db,
WORDS_TABLE_NAME);
return numRows;
}
public Integer deleteWord (String word) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("words",
"word = ? ",
new String[] { word });
}
public Integer deleteWord (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("words",
"id = ? ",
new String[] { Integer.toString(id) });
}
public String getRandomWord() {
int id = (int)(Math.random())*(numberOfRows())+1;
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select word from words where id="+id+"",
null );
String s = "";
if (res.moveToFirst())
s = res.getString(res.getColumnIndex("word"));
res.close();
return s;
}
/*
public ArrayList<String> getAllWords() {
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from words", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(WORDS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}*/
}