How to update my string value into database

柔情痞子 提交于 2019-11-30 16:31:54

问题


Have a database with table name status and column name is column1. I need to update this.

I have a String value in Activity1

String check = "value";

I have passed this value into my DBHelper.I have tried like this

dbhelper = new DBHelper(this);
dbhelper.sample(check);

I got this value in DBHelper.here. Like this

public void sample(String prms){
Log.d("sucess",prms);
}

Now how i need to update String prms into my database columnname column1

I have tried like this

public  void sample( String prms) {
        Log.d("DBHELPER SUCCESS", prms);

        try{
            SQLiteDatabase db1 = this.getWritableDatabase(); 
            db1.execSQL("update appstatus SET status = '"+prms+"' WHERE id = 1 ");
        }catch(Exception e){
            System.out.println("GET SAMPLE VALUE"+e);
        }
    }

What's wrong with my syntax? How to achieve this?

It shows exception as

02-28 12:09:45.604: I/System.out(4975): GET SAMPLE VALUEandroid.database.sqlite.SQLiteException: table report already exists (code 1): , while compiling: create table report(level TEXT, topic TEXT,  start TEXT, end TEXT, date TEXT)

回答1:


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.)




回答2:


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.




回答3:


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




回答4:


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);
    }

}
      }



回答5:


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



来源:https://stackoverflow.com/questions/22066011/how-to-update-my-string-value-into-database

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