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

时间:2013-04-11 14:31:49

标签: android sqlite

Test.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();

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

1 个答案:

答案 0 :(得分:1)

纳豆,

我已经按照以下方式更改了代码,它对我有用。 请检查并告知我们。

                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循环是昂贵的,使用一段时间与&&如图所示。 2.也关闭c1和c2。