首页 文章

Android中的SQLite如何更新特定行

提问于
浏览
118

我一直在尝试更新特定行一段时间,似乎有两种方法可以做到这一点 . 根据我的阅读和尝试,你可以使用:

execSQL(String sql) 方法

或者:

update(String table, ContentValues values, String whereClause, String[] whereArgs) 方法 .

(让我知道这是不正确的,因为我是Android新手,对SQL很新 . )

那么让我来看看我的实际代码 .

myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null);

我想完成这个:

更新Field1,Field2和Field3,其中主键(_id)等于1 .

Eclipse在“更新”一词的下方给了我一条红线,给了我这样的解释:

SQLiteDatabase类型中的方法update(String,ContentValues,String,String [])不适用于参数(String,String,String,null)

我猜我没有正确分配ContentValues . 谁能指出我正确的方向?

16 回答

  • 1
    • 为了方便起见,我个人更喜欢.update . 但execsql也会一样 .

    • 您的猜测是正确的,问题是您的内容 Value . 您应该创建一个ContentValue对象并将数据库行的值放在那里 .

    此代码应修复您的示例:

    ContentValues data=new ContentValues();
     data.put("Field1","bob");
     data.put("Field2",19);
     data.put("Field3","male");
     DB.update(Tablename, data, "_id=" + id, null);
    
  • 32

    如果您的sqlite行具有唯一ID或其他等效项,则可以使用where子句,如下所示

    update .... where id = {here is your unique row id}
    
  • 4

    对于更新,需要调用setTransactionSuccessfull来进行更改以获得提交,如下所示:

    db.beginTransaction();
    try {
        db.update(...) 
        db.setTransactionSuccessfull(); // changes get rolled back if this not called
    } finally {
       db.endTransaction(); // commit or rollback
    }
    
  • 0

    首先制作一个ContentValues对象:

    ContentValues cv = new ContentValues();
    cv.put("Field1","Bob"); //These Fields should be your String values of actual column names
    cv.put("Field2","19");
    cv.put("Field2","Male");
    

    然后使用更新方法,它现在应该工作:

    myDB.update(TableName, cv, "_id="+id, null);
    
  • 2

    试试这种方式

    String strFilter = "_id=" + Id;
      ContentValues args = new ContentValues();
      args.put(KEY_TITLE, title);
      myDB.update("titles", args, strFilter, null);**
    
  • 8

    希望这会对你有所帮助:

    public boolean updatedetails(long rowId, String address)
      {
         SQLiteDatabase mDb= this.getWritableDatabase();
       ContentValues args = new ContentValues();
       args.put(KEY_ROWID, rowId);          
       args.put(KEY_ADDRESS, address);
      return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)>0;   
     }
    
  • 23

    在你的DB中使用这个代码

    public boolean updatedetails(long rowId,String name, String address)
          {
           ContentValues args = new ContentValues();
           args.put(KEY_ROWID, rowId);          
           args.put(KEY_NAME, name);
           args.put(KEY_ADDRESS, address);
           int i =  mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null);
        return i > 0;
         }
    

    要在sample.java中更新,请使用此代码

    //DB.open();
    
            try{
                  //capture the data from UI
                  String name = ((EditText)findViewById(R.id.name)).getText().toString().trim();
                  String address =(EditText)findViewById(R.id.address)).getText().toString().trim();
    
                  //open Db
                  pdb.open();
    
                  //Save into DBS
                  pdb.updatedetails(RowId, name, address);
                  Toast.makeText(this, "Modified Successfully", Toast.LENGTH_SHORT).show();
                  pdb.close();
                  startActivity(new Intent(this, sample.class));
                  finish();
            }catch (Exception e) {
                Log.e(TAG_AVV, "errorrrrr !!");
                e.printStackTrace();
            }
        pdb.close();
    
  • 2

    您在SQLite中尝试这一个更新方法

    int id;
    ContentValues con = new ContentValues();
    con.put(TITLE, title);
    con.put(AREA, area);
    con.put(DESCR, desc);
    con.put(TAG, tag);
    myDataBase.update(TABLE, con, KEY_ID + "=" + id,null);
    
  • 0

    //这是一些简单的更新示例代码

    //首先声明这个

    private DatabaseAppHelper dbhelper;
    private SQLiteDatabase db;
    

    //初始化以下内容

    dbhelper=new DatabaseAppHelper(this);
            db=dbhelper.getWritableDatabase();
    

    //更新代码

    ContentValues values= new ContentValues();
                    values.put(DatabaseAppHelper.KEY_PEDNAME, ped_name);
                    values.put(DatabaseAppHelper.KEY_PEDPHONE, ped_phone);
                    values.put(DatabaseAppHelper.KEY_PEDLOCATION, ped_location);
                    values.put(DatabaseAppHelper.KEY_PEDEMAIL, ped_emailid);
                    db.update(DatabaseAppHelper.TABLE_NAME, values,  DatabaseAppHelper.KEY_ID + "=" + ?, null);
    

    // put ur id而不是'question mark'是我共享偏好中的一个函数 .

  • 44

    可以尝试这样:

    ContentValues values=new ContentValues();
    values.put("name","aaa");
    values.put("publisher","ppp");
    values.put("price","111");
    
    int id=sqdb.update("table_name",values,"bookid='5' and booktype='comic'",null);
    
  • 1
    public void updateRecord(ContactModel contact) {
        database = this.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_FIRST_NAME, contact.getFirstName());
        contentValues.put(COLUMN_LAST_NAME, contact.getLastName());
        contentValues.put(COLUMN_NUMBER,contact.getNumber());
        contentValues.put(COLUMN_BALANCE,contact.getBalance());
        database.update(TABLE_NAME, contentValues, COLUMN_ID + " = ?", new String[]{contact.getID()});
        database.close();
    }
    
  • 258
    public long fillDataTempo(String table){
        String[] table = new String[1];
        tabela[0] = table; 
        ContentValues args = new ContentValues();
        args.put(DBOpenHelper.DATA_HORA, new Date().toString());
        args.put(DBOpenHelper.NOME_TABELA, nome_tabela);
        return db.update(DATABASE_TABLE, args, STRING + " LIKE ?" ,tabela);
    }
    
  • 2

    简单方法:

    String strSQL = "UPDATE myTable SET Column1 = someValue WHERE columnId = "+ someValue;
    
    myDataBase.execSQL(strSQL);
    
  • 2

    你可以试试这个......

    db.execSQL("UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=6 ");
    
  • 4

    SQLite中的更新方法:

    public void updateMethod(String name, String updatename){
        String query="update students set email = ? where name = ?";
        String[] selections={updatename, name};
        Cursor cursor=db.rawQuery(query, selections);
    }
    
  • 5

    首先创建一个ContentValues对象:

    ContentValues cv = new ContentValues();
    cv.put("Field1","Bob");
    cv.put("Field2","19");
    

    然后使用更新方法 . 注意,第三个参数是where子句 . “?”是一个占位符 . 它将被替换为第四个参数(id)

    myDB.update(MY_TABLE_NAME, cv, "_id = ?", new String[]{id});
    

    这是更新特定行的清除解决方案 .

相关问题