如何重新打开或关闭SQLitedatabase?

时间:2014-08-07 20:59:35

标签: android sql sqlite

我已经针对同样的问题浏览了很多页面,但我仍然无法找到解决方案!!

我收到错误"尝试重新打开已关闭的对象SQLiteDatabase"。

public List getData(){
List list = new ArrayList();
Cursor c = db.rawQuery("SELECT * FROM Table",null );
int i = cursor.getColumnIndex(KEY1);
int ii = cursor.getColumnIndex(KEY2);
int iii = cursor.getColumnIndex(KEY3);
int inum = cursor.getColumnIndex(KEY4); 

cursor.moveToFirst();
  while (!cursor.isAfterLast()){

String student = cursor.getString(inum) + "\t" +cursor.getString(i) +"\t" +cursor.getString(ii) + "\t" +cursor.getString(iii);

     list.add(student); 
         cursor.moveToPrevious();   
        }
    }


      public List getInfo()


 {

List list = new ArrayList();
 Cursor c = db.rawQuery("SELECT * FROM Table where name = ?",new string[]{"android"} );
int i = cursor.getColumnIndex(KEY1);
int ii = cursor.getColumnIndex(KEY2);
int iii = cursor.getColumnIndex(KEY3);
int inum = cursor.getColumnIndex(KEY4); 

cursor.moveToFirst();
  while (!cursor.isAfterLast()){

String student = cursor.getString(inum) + "\t" +cursor.getString(i) +"\t" +cursor.getString(ii) + "\t" +cursor.getString(iii);

     list.add(student); 
         cursor.moveToPrevious();   
        }

我将此值作为列表并将其放入列表

ListView list = (ListView) findViewById(R.id.list);
ListView list1 = (ListView) findViewById(R.id.list1);
 List val = info.getData();
 List val1 - info.getInfo();
    ArrayAdapter adapter = new      ArrayAdapter(this,android.R.layout.simple_list_item_1,val);
ArrayAdapter adapter1 = new       ArrayAdapter(this,android.R.layout.simple_list_item_1,val1);
    list.setAdapter(adapter);
    list1.setAdapter(adapter1);
info.close()

1 个答案:

答案 0 :(得分:0)

首先,您在哪里创建数据库?

您应该使用扩展SQLiteOpenHelper

的类

例如:

public class MySQLiteHelper extends SQLiteOpenHelper {
     //here you create your database and tables
}

打开/关闭数据库

// Database Fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

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

更多信息/示例: http://www.vogella.com/tutorials/AndroidSQLite/article.html