成功获取数据后出现异常。我已关闭光标。
例外-
11-06 14:31:09.942: I/dalvikvm(598): Uncaught exception thrown by finalizer (will be discarded):
11-06 14:31:09.942: I/dalvikvm(598): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c115b8 on null that has not been deactivated or closed
11-06 14:31:09.942: I/dalvikvm(598): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-06 14:31:09.942: I/dalvikvm(598): at dalvik.system.NativeStart.run(Native Method)
11-06 14:31:09.942: I/dalvikvm(598): Uncaught exception thrown by finalizer (will be discarded):
11-06 14:31:09.942: I/dalvikvm(598): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c10768 on null that has not been deactivated or closed
11-06 14:31:09.942: I/dalvikvm(598): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-06 14:31:09.942: I/dalvikvm(598): at dalvik.system.NativeStart.run(Native Method)
11-06 14:31:09.942: I/dalvikvm(598): Uncaught exception thrown by finalizer (will be discarded):
11-06 14:31:09.942: I/dalvikvm(598): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c0f918 on null that has not been deactivated or closed
11-06 14:31:09.942: I/dalvikvm(598): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-06 14:31:09.942: I/dalvikvm(598): at dalvik.system.NativeStart.run(Native Method)
11-06 14:31:09.942: I/dalvikvm(598): Uncaught exception thrown by finalizer (will be discarded):
11-06 14:31:09.942: I/dalvikvm(598): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c0df08 on null that has not been deactivated or closed
11-06 14:31:09.942: I/dalvikvm(598): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-06 14:31:09.942: I/dalvikvm(598): at dalvik.system.NativeStart.run(Native Method)
11-06 14:31:09.942: I/dalvikvm(598): Uncaught exception thrown by finalizer (will be discarded):
11-06 14:31:09.942: I/dalvikvm(598): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c0c990 on null that has not been deactivated or closed
11-06 14:31:09.942: I/dalvikvm(598): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
11-06 14:31:09.942: I/dalvikvm(598): at dalvik.system.NativeStart.run(Native Method)
Sql 语句
String tableName="StateList_Tbl";
String databaseName="TravelguideDb";
int databaseVersion=3;
assetDatabaseHelper=new AssetDatabaseOpenHelper(HomeScreen.this, databaseName,databaseVersion);
assetDatabaseHelper.openDatabase();
assetDatabaseHelper.openReadableMode();
groupEntityList=new ArrayList<GroupEntity>();
String selectQuery = "SELECT State_id,State_name FROM " +tableName +" where country_part=36";
groupCursor=assetDatabaseHelper.executeQuery(selectQuery);
if (groupCursor.moveToFirst())
{
do {
groupEntityObj=new GroupEntity();
groupEntityObj.setGroupId(groupCursor.getInt(0));
System.out.println("stateid"+groupCursor.getInt(0));
groupEntityObj.setGroupName(groupCursor.getString(1));
System.out.println("stateid"+groupCursor.getString(1));
String stateQuery="SELECT State_id,State_name FROM " +tableName +" where country_part="+groupCursor.getInt(0);
stateCursor=assetDatabaseHelper.executeQuery(stateQuery);
stateArrayList=new ArrayList<StateMetaData>();
if(stateCursor.moveToFirst())
{
do{
stateMetaDataObj=new StateMetaData();
stateMetaDataObj.setStateId(stateCursor.getInt(0));
System.out.println("stateid"+stateCursor.getInt(0));
stateMetaDataObj.setStateName(stateCursor.getString(1));
System.out.println("stateid"+stateCursor.getString(1));
stateArrayList.add(stateMetaDataObj);
}while(stateCursor.moveToNext());
}
groupEntityObj.setStatesList(stateArrayList);
groupEntityList.add(groupEntityObj);
} while (groupCursor.moveToNext());
}
System.out.println("gr "+groupEntityList.size());
stateCursor.close();
groupCursor.close();
assetDatabaseHelper.close();
System.out.println("cursor"+stateCursor.getColumnCount());
System.out.println("cursor"+groupCursor);
AssetDatabaseOpenHelper 类
public class AssetDatabaseOpenHelper {
private Context context;
private SQLiteDatabase sqliteDatabaseObj;
private String database_name;
private CreateQueries createQueriesObj;
private MySQLiteHelper mySQLitehelperObj;
private int database_version;
private String databaseName="TravelguideDb";
private int databaseVersion=3;
public AssetDatabaseOpenHelper(Context context,String databaseName,int database_version) {
this.context = context;
this.database_name=databaseName;
this.database_version=database_version;
}
public AssetDatabaseOpenHelper(Context context) {
this.context = context;
this.database_name = databaseName;
this.database_version = databaseVersion;
}
public void openDatabase() {
mySQLitehelperObj = new MySQLiteHelper(context, database_name,
database_version);
File dbFile = context.getDatabasePath(database_name);
System.out.println("Assests"+dbFile.exists());
if (!dbFile.exists()) {
try {
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
// e.printStackTrace();
}
}
// sqliteDatabaseObj= SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
// sqliteDatabaseObj = mySQLitehelperObj.getReadableDatabase();
}
public void openReadableMode()
{
sqliteDatabaseObj = mySQLitehelperObj.getReadableDatabase();
}
public void openWriteableMode()
{
sqliteDatabaseObj = mySQLitehelperObj.getWritableDatabase();
}
public void close() {
mySQLitehelperObj.close();
}
private void copyDatabase(File dbFile) throws IOException {
OutputStream os = new FileOutputStream(dbFile);
InputStream is = null;
byte[] buffer = new byte[1024];
for(int i=1;i<5;i++)
{
is = context.getAssets().open("TravelguideDb.sqlite.00"+1);
int length;
while ((length=is.read(buffer))!=-1) {
os.write(buffer,0,length);
}
is.close();
}
os.flush();
os.close();
is.close();
}
public Cursor executeQuery(String query)
{
Cursor outputCursor= sqliteDatabaseObj.rawQuery(query, null);
return outputCursor;
}
public void createTable(String tableName, String[] columns, String[] value) {
createQueriesObj = new CreateQueries();
String createTableQuery = createQueriesObj.CreateTableQuery(tableName,
columns, value);
sqliteDatabaseObj.execSQL(createTableQuery);
System.out.println("Query" + createTableQuery);
}
public void deleteTable(String tableName)
{
sqliteDatabaseObj.execSQL("Drop table " + tableName);
}
public void deleteAllDataFromTable(String tableName) {
// truncate table
sqliteDatabaseObj.delete(tableName, null, null);
}
public void deletePerticularRows(String tableName, String whereClause,
String[] whereArgs) {
sqliteDatabaseObj.delete(tableName, whereClause, whereArgs);
}
public Cursor fetchAllRows(String tableName) {
return sqliteDatabaseObj.query(tableName, null, null, null, null, null,
null);
}
public Cursor selectOnWhereCondition(String tableName,
String[] columnsToSelect, String whereColumnName,
String[] whereEqualsTo, String groupBy, String having,
String orderBy) {
return sqliteDatabaseObj.query(tableName, columnsToSelect,
whereColumnName, whereEqualsTo, groupBy, having, orderBy);
}
}