我创建一个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)
在数据库中查询时,如果字段不是数字,那么你必须使用''来封闭文本。试试这个。
return db.update(DATABASE_TABLE, args,
"Name" + "='" + name+"'", null);