我创建了一个名为MSqliteHelper的类来帮助我在android项目中使用SQlite
public class MSqliteHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "userData"; //dataBaseName
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public MSqliteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase();
}
我创建了这样的表
@Override
public void onCreate(SQLiteDatabase db) {
String DATABASE_CREATE_TABLE =
"create table userData ("
+ "HTTPorS TEXT,"
+ "IPHostName TEXT,"
+ "port TEXT,"
+ "userName TEXT,"
+ "password TEXT,"
+ "saveOption TEXT"
+ ");";
db.execSQL(DATABASE_CREATE_TABLE);
}
我可以使用此方法添加整行数据
public long create(String HTTPorS, String IPHostName, String port, String userName, String password, String saveOption) {
ContentValues args = new ContentValues();
args.put("HTTPorS", HTTPorS);
args.put("IPHostName", IPHostName);
args.put("port", port);
args.put("userName", userName);
args.put("password", password);
args.put("saveOption", saveOption);
return db.insert("userData", null, args);
}
我有一个方法可以像这样删除整个行数据
public int delete(long rowId) {
return db.delete("userData",
"_ID=" + rowId,
null
);
}
但每次我调用此删除方法时,应用程序崩溃
错误的信息是这样的-----
E/AndroidRuntime(2976): android.database.sqlite.SQLiteException: no such column: _ID: , while compiling: DELETE FROM userData WHERE _ID=2
我哪里做错了?
感谢您的帮助!
答案 0 :(得分:3)
您必须在其中创建包含_id的表。例如
String DATABASE_CREATE_TABLE =
"create table userData ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "HTTPorS TEXT,"
+ "IPHostName TEXT,"
+ "port TEXT,"
+ "userName TEXT,"
+ "password TEXT,"
+ "saveOption TEXT"
+ ");";
答案 1 :(得分:0)
你也可以使用以下方法,
public static void delete_record ( SQLiteDatabase db, int id )
{
db.execSQL( "delete from " + TBL_NAME + " where id = " + id );
}
答案 2 :(得分:0)
myDataBase.delete(EVENTS,“event_id =”+ id,null);
myDatabase是数据库的实例, 事件是表的名称,event_id是字段。