我正在尝试将SQLite数据库添加到我的Android应用程序中。 我为pin表(Pin.java)创建了一个单独的类 MySQLiteHelper.java中的数据库处理程序
现在我正在尝试在我的一个活动中调用addPin方法 它给了我一个错误: http://pastebin.com/8h4k3Quh
11-20 18:39:44.011: E/SQLiteLog(22644): (1) no such table: tblPins
11-20 18:39:44.021: E/SQLiteDatabase(22644): Error inserting pinMessage=testinh pinDuration=1 pinTitle=test
11-20 18:39:44.021: E/SQLiteDatabase(22644): android.database.sqlite.SQLiteException: no such table: tblPins (code 1): , while compiling: INSERT INTO tblPins(pinMessage,pinDuration,pinTitle) VALUES (?,?,?)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.github.viperdream.MySQLiteHelper.addPin(MySQLiteHelper.java:54)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.github.viperdream.AddPinActivity.btnCreatePin(AddPinActivity.java:39)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.view.View$1.onClick(View.java:3628)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.view.View.performClick(View.java:4240)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.view.View$PerformClick.run(View.java:17721)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.os.Handler.handleCallback(Handler.java:730)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.os.Looper.loop(Looper.java:137)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at dalvik.system.NativeStart.main(Native Method)
11-20 18:39:44.021: E/SQLiteLog(22644): (1) no such table: tblPins
11-20 18:39:44.021: D/AndroidRuntime(22644): Shutting down VM
11-20 18:39:44.021: W/dalvikvm(22644): threadid=1: thread exiting with uncaught exception (group=0x415df700)
11-20 18:39:44.021: E/AndroidRuntime(22644): FATAL EXCEPTION: main
11-20 18:39:44.021: E/AndroidRuntime(22644): java.lang.IllegalStateException: Could not execute method of the activity
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View$1.onClick(View.java:3633)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View.performClick(View.java:4240)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View$PerformClick.run(View.java:17721)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.os.Handler.handleCallback(Handler.java:730)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.os.Looper.loop(Looper.java:137)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 18:39:44.021: E/AndroidRuntime(22644): at dalvik.system.NativeStart.main(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): Caused by: java.lang.reflect.InvocationTargetException
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View$1.onClick(View.java:3628)
11-20 18:39:44.021: E/AndroidRuntime(22644): ... 11 more
11-20 18:39:44.021: E/AndroidRuntime(22644): Caused by: android.database.sqlite.SQLiteException: no such table: tblPins (code 1): , while compiling: Select * FROM tblPins WHERE pinTitle = "test"
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.github.viperdream.MySQLiteHelper.findPin(MySQLiteHelper.java:61)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.github.viperdream.AddPinActivity.btnCreatePin(AddPinActivity.java:41)
它说它无法找到表tblPins,但我认为它会在调用函数后自动创建。
我似乎无法想出这个,这是我的代码:
Databasehandler,MySQLiteHelper.java:
public class MySQLiteHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "PinDB";
private static final String TABLE_PINS = "tblPins";
private static final String KEY_PINID = "pin_id";
private static final String KEY_PINTITLE = "pinTitle";
private static final String KEY_PINMESSAGE = "pinMessage";
private static final String KEY_PINDURATION = "pinDuration";
private static final String[] COLUMNS = {KEY_PINID, KEY_PINTITLE, KEY_PINMESSAGE, KEY_PINDURATION};
public MySQLiteHelper(Context context, String name, CursorFactory factory, int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE "
+ TABLE_PINS + " ("
+ KEY_PINID +" INTEGER PRIMARY KEY," + KEY_PINTITLE +
" TEXT,"+
KEY_PINMESSAGE+" TEXT,"
+ KEY_PINDURATION +" INTEGER "+ ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersio, int newVersion){
db.execSQL("DROP TABLE IF EXISTS tblPins");
this.onCreate(db);
}
public void addPin(Pin pin){
Log.d("addPin", pin.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PINTITLE, pin.getPinTitle());
values.put(KEY_PINMESSAGE, pin.getPinMessage());
values.put(KEY_PINDURATION, pin.getPinDuration());
db.insert(TABLE_PINS, null, values);
db.close();
}
public Pin findPin(String pinTitle){
String query = "Select * FROM " + TABLE_PINS + " WHERE " + KEY_PINTITLE + " = \"" + pinTitle +"\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Pin pin = new Pin();
if (cursor.moveToFirst()){
cursor.moveToFirst();
pin.setPinID(Integer.parseInt(cursor.getString(0)));
pin.setPinTitle(cursor.getString(1));
pin.setPinMessage(cursor.getString(2));
pin.setPinDuration(Integer.parseInt(cursor.getString(3)));
cursor.close();
}else{
pin = null;
}
db.close();
return pin;
}
public boolean deletePin(String pinTitle){
boolean result = false;
String query = "Select * FROM " + TABLE_PINS + " WHERE " + KEY_PINTITLE + " = \"" + pinTitle + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Pin pin = new Pin();
if(cursor.moveToFirst()){
pin.setPinID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_PINS, KEY_PINID + " = ?",
new String[] { String.valueOf(pin.getID()) });
cursor.close();
result = true;
}
db.close();
return result;
}
}
如果您需要更多信息,请不要犹豫!
提前致谢
更新:我已经设法得到了表格,我在addPin方法中放了onCreate(),但是有什么理由为什么当我不把它放在addPin中时这个方法不被调用?