INSERT FROM语法

时间:2017-12-07 12:56:57

标签: android sql

logcat的

    12-07 12:24:30.394 7246-7246/me.jaxbot.appfoodgrab E/SQLiteLog: (1) near "FROM": syntax error
    12-07 12:24:30.396 7246-7246/me.jaxbot.appfoodgrab E/AndroidRuntime: FATAL EXCEPTION: main
 Process: me.jaxbot.appfoodgrab, PID: 7246
 android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: INSERT FROM OrderDetail(ProductId,ProductName,Quantity,Price,Discount)VALUES('02','Nasi Goreng','5','6','5');
     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
     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.executeSql(SQLiteDatabase.java:1677)
     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
     at me.jaxbot.appfoodgrab.Database.Database.addToCart(Database.java:61)
     at me.jaxbot.appfoodgrab.FoodDetail$1.onClick(FoodDetail.java:55)
     at android.view.View.performClick(View.java:5610)
     at android.view.View$PerformClick.run(View.java:22265)
     at android.os.Handler.handleCallback(Handler.java:751)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
    12-07 12:24:30.420 1589-1839/? E/ActivityManager: Found activity ActivityRecord{dc9c9ab u0 me.jaxbot.appfoodgrab/.MainActivity t99 f} in proc activity list using null instead of expected ProcessRecord{226ea2 7246:me.jaxbot.appfoodgrab/u0a82}
    12-07 12:38:42.780 2368-2922/? E/NetworkScheduler: ignoring stale queue check message

我的编码

  public void addToCart (Order order)
    {
        SQLiteDatabase db = getReadableDatabase();
        String query = String.format("INSERT FROM OrderDetail(ProductId,ProductName,Quantity,Price,Discount)VALUES('%s','%s','%s','%s','%s');",
                order.getProductId(),
                order.getProductName(),
                order.getQuantity(),
                order.getPrice(),
                order.getDiscount());
        db.execSQL(query);

    }

2 个答案:

答案 0 :(得分:2)

问题在于SQL查询。它就像

INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]  
VALUES (value1, value2, value3,...valueN);

所以你的情况

String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount)VALUES('%s','%s','%s','%s','%s');", order.getProductId(), order.getProductName(), order.getQuantity(), order.getPrice(), order.getDiscount()); db.execSQL(query);

希望这会有所帮助。

答案 1 :(得分:1)

您的query错了

使用 INSERT INTO 代替 INSERT FROM

试试这个

public void addToCart (Order order)
    {
        SQLiteDatabase db = getReadableDatabase();
        String query = String.format("INSERT INTO  OrderDetail(ProductId,ProductName,Quantity,Price,Discount)VALUES('%s','%s','%s','%s','%s');",
                order.getProductId(),
                order.getProductName(),
                order.getQuantity(),
                order.getPrice(),
                order.getDiscount());
        db.execSQL(query);

    }