如何通过 SQL 命令从 SQLite android 中的 table 更新行?

How to update a row from a table in SQLite android by SQL command?

我的代码无法在 Button 单击更新操作。

 Button update_user= (Button) findViewById(R.id.update_user)
 update_user.setOnClickListener(new View.OnClickListener() {
@Override
            public void onClick(View view) {

                SignIn_Signup.sqLiteDatabase.execSQL("UPDATE mytable  SET name = "+name1_updated.getText() +"
                        WHERE name=name1.getText()+  );


            }
        });

请帮忙。

我认为正确的SQL说法是:

"UPDATE mytable SET name = " + name1_updated.getText() + " WHERE name = " + name1.getText()); 

您在 where 子句中缺少引号

SignIn_Signup.sqLiteDatabase.execSQL("UPDATE mytable  SET name = '"+name1_updated.getText() +"' WHERE name='"name1.getText()"'";

运行时(例如):

UPDATE mytable  SET name = 'psypher' WHERE name='neeraj';

另一种方法:

   ContentValues contentValues = new ContentValues();
    contentValues.put("name", "psyhper");
    getWritableDatabase().update("mytable", contentValues, "name" + "=?", new String[]{<put whatever name>});

如果你想在 Sqlite 中进行 delete 操作,你必须使用这些 query

 db.execSQL("delete from "+YOUR_TABLE_NAME+" where YOUR_COLUMN_NAME='"+VALUE_BY_WHICH_YOU_WANT_TO_DELETE+"'");

并为 Update 操作。使用以下 query

SignIn_Signup.sqLiteDatabase.execSQL("UPDATE mytable  SET name = '"+name1_updated.getText() +"' WHERE name='"name1.getText()"'";

尝试使用rawQuery方法

SignIn_Signup.sqLiteDatabase.rawQuery("UPDATE mytable  SET name = '"+name1_updated.getText() + "'" + "WHERE name= "+"'"name1.getText() +"'"  );

对于更新操作,请确保您在 table 中有一些适当的约束,例如 PRIMARY KEY。 "insertWithOnConflict" 方法将执行 insert/update 操作。

ContentValues contentValues = new ContentValues();
contentValues.put("name", "Dhruv");
mDatabase.insertWithOnConflict(DATABASE_TABLE_NAME, null, contentValues , SQLiteDatabase.CONFLICT_REPLACE);

我的代码终于可以运行了 那就是:

Button update_user = (Button) findViewById(R.id.update_user);
   update_user.setOnClickListener(new View.OnClickListener() {

        @Override`
        public void onClick(View view) {

            SignIn_Signup.sqLiteDatabase.execSQL("UPDATE mytable  SET name = '"+name1_updated.getText() +"' WHERE name='"+name1.getText()+"'");
        }
    });