Android错误:从未在数据库上显式调用close()

时间:2011-12-07 21:13:54

标签: java android sqlite

不确定是什么问题, 当我尝试添加更多表时,我让数据库在一个表上运行,并且表示已经设置了新表。

活动会发送一个枚举,告诉它要查看哪种类型的位置,即要处理的表格

这是处理sql的类

package com.android.TestApp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class Location extends Activity{

public static final String KEY_SHOPROWID = "_id";
public static final String KEY_SHOPNAME = "shop_name";
public static final String KEY_SHOPADDRESS = "shop_address";
public static final String KEY_SHOPDESCRIPTION = "shop_description";
public static final String KEY_SHOPPOST = "shop_post";
public static final String KEY_SHOPRATE = "shop_rate";
public boolean ISNOT = false;

private static final String DATABASE_NAME = "NewLocationDataBase";
private static final String DATABASE_SHOPTABLE = "shop_table";
private static final String DATABASE_CAFETABLE = "cafe_table";
private static final String DATABASE_RESTURANTTABLE ="resturant_table";
private static final String DATABASE_HOTELTABLE = "hotel_table";

private static final int DATABASE_VERSION = 1;

private DbHelper theHelper;
private final Context theContext;
private SQLiteDatabase theDataBase;

public class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_SHOPTABLE + " ("+
            KEY_SHOPROWID + " INTEGER PRIMARY KEY  AUTOINCREMENT, " +
            KEY_SHOPNAME + " TEXT NOT NULL, " +
            KEY_SHOPADDRESS + " TEXT NOT NULL, " +
            KEY_SHOPDESCRIPTION + " TEXT NOT NULL, " +
            KEY_SHOPPOST + " TEXT NOT NULL, " +
            KEY_SHOPRATE + " INTEGER);"
            );

        db.execSQL("CREATE TABLE " + DATABASE_CAFETABLE + " (" +
                KEY_SHOPROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_SHOPNAME + " TEXT NOT NULL, " +
                KEY_SHOPADDRESS + " TEXT NOT NULL, " +
                KEY_SHOPDESCRIPTION + " TEXT NOT NULL, " +
                KEY_SHOPPOST + " TEXT NOT NULL, " +
                KEY_SHOPRATE + " INTEGER);"
                );

        db.execSQL("CREATE TABLE " + DATABASE_RESTURANTTABLE + " (" +
                KEY_SHOPROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_SHOPNAME + " TEXT NOT NULL, " +
                KEY_SHOPADDRESS + " TEXT NOT NULL, " +
                KEY_SHOPDESCRIPTION + " TEXT NOT NULL, " +
                KEY_SHOPPOST + " TEXT NOT NULL, " +
                KEY_SHOPRATE + " INTEGER);"
                );

        db.execSQL("CREATE TABLE " + DATABASE_HOTELTABLE + " (" +
                KEY_SHOPROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_SHOPNAME + " TEXT NOT NULL, " +
                KEY_SHOPADDRESS + " TEXT NOT NULL, " +
                KEY_SHOPDESCRIPTION + " TEXT NOT NULL, " +
                KEY_SHOPPOST + " TEXT NOT NULL, " +
                KEY_SHOPRATE + " INTEGER);"
                );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_SHOPTABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CAFETABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_RESTURANTTABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_HOTELTABLE);
        onCreate(db);
    }

}

public Location(Context c){
    theContext = c;
}

public void createEntry(LocationType type, String name, String address, String description,
        String post, int rate) {
    // TODO Auto-generated method stub

    ContentValues cv = new ContentValues();
    cv.put(KEY_SHOPNAME, name);
    cv.put(KEY_SHOPADDRESS, address);
    cv.put(KEY_SHOPDESCRIPTION, description);
    cv.put(KEY_SHOPPOST, post);
    cv.put(KEY_SHOPRATE, rate);
    switch(type){
    case shop:
        theDataBase.insert(DATABASE_SHOPTABLE, null, cv);
    }
}

public Location writeOpen() throws SQLException{
    theHelper = new DbHelper(theContext);
    theDataBase = theHelper.getWritableDatabase();
    return this;
}

public Location readOpen(){
    theHelper = new DbHelper(theContext);
    theDataBase = theHelper.getReadableDatabase();
    return this;
}

