如何将我的字符串值更新到数据库中

时间:2014-02-27 10:41:14

标签: android sqlite sqliteopenhelper

拥有一个表名为status且列名为column1的数据库。我需要更新它。

我在Activity1

中有一个字符串值
String check = "value";

我已将此值传递给我的DBHelper。我试过这样的

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

我在DBHelpe r中得到了这个值。喜欢这个

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

现在我需要如何将String prms更新为我的数据库列名column1

我试过这样的

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

我的语法有什么问题?怎么做到这一点?

它显示异常为

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)

5 个答案:

答案 0 :(得分:2)

rawQuery()实际上并不运行SQL。请改用execSQL()

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

(请参阅What is the correct way to do inserts/updates/deletes in Android SQLiteDatabase using a query string?了解rawQuery()execSQL()如何在幕后工作的更多信息。)

答案 1 :(得分:2)

您可以直接Execute此查询,如:

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

您应该替换您的代码

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

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

void execSQL(String sql)

执行单个 SQL 语句,该语句不是SELECT或任何其他返回数据的 SQL 语句。

无法返回任何数据(例如受影响的行数)。相反,我们鼓励您尽可能使用 insert(String,String,ContentValues update(String,ContentValues,String,String [])等。< / p>

答案 2 :(得分:1)

db.execSQL(“更新状态SET column1 ='”+ prms +“'”,null);

答案 3 :(得分:1)

更新以下行代码,

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

到这一行

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

所以你的方法看起来像这样,

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

警告:此查询将更新Column1的所有行;您应该包含where子句

答案 4 :(得分:1)

按照此代码,它返回no。更新时受影响的行数。

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

其中,helper是类的对象,扩展SQLiteOpenHelper,如下面的代码所示。

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

}
      }