2015-12-09 18 views
-1

將數據插入到第二個表``我使用SQLite Database.I已經創建了兩個tables.first表是用戶表,它成功地創建和inserted.but在我的第二個表還成功創建,但不insert.this是我進步problem.Thank ..無法在Android的

這是OnClick事件在這裏嘗試調用插件的方法是存在於數據庫類

public void onClick(View v) { 
    String itemname; 
    String price; 
    int position; 
    if(v.getId()==R.id.id_btn_submit){ 
     //insert the price into database 
      itemname =mEditItemName.getText().toString(); 
      price =(mEditPrice.getText().toString()); 
      position=spinner.getSelectedItemPosition(); 

     if(itemname.isEmpty()){ 
      mEditItemName.setError("enter the item name"); 

     }else if(price.isEmpty()){ 
      mEditPrice.setError("pls enter the price"); 
     }else{ 
      dataBase=new DataBase(this); 
      position=position+1; 
      mItemId= dataBase.insertAll(itemname,price, String.valueOf(position)); 
     } 
     mEditItemName.setText(""); 
     mEditPrice.setText(""); 
     Log.d("bucky ","succefully insert "); 

    } 

這是插入方法

public long insertAll(String itemname ,String price,String itemcategory){ 
    SQLiteDatabase sqLiteDatabase=databasehelper.getWritableDatabase(); 
    ContentValues content=new ContentValues(); 
    content.put(databasehelper.mITEMNAME, itemname); 
    content.put(databasehelper.mITEMPRICE, price); 
    content.put(databasehelper.mITEMCATEGORY, itemcategory); 
    long id=sqLiteDatabase.insert(databasehelper.mTABLEMENUITEM,null,content); 
    Log.d("bucky insert item", String.valueOf(id)); 
    return id; 
} 

CREATE語句和UPDATE語句

public class DataBaseHelper extends SQLiteOpenHelper{ 
    private final static String mDATABASENAME="foodpanta"; 
    private final static int mVERSION=9; 
    private Context context; 
    //user information table 
    private final static String mTABLEUSERDETAIL="userdetail"; 
    private final static String mUID="_id"; 
    private final static String mUSERNAME="username"; 
    private final static String mUSERMAIL="usermail"; 
    private final static String mUSERPASS="userpass"; 
    private final static String mUSERMOBILE="usermobile"; 
    private final static String mCREATETABLEUSER ="CREATE TABLE " + mTABLEUSERDETAIL +" ("+mUID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +mUSERNAME+" VARCHAR(255) NOT NULL, "+mUSERMAIL+" VARCHAR(255) NOT NULL, "+mUSERPASS+" VARCHAR(255) NOT NULL, "+mUSERMOBILE+" VARCHAR(255));"; 

    //Adding menu item table 
    private final static String mTABLEMENUITEM="menuitem"; 
    private final static String mITEMID="_id"; 
    private final static String mITEMCATEGORY="item category"; 
    private final static String mITEMPRICE="itemprice"; 
    private final static String mITEMNAME="itemname"; 
    private final static String mCREATETABLEMENU="CREATE TABLE " + mTABLEMENUITEM +" ("+mITEMID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +mITEMNAME+" VARCHAR(255) NOT NULL, "+mITEMPRICE+" VARCHAR(255) NOT NULL, "+mITEMCATEGORY+" VARCHAR(255) NOT NULL);"; 
    public DataBaseHelper(Context context) { 
     super(context, mDATABASENAME, null, mVERSION); 
     this.context=context; 
     Message.message(context,"on constructor called"); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(mCREATETABLEUSER); 

     db.execSQL(mCREATETABLEMENU); 
     Message.message(context, "on create called"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + mTABLEUSERDETAIL); 

     db.execSQL("DROP TABLE IF EXISTS " + mTABLEMENUITEM); 
     Message.message(context, "on upgrade called"); 
     onCreate(db); 

    } 

} 

Stack Trace: 
12-09 17:13:45.698 15388-15388/com.example.node10.foodcourt E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method android.support.v7.widget.AppCompatSpinner$DropDownAdapter.<init> 
12-09 17:13:46.085 672-672/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History> 
12-09 17:13:46.086 672-672/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History> 
12-09 17:13:48.801 672-672/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History> 
12-09 17:13:48.802 672-672/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History> 
12-09 17:13:51.138 15388-15388/com.example.node10.foodcourt E/SQLiteLog: (1) near "category": syntax error 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase: Error inserting item category=1 itemprice=59 itemname= bzb 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase: android.database.sqlite.SQLiteException: near "category": syntax error (code 1): , while compiling: INSERT INTO menuitem(item category,itemprice,itemname) VALUES (?,?,?) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1492) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1364) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at com.example.node10.foodcourt.DataBase.insertAll(DataBase.java:72) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at com.example.node10.foodcourt.ItemAdd.onClick(ItemAdd.java:75) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.view.View.performClick(View.java:4463) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.view.View$PerformClick.run(View.java:18770) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.os.Handler.handleCallback(Handler.java:808) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.os.Handler.dispatchMessage(Handler.java:103) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.os.Looper.loop(Looper.java:193) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at android.app.ActivityThread.main(ActivityThread.java:5300) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at java.lang.reflect.Method.invokeNative(Native Method) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at java.lang.reflect.Method.invoke(Method.java:515) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646) 
12-09 17:13:51.140 15388-15388/com.example.node10.foodcourt E/SQLiteDatabase:  at dalvik.system.NativeStart.main(Native Method) 
+0

你可以發佈你的堆棧跟蹤? – Fustigador

+0

我調試了代碼,發現id值是-1。 – rafeek

+0

這是因爲它沒有插入。插入將返回插入值的PK值。 – Fustigador

回答

0
INSERT INTO menuitem(item category,itemprice,itemname) VALUES (?,?,?) 

還有就是你的錯誤,「項目類別」應爲「itemcategory」,你增加了一個空間的地方,和SQLite認爲你只是試圖插入四個值代替三個。

具體來說,在這裏:

private final static String mITEMCATEGORY="item category";