How to update my string value into database

…衆ロ難τιáo~ 提交于 2019-11-30 17:07:24
laalto

rawQuery() doesn't actually run the SQL. Use execSQL() instead:

db.execSQL("update status SET column1 = '"+prms+"' ");

(See What is the correct way to do inserts/updates/deletes in Android SQLiteDatabase using a query string? to learn more how rawQuery() and execSQL() work under the hood.)

You can Directly Execute this Query like:

db.execSQL("update status SET column1 = '"+prms+"' ", null);

You should replace your code

Cursor cursor = db.execSQL("update status SET column1 = '"+prms+"' ", null);

With

db.execSQL("update status SET column1 = '"+prms+"' ", null);

void execSQL(String sql)

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert(String, String, ContentValues, update(String, ContentValues, String, String[]), et al, when possible.

db.execSQL("update status SET column1 = '"+prms+"' ", null);

Follow this code,it return no. of rows effected while updating.

SQLiteHelper helper;
public int updateName(String oldName,String newName)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(this.NAME, newName);
String [] whereArgs={oldName};

int count = db.update(helper.TABLE_NAME, values, helper.NAME+" =? ",whereArgs);
 return count;
 }

where helper is an object of class that extends SQLiteOpenHelper like the code below..

static class SQLiteHelper extends SQLiteOpenHelper
{
 ....////
 private static final String DATABASE_NAME = "databaseName";
 private static final String TABLE_NAME = "TABLENAME";
 private static final int DATABASE_VERSION = 1;
 private static final String UID = "_id";
 private static final String NAME = "Name";
 private static final String PASSWORD = "Password";
 private static final String CREATE_TABLE ="CREATE TABLE "+ TABLE_NAME +"(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " VARCHAR(255)," + PASSWORD + " VARCHAR(255));";

 public SQLiteHelper(Context context) 
 {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
    Message.Message(context, "Constructor Called");


}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub


    try {
        Message.Message(context, "OnCreate Called");
        db.execSQL(CREATE_TABLE);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Message.Message(context, "" + e);
    }

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

    try {
        Message.Message(context, "onUpgrade Called");
        db.execSQL(DROP_TABLE);
        onCreate(db);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Message.Message(context, ""+e);
    }

}
      }

Update your following line code,

Cursor cursor = db.rawQuery("update status SET column1 = '"+prms+"' ", null);

to this line

db.execSQL( "update status SET column1 = '"+prms+"' " );

So your method will look like this,

public void sample(String prms)
{ 
    Log.d("sucess",prms); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.execSQL("update status SET column1 = '"+prms+"'WHERE id = '5' " );   // remove second param from here.
} 

Caution : This query is going to update all the row of Column1; You should include where clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!