gpt4 book ai didi

java - Android SQLite 数据库连接错误

转载 作者:行者123 更新时间:2023-12-01 14:13:05 25 4
gpt4 key购买 nike

我想将我的应用程序与 Android 手机上的数据库连接,因此我编写了一个名为 DBHelper 的类。

但是我收到一个错误,提示“MODE_PRIVATE”无法解析为变量。所以我做了“Context.MODE_PRIVATE”。现在,可以解析变量,但我收到一个新错误:

"The method openOrCreateDatabase(String, int, null) is undefinded for the Type DBHelper".

如果我使用DBHelper.this.openOrCreateDatabase打开它并没有帮助。

有人可以帮我吗?

这是我的代码:

public class DBHelper {

SQLiteDatabase db;

public void insert(String news, Context con){

db = openOrCreateDatabase("PlanB", con.MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS News");
db.execSQL("CREATE TABLE IF NOT EXISTS INBOX(id INTEGER,title VARCHAR,text VARCHAR,date VARCHAR);");

String[] divided = news.split("/newentry/");
int length = divided.length;
int pos = 0;

while(pos <= length){
String[] entry = divided[pos].split("/;/");
db.execSQL("INSERT INTO INBOX VALUES('"+entry[0]+"','"+entry[1]+"','"+entry[2]+"','"+entry[3]+"');");
pos++;
}

db.close();
}
}

最佳答案

看看SQLiteOpenHelper因为如果您在 Android 中使用 SQLite,您应该将其扩展到某个地方。

文档:

A helper class to manage database creation and version management.

You create a subclass implementing onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int) and optionallyonOpen(SQLiteDatabase), and this class takes care of opening thedatabase if it exists, creating it if it does not, and upgrading it asnecessary. Transactions are used to make sure the database is alwaysin a sensible state.

这是一个database helper您也许能从中找到一些用处。

关于java - Android SQLite 数据库连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18343838/

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