SQLiteException:near" INTO":语法错误(代码1):

时间:2017-10-15 10:08:40

标签: java android sqlite

我面临一个错误:我确定它并不多,但我有点卡住它!我在编译时遇到错误:

  

SQLiteException:near" INTO&#34 ;:语法错误(代码1):, while   编译:UPDATE INTO(PartListDetail(ProductID,ProductName,Quantity)   VALUES('''空'' 0&#39);)

如果有人可以提供帮助,那将是非常好的(对不起我的英语)

这是我的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 个答案:

答案 0 :(得分: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) } );

答案 1 :(得分: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);
    }

我不知道我错在哪里,谢谢你的答案。