在我添加UserUid字段之前,它工作得很好,现在它不断崩溃,我不知道为什么。这是一个数据库,我已修复它可以从特定的当前用户中选择,但是它告诉我该列不存在。
我已经重新创建了Sqlite数据库,没有运气
我遇到的错误是在GetCountCart()处,但我认为问题不存在
public class Database extends SQLiteAssetHelper {
private static String DB_NAME="OrderMeDB.db";
private static int DB_VER=1;
public Database(Context context) {
super(context, DB_NAME,null,DB_VER );
}
public List<OrderModel> getCarts(String userUid)
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"UserUid","DenumireProdus" , "Cantitate" , "Pret"};
String sqlTable = "Comanda";
qb.setTables(sqlTable);
Cursor c = qb.query(db , sqlSelect , "UserUid=?",new String[]{userUid},null,null,null);
final List<OrderModel> result = new ArrayList<>();
if(c.moveToFirst())
{
do{
result.add(new OrderModel(
c.getString(c.getColumnIndex("UserUid")),
c.getString(c.getColumnIndex("DenumireProdus")),
c.getInt(c.getColumnIndex("Cantitate")),
c.getInt(c.getColumnIndex("Pret"))
));
}while (c.moveToNext());
}
return result;
}
public boolean checkFoodExists(String userUid , String denumireProdus)
{
boolean flag = false;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = null;
String SQLQuery = String.format("SELECT * FROM Comanda WHERE UserUid='%s' AND DenumireProdus='%s'",userUid,denumireProdus);
cursor = db.rawQuery(SQLQuery,null);
if(cursor.getCount()>0)
flag = true;
else
flag = false;
cursor.close();
return false;
}
public void addToCart(OrderModel order)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT OR REPLACE INTO Comanda(UserUid,DenumireProdus,Cantitate,Pret) VALUES('%s',%s','%s','%s');",
order.getUserUid(),
order.getDenumireProdus(),
order.getCantitate(),
order.getPret());
db.execSQL(query);
}
public void cleanCart(String userUid)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM Comanda WHERE UserUid='%s'",userUid);
db.execSQL(query);
}
public void removeFromCart(String userUid,String denumireProdus)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM Comanda WHERE UserUid='%s' AND DenumireProdus='%s'",userUid,denumireProdus);
db.execSQL(query);
}
public void increaseCart(String userUid,String denumireProdus)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("UPDATE Comanda SET Cantitate= Cantitate+1 WHERE UserUid='%s' AND DenumireProdus='%s'",userUid,denumireProdus);
db.execSQL(query);
}
public int getCountCart(String userUid) {
int count = 0;
SQLiteDatabase db = getReadableDatabase();
String query = String.format("SELECT COUNT(*) FROM Comanda WHERE UserUid='%s'",userUid);
Cursor cursor =db.rawQuery(query,null);
if (cursor.moveToFirst())
{
do{
count = cursor.getInt(0);
}
while (cursor.moveToNext());
}
return count;
}
public int getItemCount(String denumireProdus , String userUid) {
int count = 0;
SQLiteDatabase db = getReadableDatabase();
String query = String.format("SELECT COUNT(*) FROM Comanda WHERE UserUid='s' AND DenumireProdus='%s' ",userUid,denumireProdus);
Cursor cursor =db.rawQuery(query,null);
if (cursor.moveToFirst())
{
do{
count = cursor.getInt(0);
}
while (cursor.moveToNext());
}
return count;
}
}
这是我得到的错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {Food}:android.database.sqlite.SQLiteException:无此类列:UserUid(代码1 SQLITE_ERROR [1]):,同时编译:SELECT COUNT(*)FROM Comanda WHERE UserUid ='QM9amP6CX4g24wrq7Hu2'
答案 0 :(得分:0)
根据我的理解,错误消息指出您尚未将列添加到表中。我对Java不太熟悉,但是我会检查列是否已创建并在正确的表中。