我正面臨着一個錯誤:我確定它不是很多,但我有點卡住它!我發現了一個錯誤,而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();
}
UPDATE語句的語法不同於INSERT語句。去谷歌上查詢。 – Eran
[Android中的SQLite的可能重複如何更新特定的行](https://stackoverflow.com/questions/9798473/sqlite-in-android-how-to-update-a-specific-row) –
UPDATE INTO '不存在 –