「私はSQLite
データベースを使用しています。2 つのテーブルを作成しました。最初のテーブルはユーザー テーブルです。作成と挿入は成功していますが、2 番目のテーブルも正常に作成されましたが、挿入されていません。これは私の問題です。よろしくお願いします。..
これは 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 Statement と update 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)