我有一个应用程序,我想在 ti 中使用数据库。但我无法让它工作。我将提供源代码:
~tabActivity.java --> 这个活动扩展了另一个名为 deviceActivity.java 的活动。Hera 是一些代码片段。
private DatabaseHandler DBHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBHandler = new DatabaseHandler(getApplication());
}
在这个活动中,我有一些名称和 imageView 的 listView。当我单击 imageView 时,将执行此代码。在这里你可以看到,我从 DBHandler 类调用方法。它返回列表。
device_status_image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(LightsTabLightActivity.this
.getParent());
dialog.setContentView(R.layout.list_view_icon);
listView = (ListView) dialog.findViewById(R.id.list_view);
IconAdapter adapter = new IconAdapter(getLayoutInflater(),
light.id, light.oldCategory, light.name, DBHandler
.getAllIconsOfOldCategory(light.oldCategory));
listView.setAdapter(adapter);
dialog.show();
}
});
这是DatabaseHandler.java,它假设创建数据库,2个表并填充一个表。因此,当 in 方法getAllIconsOfOldCategory()
到达该行时SQLiteDatabase db = this.getWritableDatabase();
,它就开始了onCreate
。这是应用程序崩溃的地方。它在运行命令执行查询时停止工作。但我不知道,可能出了什么问题。
db.execSQL(createDeviceTable);
db.execSQL(createIconTable);
这是完整的 DatabaseHandler 类:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 4;
// Database Name
private static final String DATABASE_NAME = "myDatabase";
// Table names
private static final String TABLE_DEVICE = "device";
private static final String TABLE_ICON = "icon";
// Column names of device table
private static final String ID_DEVICE = "idDevice";
private static final String UNIT_NUMBER = "unitNumber";
private static final String DEVICE_ID = "deviceId";
private static final String FK_ICON = "kdIcon";
// Colimn names of icon table
private static final String ID_ICON = "idIcon";
private static final String CATEGORY = "category";
private static final String ON = "on";
private static final String OFF = "off";
private static final String ON_COOLING = "onCooling";
private static final String ON_HEATING = "onHeating";
private static final String ICON_0 = "icon0";
private static final String ICON_10 = "icon10";
private static final String ICON_20 = "icon20";
private static final String ICON_25 = "icon25";
private static final String ICON_30 = "icon30";
private static final String ICON_40 = "icon40";
private static final String ICON_50 = "icon50";
private static final String ICON_60 = "icon60";
private static final String ICON_75 = "icon75";
private static final String ICON_80 = "icon80";
private static final String ICON_90 = "icon90";
private static final String ICON_100 = "icon100";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createDeviceTable = "CREATE TABLE " + TABLE_DEVICE + " ("
+ ID_DEVICE
+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "
+ UNIT_NUMBER + " INTEGER NOT NULL UNIQUE , " + DEVICE_ID
+ " INTEGER NOT NULL UNIQUE , " + FK_ICON + " INTEGER)";
String createIconTable = "CREATE TABLE " + TABLE_ICON + " (" + ID_ICON
+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "
+ CATEGORY + " INTEGER NOT NULL , " + ON + " TEXT, " + OFF
+ " TEXT, " + ON_COOLING + " TEXT, " + ON_HEATING + " TEXT, "
+ ICON_0 + " TEXT, " + ICON_10 + " TEXT, " + ICON_20
+ " TEXT, " + ICON_25 + " TEXT, " + ICON_30 + " TEXT, "
+ ICON_40 + " TEXT, " + ICON_50 + " TEXT, " + ICON_60
+ " TEXT, " + ICON_75 + " TEXT, " + ICON_80 + " TEXT, "
+ ICON_90 + " TEXT, " + ICON_100 + " TEXT)";
db.execSQL(createDeviceTable);
db.execSQL(createIconTable);
ContentValues values = new ContentValues();
values.put(ID_ICON, 1);
values.put(CATEGORY, 3);
values.put(ON, "lightbulb_0_v2");
values.put(OFF, "lightbulb_100_v2");
db.insert(TABLE_ICON, null, values);
values.clear();
values.put(ID_ICON, 2);
values.put(CATEGORY, 3);
values.put(ON, "lightbulb_0_v1");
values.put(OFF, "lightbulb_max_v1");
db.insert(TABLE_ICON, null, values);
values.clear();
values.put(ID_ICON, 3);
values.put(CATEGORY, 3);
values.put(ON, "electric_outlet_on");
values.put(OFF, "electric_outlet_off");
db.insert(TABLE_ICON, null, values);
values.clear();
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEVICE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ICON);
// Create tables again
onCreate(db);
}
public List<String> getAllIconsOfOldCategory(int oldCategory) {
List<String> iconsList = new ArrayList<String>();
String selectIconsQuery = "";
if (oldCategory == 3)
selectIconsQuery = "SELECT " + ON + " FROM " + TABLE_ICON;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectIconsQuery, null);
if (cursor.moveToFirst()) {
do {
iconsList.add(cursor.getString(0));
} while (cursor.moveToNext());
}
return iconsList;
// return null;
}
}
感谢您的帮助!
编辑 1:因此,我从用于创建表的两个字符串中删除了 AUTOINCREMENT。而且,我发现程序运行良好,没有错误,直到我更改DATABASE_VERSION
为更高的值。如果我更改,则会发生此错误:
09-15 20:07:28.539: E/AndroidRuntime(8622): FATAL EXCEPTION: main
09-15 20:07:28.539: E/AndroidRuntime(8622): java.lang.IllegalStateException: database /data/data/com.netichome.android/databases/netichome (conn# 0) already closed
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2189)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2195)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:740)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:176)
09-15 20:07:28.539: E/AndroidRuntime(8622): at com.netichome.mobile.backend.DatabaseHandler.getAllIconsOfOldCategory(DatabaseHandler.java:121)
09-15 20:07:28.539: E/AndroidRuntime(8622): at com.netichome.mobile.tabActivity$3.onClick(tabActivity.java:129)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.view.View.performClick(View.java:3549)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.view.View$PerformClick.run(View.java:14393)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.os.Handler.handleCallback(Handler.java:605)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.os.Looper.loop(Looper.java:154)
09-15 20:07:28.539: E/AndroidRuntime(8622): at android.app.ActivityThread.main(ActivityThread.java:4945)
09-15 20:07:28.539: E/AndroidRuntime(8622): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 20:07:28.539: E/AndroidRuntime(8622): at java.lang.reflect.Method.invoke(Method.java:511)
09-15 20:07:28.539: E/AndroidRuntime(8622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-15 20:07:28.539: E/AndroidRuntime(8622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-15 20:07:28.539: E/AndroidRuntime(8622): at dalvik.system.NativeStart.main(Native Method)
顺便说一句:如果我理解正确,当我更改 DATABASE_VERSION 程序时运行 onUpgrade() 方法,该方法会删除两个表,然后调用 onCreate()。当涉及到db.close()
应用程序崩溃时。但为什么?如果这是唯一的 db.close() 命令,数据库如何已经关闭?如果我删除它,应用程序就会工作。但我认为删除它不是一个好主意,或者它是?