我有一个应用程序,我想在ti中使用数据库。但我无法让它发挥作用。我将提供源代码:
~tabActivity.java - >此活动使用名称deviceActivity.java扩展另一个活动。赫拉是一些代码片段。
private DatabaseHandler DBHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBHandler = new DatabaseHandler(getApplication());
}
在这个活动中,我有一些名称和imageView的listView。当我单击imageView时,将执行此代码。在这里你可以看到,我从DBHandler类调用方法。它返回List。
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个表并填充一个表。因此,当方法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)
BTW:如果我理解正确,当我更改DATABASE_VERSION程序运行onUpgrade()方法时,它会丢弃两个表,然后调用onCreate()。当谈到db.close()
app崩溃时。但为什么?如果这是唯一的db.close()命令,数据库如何关闭?如果我删除它,应用程序工作。但我认为删除它不是一个好主意,或者它是?
答案 0 :(得分:1)
代码中有一些看起来不对的东西。
首先,在您的TABLE_ICON创建脚本中,您将ID_ICON设置为AUTOINCREMENT,但是您在插入中明确设置了ID_ICON值。
其次,在你的getAllIconsOfOldCategory方法中,如果oldCategory == 3,你只会设置selectIconsQuery,但无论是否设置了查询,它似乎都会将它传递给db.rawQuery。这肯定会导致错误。
除此之外,也许您可以发布错误日志,以便我们进一步调查?