2011-08-19 41 views
-1

我創建一個Android應用程序,在該應用程序我插入和刪除正常但我不能更新它.Am嘗試更新,但我得到一個錯誤。如何更新TextBox中Android中按鈕點擊事件的數據?

public Helper(Context ctx) 
{ 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    { 
     Log.w(TAG, "Upgrading database from version " + oldVersion 
       + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS titles"); 
     onCreate(db); 
    } 
}  

//---opens the database--- 
public Helper open() throws SQLException 
{ 
    db = DBHelper.getWritableDatabase(); 
    return this; 
} 

//---closes the database---  
public void close() 
{ 
    DBHelper.close(); 
} 

//---insert a title into the database--- 
public long insert(String name) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put("Name", name); 
    return db.insert(DATABASE_TABLE, null,initialValues); 
} 
//---updates a title--- 
public long update(String name) 
     { 
      ContentValues args = new ContentValues(); 
      args.put("Name",name); 

      return db.update(DATABASE_TABLE, args, 
          "Name" + "=" + name, null); 
     } 


//---deletes a particular title--- 
public int delete(String name) 
{ 
    // return db.delete(DATABASE_TABLE,"Name="+name, null); 
    return db.delete(DATABASE_TABLE,"Name=?",new String[]{String.valueOf(name)}); 
} 

}

我的活動文件

private static final String DATABASE_CREATE = 
    "create table titles (_id integer primary key autoincrement, " 
    + "Name text not null);"; 

private final Context context; 

private DatabaseHelper DBHelper; 
private SQLiteDatabase db; 

public Helper(Context ctx) 
{ 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    { 
     Log.w(TAG, "Upgrading database from version " + oldVersion 
       + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS titles"); 
     onCreate(db); 
    } 
}  

//---opens the database--- 
public Helper open() throws SQLException 
{ 
    db = DBHelper.getWritableDatabase(); 
    return this; 
} 

//---closes the database---  
public void close() 
{ 
    DBHelper.close(); 
} 

//---insert a title into the database--- 
public long insert(String name) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put("Name", name); 
    return db.insert(DATABASE_TABLE, null,initialValues); 
} 
//---updates a title--- 
public long update(String name) 
     { 
      ContentValues args = new ContentValues(); 
      args.put("Name",name); 

      return db.update(DATABASE_TABLE, args, 
          "Name" + "=" + name, null); 
     } 


//---deletes a particular title--- 
public int delete(String name) 
{ 
    // return db.delete(DATABASE_TABLE,"Name="+name, null); 
    return db.delete(DATABASE_TABLE,"Name=?",new String[]{String.valueOf(name)}); 
} 

}

+0

請問您發佈的錯誤,以便我們多一點?此外,您似乎在代碼段中粘貼了相同的代碼兩次。 –

回答

2

當數據庫查詢,如果字段是不是數字,那麼你必須使用 '' 封閉text.Try這個。

return db.update(DATABASE_TABLE, args, 
           "Name" + "='" + name+"'", null); 
+0

你好先生我改變了這種編碼,但我的數據沒有更新 – pkol

+0

oops.I see.How你可以更新名稱的名稱是不存在的。你必須更新名稱與id不與名稱。 – Rasel

+0

我使用兩個文本框更新名稱 – pkol