不知道有什麼問題, 我試圖添加更多的表時,我已經在一個表上工作的數據庫,並且表示已經設置了新表。Android錯誤:關閉()從未在數據庫上明確調用
活動發送一個枚舉告訴它要尋找什麼類型的位置處,即以解決什麼表
這是類手柄的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();
}
}
}
EDIT 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();
`
這樣,我調用close()的位置對象,這就要求無論是閱讀關閉或writeclose取決於我稱之爲這是在位置(SQL文件)宣佈此調用close上theHelper – user1086501
你需要調用它裏面你readClose和writeClose函數,因爲它不是一個靜態變量。 – Suchi
你的權利發現,伴隨着我做出的其他錯誤,包括不把我的代碼切換部分的休息,歡呼聲,明天會發布修復程序,以防萬一我做了什麼 – user1086501