Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在SQLite中使用特定ID更新数据_Android_Sqlite - Fatal编程技术网

Android 如何在SQLite中使用特定ID更新数据

Android 如何在SQLite中使用特定ID更新数据,android,sqlite,Android,Sqlite,我想在SQLite中更新一些数据。这是编辑数据的代码: db.open(); c = db.getData(); if (c.moveToFirst()) { do { Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid"))); Log.v("_______pos", "______UUID___________"+pos);

我想在SQLite中更新一些数据。这是编辑数据的代码:

db.open();
c = db.getData();
if (c.moveToFirst()) {
    do {
        Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid")));
        Log.v("_______pos", "______UUID___________"+pos);
        String strSQL = "UPDATE DeviceDetails SET devicename ="+ edittext.getText().toString() +"  WHERE uuid = "+c.getString(c.getColumnIndex("uuid")) ;

        db.select(strSQL);
        Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid")));
        Log.v("_______BACK PRESSED", "______devicename___________"+c.getString(c.getColumnIndex("devicename")));

        Log.v("___________text", "_______________"+c.getString(c.getColumnIndex("devicename")));
        Log.v("___________edittext", "_______________"+edittext.getText().toString());
        Log.v("_____ADDRESS______edittext", "_______________"+pos);
        Intent intent=new Intent();
        intent.putExtra("Dname", edittext.getText().toString());
        Log.v("_____edittext in intent________", "__________"+edittext.getText().toString());
        intent.putExtra("Daddress",pos);
        Log.v("_____edittext in intent________", "__________"+pos);
        setResult(RESULT_OK, intent);
        finish();
    } while (c.moveToNext());
}

db.close();
在DatabaseAdapter中,我执行了以下查询操作。但它并没有更新数据

public Cursor select(String query) throws SQLException {
    return db.rawQuery(query, null);
}
  • 您的字符串文本没有正确地为SQL引用
    '
    。但是,最好的选择是使用
    占位符和绑定参数

  • 使用
    execSQL()
    而不是
    rawQuery()
    进行更新
    rawQuery()
    无法单独运行SQL<代码>execSQL()将

  • 例如:

    db.execSQL("UPDATE DeviceDetails SET devicename = ? WHERE uuid = ?",
        new String[] {
            edittext.getText().toString(),
            c.getString(c.getColumnIndex("uuid"))
        });  
    

    你所说的重命名是什么意思?它的简单更新

    更新查询将在您执行此操作时执行此操作

    用这个更新你的查询

    String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"'  WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
    

    您应该使用
    .update
    更新数据库中的行,而不是
    .rawQuery

    String value = c.getString(c.getColumnIndex("uuid"));
    String[] whereVars = new String[]{value};
    String where = "uuid = ?"
    
    ContentValues args = new ContentValues();
    args.put("devicename", edittext.getText().toString());
    
    db.update("DeviceDetails", args, where, whereVars);
    
    我还将让您知道,上述代码的性能,循环中的更新,将是垃圾。。因此,您应该在单个事务中执行所有更新:

    db.beginTransaction();
    try {
        if (c.moveToFirst()) {
            do {
                updateDeviceByUuid(c.getString(c.getColumnIndex("uuid"),  
                                   edittext.getText().toString());
            } while (c.moveToNext());
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    } 
    


    用“single quote”标记将字符串括起来,例如引用:,您好,我在执行此操作时遇到此错误。03-21 11:43:17.198:E/MessageQueue JNI(5836):android.database.sqlite.SQLiteException:near:1A:语法错误(代码1):,编译时:UPDATE DeviceDetails SET devicename='gxyxyzuc'其中uuid=00:1A:5B:00:15:1003-21 11:43:17.198:E/MessageQueue JNI(5836):android.database.sqlite.SQLiteException:near:1A”:语法错误(代码1):,在编译:UPDATE DeviceDetails SET devicename='gxyxyzuc'时,其中uuid=00:1A:5B:00:15:10我想您错过了uuid的。@user254633您在uuid中也缺少了”,只需从我的答案中看到更新后的查询,它执行了,但没有重命名为oldname whyhi,在adapter how to add execSQL()中与那里的
    rawQuery()
    相同,例如,将字符串参数作为参数传递。如图所示,只有public void select(字符串查询)抛出SQLException{db.execSQL(query,null);}我还有其他问题,请帮助我。我会发送完整的项目,所以请帮助我。在这些问题上。如何发送完整的项目。
    public int updateDeviceByUuid(String uuid, String deviceName){
        String[] whereVars = new String[]{uuid};
        String where = "uuid = ?"
    
        ContentValues args = new ContentValues();
        args.put("devicename", deviceName);
    
        return db.update("DeviceDetails", args, where, whereVars);
    }