嗨我寫這個應用程序,需要使用數據庫,我碰到了一個障礙。我似乎無法從數據庫中獲取信息。我有以下代碼:如何從sqllite表中獲取數據
合同類:
public class SettingsContract {
private SettingsContract() {
}
public final class SettingsEntry implements BaseColumns {
public static final String TABLE_NAME = "Settings";
public static final String COLUMN_NAME_LAST_REFRESH_DT = "LastRefreshDt";
}
}
DbHelper類:
public class SettingsDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Database.db";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE IF NOT EXISTS" + SettingsContract.SettingsEntry.TABLE_NAME + " (" +
SettingsContract.SettingsEntry._ID + " INTEGER PRIMARY KEY," +
SettingsContract.SettingsEntry.COLUMN_NAME_LAST_REFRESH_DT + " DATE,";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + SettingsContract.SettingsEntry.TABLE_NAME;
public SettingsDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
而且,我用這個代碼:
private SettingsDbHelper dbHelper;
public SettingsDataSource(Context context) {
this.dbHelper = new SettingsDbHelper(context);
}
public Setting getSettings() {
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT " +
SettingsContract.SettingsEntry.COLUMN_NAME_LAST_REFRESH_DT + " FROM "
+ SettingsContract.SettingsEntry.TABLE_NAME, null);
...
現在,當我打電話rawQuery這是我在日誌中的內容:
04-15 12:18:14.026 2698-2698/com.busridercluj E/SQLiteLog:
(1) no such table: Settings
我在這裏錯過了什麼?爲什麼不按照它應該創建的表?
「CREATE TABLE IF NOT EXISTS」 - >「CREATE TABLE如果不存在「 - 請注意額外的水療中心ce在最後 – pskink
您的CREATE TABLE失敗...顯示更多的logcat –