public void writeClose(){
    if(theHelper!=null){
        theHelper.close();
    }
}
public void readClose(){
    if(theHelper!=null){
        theHelper.close();
    }
}


public boolean isEmpty(LocationType type){
    String [] data = {KEY_SHOPNAME};
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    int size = 0;
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
    }
    size = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        size = size+1;
    }
    if(size ==0){
        return true;
    }else{
        return false;
    }
}

public String [] getNames(LocationType type){
    String [] data = {KEY_SHOPNAME};
    int size = 0;
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    }

    String [] Names = new String [size];
    String name = "";
    int nameIndex = c.getColumnIndex(KEY_SHOPNAME);
    int index = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        name = c.getString(nameIndex);
        Names[index] = name;
        index = index+1;
    }
    return Names;
}

public String [] getDescription(LocationType type){
    String [] data = {KEY_SHOPDESCRIPTION};
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    int size = 0;
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        size = size+1;
        }
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    }

    String [] Descriptions = new String [size];
    String desc = "";
    int descIndex = c.getColumnIndex(KEY_SHOPDESCRIPTION);
    int index = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        desc = c.getString(descIndex);
        Descriptions[index] = desc;
        index = index+1;
    }
    return Descriptions;
}

public String [] getAddress(LocationType type){
    String [] data = {KEY_SHOPADDRESS};
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    int size = 0;
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    }

    String [] Address = new String [size];
    String address = "";
    int addressIndex = c.getColumnIndex(KEY_SHOPADDRESS);
    int index = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        address = c.getString(addressIndex);
        Address[index] = address;
        index = index+1;
    }
    return Address;
}

public String [] getPost(LocationType type){

    String [] data = {KEY_SHOPPOST};
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    int size = 0;
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    }

    String [] Post = new String [size];
    String post = "";
    int addressIndex = c.getColumnIndex(KEY_SHOPPOST);
    int index = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        post = c.getString(addressIndex);
        Post[index] = post;
        index = index+1;
    }
    return Post;
}


public String [] getRow(LocationType type){

    String [] data = {KEY_SHOPROWID};
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    int size = 0;
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    }

    String [] Post = new String [size];
    String post = "";
    int addressIndex = c.getColumnIndex(KEY_SHOPROWID);
    int index = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        post = c.getString(addressIndex);
        Post[index] = post;
        index = index+1;
    }
    return Post;
}

public int [] getRate(LocationType type){
    String [] data = {KEY_SHOPRATE};
    Cursor c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    int size = 0;
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
        size = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            size = size+1;
        }
    }
    int [] rate = new int [size];
    int Rate = 0;
    int addressIndex = c.getColumnIndex(KEY_SHOPRATE);
    int index = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        Rate = c.getInt(addressIndex);
        rate[index] = Rate;
        index = index+1;
    }
    return rate;
}

public void updateEntry(String index, LocationType type, String nameNew,
        String addressNew, String descriptionNew, String postNew) {
    // TODO Auto-generated method stub
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_SHOPNAME, nameNew);
    cvUpdate.put(KEY_SHOPADDRESS, addressNew);
    cvUpdate.put(KEY_SHOPDESCRIPTION, descriptionNew);
    cvUpdate.put(KEY_SHOPPOST, postNew);
    switch(type){
    case shop:
        theDataBase.update(DATABASE_SHOPTABLE, cvUpdate, KEY_SHOPROWID + "=" + index, null);
    case cafe:
        theDataBase.update(DATABASE_CAFETABLE, cvUpdate, KEY_SHOPROWID + "=" + index, null);
    case resturant:
        theDataBase.update(DATABASE_RESTURANTTABLE, cvUpdate, KEY_SHOPROWID + "=" + index, null);
    }


}

public int getSize(LocationType type) {
    // TODO Auto-generated method stub
    String [] data = {KEY_SHOPPOST};
    int size = 0;
    Cursor c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
    switch(type){
    case shop:
        c = theDataBase.query(DATABASE_SHOPTABLE, data, null, null, null, null, null);
    case cafe:
        c = theDataBase.query(DATABASE_CAFETABLE, data, null, null, null, null, null);
    case resturant:
        c = theDataBase.query(DATABASE_RESTURANTTABLE, data, null, null, null, null, null);
    }
    size = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        size = size+1;
    }

    return size;
}

