How to update my string value into database

前端 未结 5 1770
粉色の甜心
粉色の甜心 2021-01-03 18:59

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

I have a String value in Activity1 <

5条回答
  •  独厮守ぢ
    2021-01-03 19:25

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

提交回复
热议问题