从SQLite数据库中检索数据并更新它

时间:2015-09-16 12:08:44

标签: android sqlite android-activity android-sqlite

我正在尝试检索一个表并稍后更新其中一个列。

我的大部分内容都是插入一些数据并将其检索回来,但我无法更新其中一个列。

DBhelper.class:

 String CREATE_USER_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + USER_ID + " INTEGER PRIMARY KEY   AUTOINCREMENT," + USER_NAME 
       + " TEXT,"
            + USER_PH_NO + " TEXT" + USER_DISPLAY_NAME + " TEXT" +  
            USER_DISPLAY_PICTURE + " TEXT" + ")";
    db.execSQL(CREATE_USER_CONTACTS_TABLE);


public void addUser(Database_Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(USER_NAME, contact.get_owner_name());
    values.put(USER_PH_NO, contact.get_owner_phone_number());

    // Inserting Row
    db.insert(TABLE_USER, null, values);
    db.close(); // Closing database connection
}
public List<Database_Contact> getAllDatabase_Users() {
    List<Database_Contact> userList = new ArrayList<Database_Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_USER;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Database_Contact contact = new Database_Contact();
            contact.set_user_id(Integer.parseInt(cursor.getString(0)));
            contact.set_owner_name(cursor.getString(1));
            contact.set_owner_phone_number(cursor.getString(2));
            // Adding contact to list
            userList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return userList;
}
public int setDisplayImage(long id,String path) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(USER_DISPLAY_PICTURE, path);
    // updating row
    return db.update(TABLE_USER, values, USER_ID +"="+id,null);
}

DatabaseContact.class:

public class Database_Contact {

//private variables


int _user_id;
String _display_name;
String _display_picture;
String _owner_phone_number;
String _owner_name;



public int get_user_id() {
    return _user_id;
}

public void set_user_id(int _user_id) {
    this._user_id = _user_id;
}




public String get_display_name() {
    return _display_name;
}

public void set_display_name(String _display_name) {
    this._display_name = _display_name;
}

public String get_display_picture() {
    return _display_picture;
}

public void set_display_picture(String _display_picture) {
    this._display_picture = _display_picture;
}

public String get_owner_phone_number() {
    return _owner_phone_number;
}

public void set_owner_phone_number(String _owner_phone_number) {
    this._owner_phone_number = _owner_phone_number;
}

public String get_owner_name() {
    return _owner_name;
}

public void set_owner_name(String _owner_name) {
    this._owner_name = _owner_name;
}

}

Mainactivity.class:

mydb = new DBHelper(this);
    List<Database_conytact> images = mydb.getAllDatabase_Users();
    mydb.setDisplayImage(images.get(0).get_user_id(), "String");

我收到以下错误:

  

android.database.sqlite.SQLiteException:near“Image”:语法错误   (代码1):,编译时:UPDATE用户SET显示图像=?哪里   USER_ID = '1'               在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native   方法)               在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891)               在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502)               在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)               在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)               在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)               在android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1574)               在android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1522)               在com.example.gnaveen.securityapp.DBHelper.setDisplayImage(DBHelper.java:296)               在com.example.gnaveen.securityapp.DisplayPicture $ 1.onClick(DisplayPicture.java:61)               在android.view.View.performClick(View.java:4789)               在android.view.View $ PerformClick.run(View.java:19881)               在android.os.Handler.handleCallback(Handler.java:739)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:135)               在android.app.ActivityThread.main(ActivityThread.java:5293)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

6 个答案:

答案 0 :(得分:0)

为什么不试试这个:

return db.update(TABLE_USER, values, USER_ID  + "='" + id +"'", null);

希望这适合你

答案 1 :(得分:0)

试试这个:

 String where="USER_ID =?";
 return db.update(TABLE_USER,values,where,new String[]{""+id});

答案 2 :(得分:0)

  1. 你实际上得到了什么错误?空指针异常?
  2. 从上面的代码我得到的是,每次执行代码时都会创建一个表而不是使用。

    String CREATE_USER_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " +     TABLE_USER + "("
        + USER_ID + " INTEGER PRIMARY KEY   AUTOINCREMENT," + USER_NAME +" TEXT,"
        + USER_PH_NO + " TEXT" + USER_DISPLAY_NAME + " TEXT" +  
        USER_DISPLAY_PICTURE + " TEXT" + ")";
    
  3. 仅当表格不存在时才会创建表格。

答案 3 :(得分:0)

您的表格创建错误

 String CREATE_USER_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER + "("
        + USER_ID + " INTEGER PRIMARY KEY   AUTOINCREMENT," + USER_NAME 
   + " TEXT,"
        + USER_PH_NO + " TEXT" + USER_DISPLAY_NAME + " TEXT" +  
        USER_DISPLAY_PICTURE + " TEXT" + ")";

你遗漏了一些逗号。试试这个

 String CREATE_USER_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER + " (" +
        USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + USER_NAME +
        " TEXT, " + USER_PH_NO + " TEXT, " + USER_DISPLAY_NAME + " TEXT, " +  
        USER_DISPLAY_PICTURE + " TEXT)";

您需要卸载并重新安装您的应用,才能重新创建表格

答案 4 :(得分:0)

SQL语法错误:stacktrace中的fieldname中有一个空白,对于sql来说是不行的。

 "SET Display Image=?"

答案 5 :(得分:0)

public Profile.Users readfor(String userName){

    SQLiteDatabase db = this.getWritableDatabase();

    String readSingleQuery = "SELECT * FROM "+ Profile.Users.T_NAME+" WHERE "+ Profile.Users.COL2_UNAME + " =  '"+ userName+"'";

    Cursor cursor = db.rawQuery(readSingleQuery,null);

    if(cursor.moveToFirst()){

        Profile.Users users = Profile.getProfile().getUser();

        users.setId(Integer.parseInt(cursor.getString(0)));
        users.setUsername(cursor.getString(1));
        users.setPassword(cursor.getString(2));

        return users;
    }

    return null;
}