无法将数据插入android中的第二个表

时间:2015-12-09 10:39:00

标签: android android-sqlite android-database

``我正在使用SQLite Database.I创建了两个表。第一个表是用户表,它是成功创建和插入的。但是在我的第二个表中也成功创建但是没有插入。这是我的问题。感谢前进..

这是OnClick事件,这里尝试调用数据库类中存在的insert方法

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;
}

创建Statement并更新Statement

 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)

1 个答案:

答案 0 :(得分:0)

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

您的错误,&#34;项目类别&#34;应该是&#34; itemcategory&#34;,你在某处添加了一个空格,而sqlite认为你试图插入四个值而不是三个。

具体来说,这里:

private final static String mITEMCATEGORY="item category";