Sqlite没有更新行

时间:2013-12-03 11:07:46

标签: android sql sqlite transactions android-sqlite

您好我正在尝试更新一些在int字段中递增值的行,但它不起作用。 这是我的代码片段:

        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables("TICKET");
        String[] proj = new String[]{ "TICKET","NUMERO_ARTICOLO","QUANTITA","COMMESSA_IN","CODICE_SQUADRA"};
        try{
            Cursor cursor = queryBuilder.query(sqlDB, proj, selection, null, null, null, null);
            sqlDB.beginTransaction();
            cursor.moveToFirst();
            while (cursor.moveToNext()){
                String query =     "UPDATE MATERIALE "
                        + " SET RIMANENTE = RIMANENTE - " + cursor.getString(cursor.getColumnIndex("QUANTITA"))
                        + " WHERE NUMERO_ARTICOLO = '"+cursor.getString(cursor.getColumnIndex("NUMERO_ARTICOLO"))+"'"
                        +" AND COMMESSA = '"+cursor.getString(cursor.getColumnIndex("COMMESSA_IN"))+"' "
                        +" AND CODICE_SQUADRA = '"+cursor.getString(cursor.getColumnIndex("CODICE_SQUADRA"))+"' ";
                Log.d("update","Query: "+query);
                sqlDB.rawQuery(query, null);
            }
            rowsUpdated = sqlDB.update("TICKET", values, selection, null);
            sqlDB.setTransactionSuccessful();
            Log.d("update","Aggiornate "+rowsUpdated+" righe");
            sqlDB.endTransaction();
        }catch (Exception e) {
            e.printStackTrace();
        }

        break;
    default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(CONTENT_URI, null);
    return rowsUpdated;

上次查询正在运作

rowsUpdated = sqlDB.update("TICKET", values, selection, null);

但是while循环不起作用。 我做错了什么?也许更新SET RIMANENTE = RIMANENTE - " + cursor.getString(cursor.getColumnIndex("QUANTITA"))

我希望循环(while)和同一事务中的最后一次更新。

2 个答案:

答案 0 :(得分:2)

在插入更新或从数据库中选择行时,Sqlite使用基于文件的锁定。这意味着当您使用Select查询从数据库中提取记录时,直到cursorselect语句未关闭为止,您无法insert update或平均时间delete 因此,您应关闭cursor,然后使用execSQL代替rawQuery来解雇update

    try
    {
        Cursor cursor = queryBuilder.query(sqlDB, proj, selection, null, null, null, null);
        //sqlDB.beginTransaction();
        List<String> updateSql = new ArrayList<String>();
        try
        {
            cursor.moveToFirst();
            while (cursor.moveToNext())
            {
                updateSql.add(    "UPDATE MATERIALE "
                        + " SET RIMANENTE = RIMANENTE - " + cursor.getString(cursor.getColumnIndex("QUANTITA"))
                        + " WHERE NUMERO_ARTICOLO = '"+cursor.getString(cursor.getColumnIndex("NUMERO_ARTICOLO"))+"'"
                        +" AND COMMESSA = '"+cursor.getString(cursor.getColumnIndex("COMMESSA_IN"))+"' "
                        +" AND CODICE_SQUADRA = '"+cursor.getString(cursor.getColumnIndex("CODICE_SQUADRA"))+"' ");
                Log.d("update","Query: "+query);                    
            }
        } 
        finally
        {
            cursor.close();
        }
      try
      {
        sqlDB.beginTransaction();
        for (String query : updateSql)
        {
            sqlDB.execSQL(query, null); //Here it should be execSQL instead of rawQuery
        }
        rowsUpdated = sqlDB.update("TICKET", values, selection, null);

        sqlDB.setTransactionSuccessful();
        Log.d("update","Aggiornate "+rowsUpdated+" righe");
      }
      finally
      {
        sqlDB.endTransaction();
      }
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }

编辑如果我阅读documentation,则会明确提及

  

对于UPDATE语句,请改用以下任何一种语言    update(String,ContentValues,String,String [])
   updateWithOnConflict(String,ContentValues,String,String [],int)

答案 1 :(得分:1)

您最好使用SQL工具来完成此任务:

try {
    sqlDB.execSQL("UPDATE TICKET SET RIMANENTE = RIMANENTE - QUANTITA");
} catch (Exception e) {
    e.printStackTrace();
}

这将在单个声明中 - 因此,在单个事务中 - 在表QUANTITA的所有行中减去RIMANENTETICKET

编辑:

UPDATE TICKET SET RIMANENTE = RIMANENTE - QUANTITA WHERE QUANTITA IS NOT NULL

是首选,因为如果未定义QUANITTA,即NULL,则RIMANENTE将为空。 WHERE子句可以防止这种情况发生。