我正在使用SQLITE数据库开发一个Android应用程序,目前正面临一个问题。以下是我的代码
public class DatabaseHelper extends SQLiteOpenHelper {
// Log cat tag
private static final String LOG = DatabaseHelper.class.getName();
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "quotes_hub";
// Table Name
private static final String TABLE_QUOTE = "quote";
private Context context;
SQLiteDatabase db1;
// Quote Table - column names
private static final String KEY_ID = "id";
private static final String KEY_TEXT = "text";
private static final String KEY_FAVOURITE = "favourite";
// table create statement
private static final String CREATE_TABLE_QUOTE = "create table "
+ TABLE_QUOTE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TEXT + " TEXT, "
+ KEY_FAVOURITE + " TEXT DEFAULT 'NO'" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
System.out.println("sdfsdfsfsdfsfdsfs"+CREATE_TABLE_QUOTE);
}
public void onCreate(SQLiteDatabase db) {
System.out.println("************************************");
db.execSQL(CREATE_TABLE_QUOTE);
try {
int number =insertFromFile(context, R.raw.dump);
System.out.println("========================================= "+number );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
// creating exam entry in database
public long createQuote(Quote quote) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TEXT, quote.getText());
values.put(KEY_FAVOURITE, quote.getIsFavourite());
// insert row
long rowId = db.insert(TABLE_QUOTE, null, values);
return rowId;
}
// retrieving all entries from quote table
public List<Quote> getAllQuotes() {
List<Quote> quotes = new ArrayList<Quote>();
String selectQuery = "SELECT * FROM " + TABLE_QUOTE;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
if(db == null){
System.out.println("this is the database "+db );
}
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Quote quote = new Quote();
quote.setText(c.getString((c.getColumnIndex(KEY_TEXT))));
quote.setIsFavourite(c.getString((c
.getColumnIndex(KEY_FAVOURITE))));
// adding quote to quotes list
quotes.add(quote);
} while (c.moveToNext());
}
return quotes;
}
public int insertFromFile(Context context, int resourceId) throws IOException {
// Reseting Counter
int result = 0;
SQLiteDatabase db = this.getWritableDatabase();
// Open the resource
InputStream insertsStream = context.getResources().openRawResource(resourceId);
BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));
// Iterate through lines (assuming each insert has its own line and theres no other stuff)
while (insertReader.ready()) {
String insertStmt = insertReader.readLine();
db.execSQL(insertStmt);
result++;
}
insertReader.close();
// returning number of inserted rows
return result;
}
}
上面代码的问题是即使在调用getReadableDatabase()方法之后也没有调用onCreate方法。当我运行应用程序时,它给出了一个错误,指出名为“Quote”的表不存在。请帮帮我。
谢谢。
答案 0 :(得分:1)
onCreate()未被调用 - 它的功能不是错误;)只有在第一次装箱数据库时才会调用此方法 - 从您的设备卸载应用程序并在下次启动时调用它。
或者你可以实现onUpgrade()方法 - 当你增加数据库版本时(当你的常量高于db中保存的常量时)会调用这个方法。