gpt4 book ai didi

android - close() 从未在数据库上显式调用,android 数据库未关闭

转载 作者:行者123 更新时间:2023-11-30 03:39:50 24 4
gpt4 key购买 nike

测试.java:

 SQLiteDatabase db=openOrCreateDatabase("data",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS location(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME VARCHAR(100),CBcode VARCHAR(100));");
Cursor c1 = db.rawQuery("Select NAME from location", null);
Cursor c2 = db.rawQuery("Select CBcode from location", null);
if(c1==null || c2 == null)
{
Toast.makeText(getBaseContext(), "Database values empty, please refer Help option befor you start using the app..", Toast.LENGTH_LONG).show();
}
else
{
c1.moveToFirst();
c2.moveToFirst();
int col1=c1.getColumnIndex("NAME");
int col2=c2.getColumnIndex("CBcode");
for(c1.moveToFirst(); c1.moveToLast(); c1.moveToNext())
{
for(c2.moveToFirst(); c2.moveToLast(); c2.moveToNext())
{
if(loc_name == c1.getString(col1))
{
my_cb = c2.getString(col2);
c2.close();
c1.close();
Toast.makeText(getBaseContext(), "alarm has been set", Toast.LENGTH_LONG).show();
break;
}
else
{
// Toast.makeText(getBaseContext(), "No such values in database, please read Help carefully before using lovisis!", Toast.LENGTH_LONG).show();
}
}
}
}
db.close();

在我上面的代码中,我试图访问数据库并在字符串匹配时启动一个新的 Activity ,但我正在关闭,但从未在数据库错误上显式调用。请帮忙

最佳答案

纳 bean ,

我已经按如下方式更改了您的代码,它对我有用。请检查并告诉我。

                SQLiteDatabase db=openOrCreateDatabase("data",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS location(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME VARCHAR(100),CBcode VARCHAR(100));");
Cursor c1 = db.rawQuery("Select NAME from location", null);
Cursor c2 = db.rawQuery("Select CBcode from location", null);
if(c1==null || c2 == null)
{
Toast.makeText(getBaseContext(), "Database values empty, please refer Help option befor you start using the app..", Toast.LENGTH_LONG).show();
}
else
{
c1.moveToFirst();
c2.moveToFirst();

int col1=c1.getColumnIndex("NAME");
int col2=c2.getColumnIndex("CBcode");

while(c1.moveToNext() && c2.moveToNext())
{
if(loc_name.equals(c1.getString(col1)))
{
my_cb = c2.getString(col2);
Toast.makeText(getBaseContext(), "alarm has been set", Toast.LENGTH_LONG).show();
}
else
{
// Toast.makeText(getBaseContext(), "No such values in database, please read Help carefully before using lovisis!", Toast.LENGTH_LONG).show();
}
}
}
c2.close();
c1.close();
db.close();

你的错误是,1. 两个 for 循环是昂贵的,如图所示使用 while 和 &&。2. 同时关闭c1和c2。

关于android - close() 从未在数据库上显式调用,android 数据库未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15951487/

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