public void removeEntry(String rowId, LocationType type) {
    // TODO Auto-generated method stub
    switch(type){
    case shop:
        theDataBase.delete(DATABASE_SHOPTABLE, KEY_SHOPROWID + "=" + rowId, null);

    }

}

public void setRating(String row, LocationType type, int newRating) {
    // TODO Auto-generated method stub
    ContentValues cvnewRate = new ContentValues();
    cvnewRate.put(KEY_SHOPRATE,newRating );
    switch(type){
    case shop:
        theDataBase.update(DATABASE_SHOPTABLE, cvnewRate, KEY_SHOPROWID + "=" + row, null);
    }


}

这是活动

package com.android.TestApp;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

 public class ShowActivity extends ListActivity {

protected LocationType Type = LocationType.shop;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);



  try{


      Location test = new Location(this);   
      if(test.isEmpty(Type)){
          switch(Type){
          case shop:
              String [] Names = getResources().getStringArray(R.array.shopName);
              String [] Description = getResources().getStringArray(R.array.shop_descrt);
              String [] Address =  getResources().getStringArray(R.array.shop_address);
              String [] Post =  getResources().getStringArray(R.array.shop_post);
              int [] Rate = getResources().getIntArray(R.array.shop_rate);

              for(int i = 0; Names.length >i; i++){
                Location starting = new Location(this);
                starting.writeOpen();
                starting.createEntry(Type, Names[i], Address[i], Description[i], Post[i], Rate[i]);
                starting.writeClose();
              }

          case cafe:
              String [] cafeNames = getResources().getStringArray(R.array.cafeName);
              String [] cafeDescription = getResources().getStringArray(R.array.cafe_descrt);
              String [] cafeAddress =  getResources().getStringArray(R.array.cafe_address);
              String [] cafePost =  getResources().getStringArray(R.array.cafe_post);
              int [] cafeRate = getResources().getIntArray(R.array.cafe_rate);

              for(int i = 0; cafeNames.length >i; i++){
                Location starting = new Location(this);
                starting.writeOpen();
                starting.createEntry(Type, cafeNames[i], cafeAddress[i], cafeDescription[i], cafePost[i], cafeRate[i]);
                starting.writeClose();
              }
          case resturant:
              String [] resturantNames = getResources().getStringArray(R.array.resturantName);
              String [] resturantDescription = getResources().getStringArray(R.array.resturant_desct);
              String [] resturantAddress =  getResources().getStringArray(R.array.resturant_address);
              String [] resturantPost =  getResources().getStringArray(R.array.resturant_post);
              int [] resturantRate = getResources().getIntArray(R.array.resturant_rate);

              for(int i = 0; resturantNames.length >i; i++){
                Location starting = new Location(this);
                starting.writeOpen();
                starting.createEntry(Type, resturantNames[i], resturantAddress[i], resturantDescription[i], resturantPost[i], resturantRate[i]);
                starting.writeClose();
              }

          case hotel:
              String [] hotelNames = getResources().getStringArray(R.array.shopName);
              String [] hotelDescription = getResources().getStringArray(R.array.shop_descrt);
              String [] hotelAddress =  getResources().getStringArray(R.array.shop_address);
              String [] hotelPost =  getResources().getStringArray(R.array.shop_post);
              int [] hotelRate = {2,3,3,5,1};

              for(int i = 0; hotelNames.length >i; i++){
                Location starting = new Location(this);
                starting.writeOpen();
                starting.createEntry(Type, hotelNames[i], hotelAddress[i], hotelDescription[i], hotelPost[i], hotelRate[i]);
                starting.writeClose();
              }
          }

      }
  }catch(Exception e){
        Dialog d = new Dialog(this);
        String error = e.toString();
        d.setTitle("the Database is not set up sorry set up");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
  }


  try{

          Location Table = new Location(this);
          Table.readOpen();
          String [] name= Table.getNames(Type);
          Table.readClose();

      ListView ln = getListView();  
      setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, name));

      ln.setTextFilterEnabled(true);

      ln.setOnItemClickListener((new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

                  int index = (int) (id);

                  Intent intent = new Intent(view.getContext(), Layout.class);
                  intent.putExtra("index", index);
                  intent.putExtra("type", Type);
                  startActivityForResult(intent,0);
                  }

      }));

  }catch(Exception e){
        Dialog d = new Dialog(this);
        String error = e.toString();
        d.setTitle("could not display info");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
  }
}

}

