未创建Android SQLite数据库 [英] Android SQLite database is not created
问题描述
未创建 SQLite数据库
。在过去的几个小时里,我一直在玩这个游戏,但是现在我已经很累了。 Log Cat
没有收到任何错误或警告。这是到目前为止我尝试过的代码。
The SQLite database
is not created. I am playing with this for the past few hours but now i got tired a lot. I am not getting any errors or warnings in the Log Cat
. Here is my code what I have tried so far. Help will be appreciated.
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线程上的按钮上调用构造函数
Calling the Constructor in the main UI Thread on a button click like this.
new MySQLiteHelper(MainActivity.this);
权限是...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
多谢了。
推荐答案
最后,我将问题直接打入了头脑。但是如何?
At last, I shot the problem directly into the head. But How?
SQLiteOpenHelper
的逻辑是它不调用通过仅创建对象来创建onCreate
方法。
The logic of the SQLiteOpenHelper
is that it doesn't call the onCreate
method by only creating its object.
- 每当您尝试访问数据库以检索或插入数据时,如果数据库尚未创建/不可用,它将首次调用
onCreate
方法。 - <$ c $如果数据库可用,但版本低于您提供的版本,则将调用c> onUpgrade 方法。
- Whenever you try to access the database for retrieving or inserting data, if the database is not already created/available, it will call the
onCreate
method for the first time. - The
onUpgrade
method will be called if the database is available but the version is lower than the one you provide.
我在我的 MySQLiteHelper
类中创建了一个 fillTable
方法,在该类中,我第一次尝试访问该数据库通过调用 SQLiteOpenHelper
的 getWritableDatabase
方法编写。 fillTable
方法的定义如下所示。
I created a fillTable
method in my MySQLiteHelper
class where I tried to access the database for the first time for writing by calling getWritableDatabase
method of the SQLiteOpenHelper
. The definition of the fillTable
method looks like this.
public void fillTable(){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CITY_CODE, "333");
db.insert(TABLE_NAME, null, values);
db.close();
readCity();
}
在该类的新实例上调用fillTable方法:
Call the fillTable method on a new instance of the class:
new MySQLiteHelper(MainActivity.this).fillTable();
感谢所有尝试在评论中帮助我的人。
Thanks to all those who tried to help me in comments.
这篇关于未创建Android SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!