2017-10-15 81 views
-4

我正面臨着一個錯誤:我確定它不是很多,但我有點卡住它!我發現了一個錯誤,而compilate:SQLiteException:接近「INTO」:語法錯誤(代碼1):

SQLiteException: near "INTO": syntax error (code 1): , while compiling: UPDATE INTO (PartListDetail(ProductID,ProductName,Quantity) VALUES('','null','0');)

如果有人可以幫助這將是非常好的(我的英語很抱歉)

這是我DataBase.class和我Cart.class:

數據庫

public class Database extends SQLiteAssetHelper { 
    private static final String DB_NAME="myBrickList.db"; 
    private static final int DB_VER = 1; 
    public Database(Context context) { 
     super(context, DB_NAME, null, DB_VER); 
    } 

    public List<PartList> getCarts(){ 

     SQLiteDatabase db = getReadableDatabase(); 
     SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 

     String[] sqlSelect = {"ProductName","ProductID","Quantity"}; 
     String sqlTable = "PartListDetail"; 

     qb.setTables(sqlTable); 
     Cursor c = qb.query(db, sqlSelect, null, null, null,null, null); 

     final List<PartList> result = new ArrayList<>(); 
     if(c.moveToFirst()) 
     { 
      do{ 
       result.add(new PartList(c.getString(c.getColumnIndex("ProductID")), 
         c.getString(c.getColumnIndex("ProductName")), 
         c.getString(c.getColumnIndex("Quantity")) 
       )); 
      }while (c.moveToNext()); 
     } 
     return result; 

    } 


    public void addToCart(PartList partList) 
    { 
     SQLiteDatabase db = getReadableDatabase(); 
     String query = String.format("INSERT INTO PartListDetail(ProductID,ProductName,Quantity) VALUES('%s','%s','%s');", 
       partList.getProductID(), 
       partList.getProductName(), 
       partList.getQuantity()); 

     db.execSQL(query); 
    } 


    public void UpdateToCart(PartList partList) 
    { 
     SQLiteDatabase db = getReadableDatabase(); 
     String query = String.format("UPDATE INTO PartListDetail(ProductID,ProductName,Quantity) VALUES('%s','%s','%s');", 

       partList.getProductID(), 
       partList.getProductName(), 
       partList.getQuantity()); 

     db.execSQL(query); 
    } 

Cart.java

public class Cart extends AppCompatActivity { 

    RecyclerView recyclerView; 
    RecyclerView.LayoutManager layoutManager; 

    DatabaseReference requests; 
    FirebaseStorage storage; 
    StorageReference storageReference; 

    TextView txtTotalPrice; 
    FButton btnPlace; 
    List<PartList> cart = new ArrayList<>(); 
    CartAdapter adapter; 
    FButton btnUp; 

    String brickId=""; 
    Brick currentBrick; 

    FirebaseDatabase database; 

    ElegantNumberButton numberButton; 

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

     //Firebase 
     database = FirebaseDatabase.getInstance(); 
     requests = database.getReference("Brick"); 
     storage = FirebaseStorage.getInstance(); 
     storageReference = storage.getReference(); 

     //Initialisation 
     recyclerView = (RecyclerView)findViewById(R.id.listCart); 
     recyclerView.setHasFixedSize(true); 
     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 

     currentBrick = new Brick(); 

     numberButton = new ElegantNumberButton(getBaseContext()); 

     btnPlace = (FButton)findViewById(R.id.btnPlaceList); 
     btnPlace.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(cart.size() > 0) 
        showAlertDialog(); 
       else 
        Toast.makeText(Cart.this, "Your list is empty", Toast.LENGTH_SHORT).show(); 

      } 
     }); 

    } 

    private void loadListBrick() { 
     cart = new Database(this).getCarts(); 
     adapter = new CartAdapter(cart,this); 
     adapter.notifyDataSetChanged(); 
     recyclerView.setAdapter(adapter); 

    } 



    @Override 
    public boolean onContextItemSelected(MenuItem item) { 

     if (item.getTitle().equals(Common.DELETE)) 
      deleteCart(item.getOrder()); 
      if (item.getTitle().equals(Common.MISEAJOUR)) 
       showDialog(); 


     return true; 
     } 



    private void showDialog() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this); 
     alertDialog.setTitle("New Quantity"); 
     alertDialog.setMessage("Please fill full information"); 

     LayoutInflater inflater = this.getLayoutInflater(); 
     View add_menu_layout = inflater.inflate(R.layout.number_layout,null); 

     btnUp = (FButton)add_menu_layout.findViewById(R.id.btnUploadNumber); 


     btnUp.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new Database(getBaseContext()).UpdateToCart(new PartList(
         brickId, 
         currentBrick.getName(), 
         numberButton.getNumber() 

       )); 
       loadListBrick(); 
       Toast.makeText(Cart.this, "Add to MyList", Toast.LENGTH_SHORT).show(); 
      } 
     }); 


     alertDialog.setView(add_menu_layout); 
     alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp); 

     alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 

       dialogInterface.dismiss(); 

      } 
     }); 

     alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       dialogInterface.dismiss(); 
      } 
     }); 
     alertDialog.show(); 
    } 
+2

UPDATE語句的語法不同於INSERT語句。去谷歌上查詢。 – Eran

+4

[Android中的SQLite的可能重複如何更新特定的行](https://stackoverflow.com/questions/9798473/sqlite-in-android-how-to-update-a-specific-row) –

+0

UPDATE INTO '不存在 –

回答

1

語法UPDATE查詢

UPDATE table_name 
SET column1 = value1, column2 = value2, ... 
WHERE condition; 

您正在使用UPDATE INTO。沒有這樣的命令。

使用

UPDATE PartListDetail SET ProductID=%s,ProductName=%s,Quantity=%s; 

SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put("ProductID", ProductIDValue); 
    contentValues.put("ProductName", ProductNameValue); 
    contentValues.put("Quantity", QuantityValue); 
    db.update("PartListDetail", contentValues, "id = ? ", new String[] { Integer.toString(id) }); 
+0

感謝您的快速回答,但我有一個錯誤: –

+0

SQLiteException:接近「,」:語法錯誤(代碼1):,編譯時:更新PartListDetail SET ProductID =,ProductName = null,Quantity = 0 ; –

+0

您沒有正確設置數據。 partList.getProductID()和getProductName()返回錯誤的數據。 –

0
public void UpdateToCart(PartList partList) 
    { 
     SQLiteDatabase db = getReadableDatabase(); 
     String query = String.format("UPDATE PartListDetail SET ProductID=%s,ProductName=%s,Quantity=%s;", 

       partList.getProductID(), 
       partList.getProductName(), 
       partList.getQuantity()); 

     db.execSQL(query); 
    } 

我沒有看到我'錯了,感謝您的答案。

相關問題