問題是''android.database.sqlite.SQLiteException:表SCHOOLSUBJECT沒有名爲SUBJECT的列(代碼1):'' 我的代碼有什麼問題? 我想把一些數據放入數據庫然後顯示它。列和表 - SQLite
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "SQLiteDatabase.db";
public static final String TABLE_NAME = "SCHOOLSUBJECT";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_FIRST_NAME = "SUBJECT";
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_FIRST_NAME + " VARCHAR);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_FIRST_NAME,item);
Log.d(" DatabaseHelper","addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME,null,contentValues);
db.close();
if(result ==1)
return false;
else
return true
}
public String getData()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
if (cursor != null)
cursor.moveToNext();
String dataa = cursor.getString(1);
return dataa;
}
}
請發佈異常的堆棧跟蹤和拋出異常的代碼。另外,嘗試在'create table'語句結尾處刪除分號。 –
您在之前的測試運行之後添加了列或更改了其名稱。 –
嘗試重新安裝您的應用或清除其數據。數據庫將再次生成,也許它會工作。你可以改變你的數據庫@ModularSynth提到 –