1
未創建SQLite database
。過去幾個小時我一直在玩這個遊戲,但現在我累了很多。在Log Cat
中我沒有收到任何錯誤或警告。這是我迄今爲止嘗試過的代碼。幫助將不勝感激。未創建Android SQLite數據庫
public class MySQLiteHelper extends SQLiteOpenHelper{
final static String DB_NAME = "citiesdata.db";
final static String TABLE_NAME = "allcities";
final static String ID = "_id";
final static String CITY_CODE = "city_code";
final static int DB_VERSION = 1;
private SQLiteDatabase db;
Context cont;
public MySQLiteHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
cont = context;
Toast.makeText(cont, "constructor called", Toast.LENGTH_LONG).show();
}
@Override
public void onCreate(SQLiteDatabase db) {
Toast.makeText(cont, "onCreate method called", Toast.LENGTH_LONG).show();
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CITY_CODE + " TEXT);";
db.execSQL(createTable);
// INSERTING RECORDS IN DATABASE
SQLiteDatabase writableDB = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CITY_CODE, "333");
writableDB.insert(TABLE_NAME, null, values);
writableDB.close();
readCity();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(cont, "onUpgrade method called", Toast.LENGTH_LONG).show();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void readCity(){
Toast.makeText(cont, "readCity method called", Toast.LENGTH_LONG).show();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Toast.makeText(cont, cursor.getString(cursor.getColumnIndex(CITY_CODE)), Toast.LENGTH_LONG).show();
db.close();
}
}
在主UI中調用構造函數按鈕上的線程單擊像這樣。
new MySQLiteHelper(MainActivity.this);
權限是...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
非常感謝提前。
請從初始化'MySQLiteHelper'處顯示更多代碼 – tahsinRupam
確切的問題是什麼? 'readCity()'失敗了嗎? –