编辑1: 意识到我没有在开始测试时调用writeClose()并且我无法使用listActivity调用onDestroy,所以我创建了一个缓冲活动来执行检查test.isEmpty(),现在再次适用于商店的情况,

认为oncreate中的位置代码或使用此

存在问题
        Location test = new Location(this);
  try{

      test.writeOpen();
      if(test.isEmpty(Type)){
          switch(Type){
          case 1:
              String [] Names = getResources().getStringArray(R.array.shopName);
              String [] Description = getResources().getStringArray(R.array.shop_descrt);
              String [] Address =  getResources().getStringArray(R.array.shop_address);
              String [] Post =  getResources().getStringArray(R.array.shop_post);
              int [] Rate = getResources().getIntArray(R.array.shop_rate);

              for(int i = 0; Names.length >i; i++){
                Location starting = new Location(this);
                starting.writeOpen();
                starting.createEntry(Type, Names[i], Address[i], Description[i], Post[i], Rate[i]);
                starting.writeClose();
              }

         // case 2:
            //  String [] cafeNames =          getResources().getStringArray(R.array.cafeName);
     //               String [] cafeDescription = getResources().getStringArray(R.array.cafe_descrt);
    //                String [] cafeAddress =  getResources().getStringArray(R.array.cafe_address);
    //                String [] cafePost =  getResources().getStringArray(R.array.cafe_post);
    //                int [] cafeRate = getResources().getIntArray(R.array.cafe_rate);
    //                
    //                for(int i = 0; cafeNames.length >i; i++){
    //                  Location starting = new  Location(this);
    //                  starting.writeOpen();
    //                  starting.createEntry(Type, cafeNames[i], cafeAddress[i], cafeDescription[i], cafePost[i], cafeRate[i]);
    //                  starting.writeClose();
    //                }
     //           case 3:
    //                String [] resturantNames =   getResources().getStringArray(R.array.resturantName);
    //                String [] resturantDescription = getResources().getStringArray(R.array.resturant_desct);
    //                String [] resturantAddress =   getResources().getStringArray(R.array.resturant_address);
    //                String [] resturantPost =   getResources().getStringArray(R.array.resturant_post);
    //                int [] resturantRate =  getResources().getIntArray(R.array.resturant_rate);
    //                
    //                for(int i = 0; resturantNames.length >i;   i++){
    //                  Location starting = new Location(this);
    //                  starting.writeOpen();
    //                  starting.createEntry(Type, resturantNames[i], resturantAddress[i], resturantDescription[i], resturantPost[i], resturantRate[i]);
    //                  starting.writeClose();
    //                }

         // case hotel:
            //  String [] hotelNames = getResources().getStringArray(R.array.shopName);
            //  String [] hotelDescription = getResources().getStringArray(R.array.shop_descrt);
             // String [] hotelAddress =  getResources().getStringArray(R.array.shop_address);
            //  String [] hotelPost =  getResources().getStringArray(R.array.shop_post);
            //  int [] hotelRate = {2,3,3,5,1};

            //  for(int i = 0; hotelNames.length >i; i++){
            //  Location starting = new Location(this);
            //  starting.writeOpen();
            //  starting.createEntry(Type, hotelNames[i], hotelAddress[i], hotelDescription[i], hotelPost[i], hotelRate[i]);
            //  starting.writeClose();
             // }
          }

      }
      test.writeClose();
  }catch(Exception e){
        Dialog d = new Dialog(this);
        String error = e.toString();
        d.setTitle("the Database is not set up sorry set up");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
  }
  test.writeClose();

`

2 个答案:

答案 0 :(得分:0)

使用db adapter实例后,需要显式调用close方法。

theHelper.close();

这应该有所帮助。

答案 1 :(得分:0)

在处理SQL数据库的类中创建一个像这样的公共方法..

public void close(){
theDatabse.close();
}

在您使用数据库的代码中,请确保调用

 theHelper.close();

当您完成访问数据库或您在onDestroy();