我是一個在數據庫中的新手。目前我已經創建了一個應用程序,我想將它鏈接到SQLite數據庫。在遵循YouTube上的教程之後,我通過嘗試添加簡單的行來測試我的數據庫。我得到一個錯誤E/SQLiteLog: (1) no such table: SCORES
E/SQLiteDatabase: Error inserting DScore=0 LScore=0 Name=Aris
這是我的代碼,截至目前:嘗試在android中插入一行sqlite數據庫NoSuchTable存在
public class DB_Controller {
private DB_Scores helper;
public DB_Controller(Context context) {
helper = new DB_Scores(context);
}
public long insertRow(String name){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DB_Scores.NAME, name);
values.put(DB_Scores.DSCORE, 0);
values.put(DB_Scores.LSCORE, 0);
long id = db.insert(DB_Scores.TABLE_NAME,null, values);
return id;
}
static class DB_Scores extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "scores.db";
private static final String TABLE_NAME = "SCORES";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String NAME = "Name";
private static final String LSCORE = "LScore";
private static final String DSCORE = "DScore";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY " +
"AUTOINCREMENT, "+NAME+" TEXT UNIQUE, "
+LSCORE+" INTEGER, "+DSCORE+" INTEGER);";
private Context context;//?
public DB_Scores(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Toast.makeText(context, "Constructor was called", Toast.LENGTH_LONG).show();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
Toast.makeText(context, "onCreate was called", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
Toast.makeText(context, "onUpgrade called", Toast.LENGTH_LONG).show();
onCreate(db);
}catch(SQLException e){
Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
}
}
}
}
以及活動,我創建DB_Controller的對象
public class Scores_Activity extends AppCompatActivity {
private DB_Controller dbController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scores_activity);
dbController = new DB_Controller(this);
addRow();
}
public void addRow() {
long id=dbController.insertRow("Aris");
if (id < 0) {
Toast.makeText(this, "Unsuccessful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
}
它標記爲錯誤的行來源是long id=dbController.insertRow("Aris");
,
long id = db.insert(DB_Scores.TABLE_NAME,null, values);
和addRow();
我的猜測是,我犯了一個邏輯錯誤whil Ë創建database.Thanks您的時間
第一個應用程序執行後,你可能改變你的表名。 –
使用ormlite代替sqlite –
當我測試時,我玩了數據庫名稱,但我認爲我從來沒有改變表名。你建議的是,我第一次創建這個數據庫我有一個不同的表,現在我試圖從頭開始執行onCreate方法不執行?有什麼方法可以檢查是否是這種情況? @Rotwang –