更新所选列中的数据库android

时间:2013-11-22 06:28:56

标签: android android-sqlite android-preferences

我需要更新数据库中的选定列 这是我的db适配器类

public class DBAdapter {

private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "jobDiagnosis";
private static final int DATABASE_VERSION = 2;
Cursor cursor = null;
private static final String TABLE_DATA = "create table Job_Saved (" +
        "Id text not null," +
        "Title text not null," +
        "Location text not null," +
        "State text not null," +
        "Company text not null," +
        "Description text not null," +
        "Status text not null," +
        "Username text not null," +
        "Password text not null)";
private final Context context;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
private static DBAdapter instance;

private DBAdapter(Context c) {
    System.out.println("Constructor of DB Adapter...");
    this.context = c;
    System.out.println("Creating the object of db helper class..");
    try
    {
    dbHelper = new DatabaseHelper(context);
    dbHelper.getWritableDatabase();
    }
    catch (Exception e) {
        // TODO: handle exception
        Log.d("err", ""+e);
    }
}

public static DBAdapter getInstance(Context C) {
    if (null == instance) {
        instance = new DBAdapter(C);
    }
    return instance;
}

public void openReadableDatabase() throws SQLException {
    db = dbHelper.getReadableDatabase();
}

public void openWritableDatabase() throws SQLException {
    db = dbHelper.getWritableDatabase();

}


public void close() {
    dbHelper.close();   
}   
public long DeleteLocation()
{
    return db.delete("Job_Saved", null, null);
}
public long DeleteLocation(String id)
{
    return db.delete("Job_Saved", "id='" + id + "'", null);
}
public long updateLocation(String id,String str)
{
    return db.update("Job_saved", null, "Id='" + id + "'"+"& "+"Status='" + str + "'", null);
}
public long insertlocation(String Id,String Title,String Location,String State,String Company,String Description,String value, String Username,String Password) {
    //DatabaseHelper d=new DatabaseHelper(DBAdapter.this);
    ContentValues initialValues = new ContentValues();
    initialValues.put("Id", Id);
    initialValues.put("Title", Title);
    initialValues.put("Location", Location);
    initialValues.put("State", State);
    initialValues.put("Company",Company);
    initialValues.put("Description", Description);
    initialValues.put("Status", value);
    initialValues.put("Username", Username);
    initialValues.put("Password", Password);
    return db.insert("Job_Saved", null, initialValues);
}
public ArrayList<Data> getAllData(String username){
    ArrayList<Data> arr = new ArrayList<Data>();
    Cursor c = db.query("Job_Saved", null, "Username='" + username + "'", null, null, null, null);  
    if(c.getCount()>0){
        while(c.moveToNext()){
            Data f = new Data();
            f.setid(c.getString(0));
            f.setbusinessname(c.getString(1));
            f.setcityname(c.getString(2));
            f.setstatename(c.getString(3));
            f.setcompanyname(c.getString(4));
            f.setDesc(c.getString(5));
            f.setStatus(c.getString(6));
            f.setUser(c.getString(7));
            //f.setCharging(c.getString(8));
            arr.add(f);
        }

    }
    c.close();
    return arr;
}
public Boolean getStatus(String str){
    Boolean check_status=false;
    Cursor c = db.query("Job_Saved", null, "id='" + str + "'", null, null, null, null); 
    if(c.getCount()>0){
    check_status=true;
    }
    c.close();
    return check_status;
}
public boolean getStatus(String str,String username){
    Boolean check_status=false;
    Cursor c = db.query("Job_Saved", null, "Id='" + str + "'"+" & "+"Username='" + str + "'", null, null, null, null);  
    while(c.getCount()>0){
    check_status=true;
    }
    c.close();
    return check_status;
}
public void updateDownload(String id  , String status)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put("status", status);
    db.update("Likes", initialValues, "id=?", new String[] {id});
}
public String getIdStatus(String id){
    String a="";
    Cursor c =  db.query("Likes", null, "id='" + id + "'", null, null, null,
            null);      
    if(c.getCount()>0){
        while(c.moveToNext())
        {
         a=c.getString(1);
        }

    }
    return a;
}

我想更新if的状态基础,但我无法创建更新方法 我是android开发的新手

PLZ帮助我 PLz检查updateLocatin方法

我真的很抱歉我的英语不好。

提前谢谢

1 个答案:

答案 0 :(得分:1)

使用ContentValues提供更新方法的值。

public long updateLocation(String id,String str)
{
    ContentValues values = new ContentValues();
    values.put("Status", str);

    return db.update("Job_Saved", values, "Id='" + id + "'", null);
}

注意:最好在WHERE子句中使用参数化语句来防止SQL注入。

return db.update("Job_Saved", values, "Id=?", new String[]{ id });