我創建一個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)});
}
}
請問您發佈的錯誤,以便我們多一點?此外,您似乎在代碼段中粘貼了相同的代碼兩次。 –