gpt4 book ai didi

android - 如何在android中的SQlite中删除整行

转载 作者:行者123 更新时间:2023-11-29 18:13:26 25 4
gpt4 key购买 nike

我创建了一个名为 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

我哪里做错了?

感谢您的帮助!

最佳答案

您必须创建包含 _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"
+ ");";

关于android - 如何在android中的SQlite中删除整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com