2015-01-11 106 views
0

我的應用程序這一塊引起我一些悲傷...或者是沒有得到正確創建我的表或插入語句是脫節的。我遵循這個教程,似乎很好地解釋了http://www.giantflyingsaucer.com/blog/?p=1342,但我似乎無法找到我的代碼和他們的代碼之間的區別。任何建議將不勝感激!SQLite的問題 - 的onCreate或插入

這裏是我的MainActivity代碼:

私人SQLiteIngAssistant sqlliteIngAssistant;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    sqlliteIngAssistant = new SQLiteIngAssistant(MainActivity.this); 
    sqlliteIngAssistant.openDB(); 

    sqlliteIngAssistant.insertIng("Foo"); 
    String[] autofillIngs = sqlliteIngAssistant.getAllIngNames(); 

}

這裏是我的SQL助手:

public class SQLiteIngAssistant extends SQLiteOpenHelper { 
    private static final String DB_NAME = "usingsqlite.db"; 
    private static final int DB_VERSION_NUMBER = 1; 
    private static final String DB_TABLE_NAME = "ings"; 
    private static final String DB_COLUMN_1_NAME = "ing_name"; 
    private static final String DB_COLUMN_2_NAME = "cons"; 

    private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME + 
      " (_id string primary key " + DB_COLUMN_1_NAME + ", " + DB_COLUMN_2_NAME + " text not null);)"; 

    private SQLiteDatabase sqliteDBInstance = null; 

    public SQLiteIngAssistant(Context context){ 
     super(context, DB_NAME, null, DB_VERSION_NUMBER); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
     // TODO: Implement onUpgrade 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sqliteDBInstance){ 
     Log.i("onCreate", "Creating the database..."); 
     sqliteDBInstance.execSQL(DB_CREATE_SCRIPT); 
    } 

    public void openDB() throws SQLException{ 
     Log.i("openDB", "Checking sqliteDBInstance..."); 
     if(this.sqliteDBInstance == null){ 
      Log.i("openDB", "Creating sqliteDBInstance..."); 
      this.sqliteDBInstance = this.getWritableDatabase(); 
     } 
    } 

    public long insertIng(String Name){ 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(DB_COLUMN_1_NAME, Name); 
     return this.sqliteDBInstance.insert(DB_TABLE_NAME, null, contentValues); 
    } 


    public String[] getAllIngNames(){ 
     Cursor cursor = this.sqliteDBInstance.query(DB_TABLE_NAME, new String[] {DB_COLUMN_1_NAME}, null, null, null, null, null); 

     if(cursor.getCount() >0){ 
      String[] str = new String[cursor.getCount()]; 
      int i = 0; 

      while (cursor.moveToNext()){ 
       str[i] = cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_NAME)); 
       i++; 
      } 
      return str; 
     } else { 
      return new String[] {}; 
     } 
    } 
} 

這裏是堆棧跟蹤:

01-11 19:23:04.382 14630-14630/com.example.ciltkick.nom E/SQLiteDatabase﹕ Error inserting ing_name=Foo 
    android.database.sqlite.SQLiteException: table ings has no column named ing_name (code 1): , while compiling: INSERT INTO ings(ing_name) VALUES (?) 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893) 
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504) 
      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
      at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1475) 
      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1347) 
      at com.example.ciltkick.nom.SQLiteIngAssistant.insertIng(SQLiteIngAssistant.java:61) 
      at com.example.ciltkick.nom.MainActivity.onCreate(MainActivity.java:65) 
      at android.app.Activity.performCreate(Activity.java:5008) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096) 
      at android.app.ActivityThread.access$600(ActivityThread.java:138) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:213) 
      at android.app.ActivityThread.main(ActivityThread.java:4787) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
      at dalvik.system.NativeStart.main(Native Method) 
01-11 19:23:04.422 14630-14630/com.example.ciltkick.nom E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ciltkick.nom/com.example.ciltkick.nom.MainActivity}: android.database.sqlite.SQLiteException: no such column: ing_name (code 1): , while compiling: SELECT ing_name FROM ings 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096) 
      at android.app.ActivityThread.access$600(ActivityThread.java:138) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:213) 
      at android.app.ActivityThread.main(ActivityThread.java:4787) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.database.sqlite.SQLiteException: no such column: ing_name (code 1): , while compiling: SELECT ing_name FROM ings 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893) 
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504) 
      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1322) 
      at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1169) 
      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1040) 
      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1208) 
      at com.example.ciltkick.nom.SQLiteIngAssistant.getAllIngNames(SQLiteIngAssistant.java:89) 
      at com.example.ciltkick.nom.MainActivity.onCreate(MainActivity.java:77) 
      at android.app.Activity.performCreate(Activity.java:5008) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096) 
            at android.app.ActivityThread.access$600(ActivityThread.java:138) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:213) 
            at android.app.ActivityThread.main(ActivityThread.java:4787) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
            at dalvik.system.NativeStart.main(Native Method) 

回答

2
private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME + 
     " (_id string primary key " + DB_COLUMN_1_NAME + ", " + DB_COLUMN_2_NAME + " text not null);)"; 

你錯過了前一列DEF的primary key及以下DB_COLUMN_1_NAME之間的,

將它添加那裏以後,卸載應用程序來創建數據庫。


我想DB_COLUMN_1_NAME成爲主鍵的名稱,我該怎麼辦呢?

它更改爲類似

private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME + 
     " (" + DB_COLUMN_1_NAME + " string primary key," + DB_COLUMN_2_NAME + " text not null);)"; 

這也消除了主鍵列_id你面前了。

或者我可以訪問的行不使用主鍵?

是的,你可以用任何標準來選擇行。請記住,如果您有大量數據並且使用非索引列進行查找,那麼性能將會很糟糕。主鍵列自動編入索引。

+0

我想DB_COLUMN_1_NAME成爲主鍵的名稱,我該怎麼辦呢?或者我可以訪問一行而不使用主鍵? – sqlbuddy

+0

謝謝!一切都很好......現在 – sqlbuddy

0

你錯過了旁邊的主鍵,也是數據類型逗號,替換這個試試,

private static final String DB_CREATE_SCRIPT = "CREATE TABLE IF NOT EXISTS " + DB_TABLE_NAME + 
      " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + DB_COLUMN_1_NAME + " , " + DB_COLUMN_2_NAME + " text not null);)";