问题描述
我试图将值插入的SQLite
数据库的Android应用程序。我写下面的code:
公共类DataBaseAdapter扩展SQLiteOpenHelper
{
私有静态最终诠释DATABASE_VERSION = 1;
//数据库名称
私有静态最后弦乐DATABASE_NAME =AlarmSystem;
//报警表名
私有静态最后弦乐TABLE_NAME =报警;
//告警表列名
私有静态最后弦乐KEY_ALARM_ID =ID;
私有静态最后弦乐KEY_DESC =说明;
私有静态最后弦乐KEY_REPEAT_DAY =RepeatDay;
私有静态最后弦乐KEY_REPEAT_TYPE =REPEAT_TYPE;
私有静态最后弦乐KEY_CALENDAR =日历;
私有静态最后弦乐KEY_APP =设备;
私有静态最后弦乐KEY_ACTIVE =活动;
//创建数据库
公共DataBaseAdapter(上下文CON)
{
超(CON,DATABASE_NAME,空,DATABASE_VERSION);
}
//创建表
@覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
字符串CREATE_TABLE =创建表+ TABLE_NAME +(
+ KEY_ALARM_ID +整数主键自动增量,+ KEY_DESC +TEXT+ KEY_REPEAT_DAY +TEXT
+ KEY_REPEAT_TYPE +的整数,+ KEY_CALENDAR +TEXT+ KEY_APP +TEXT+ KEY_ACTIVE +的整数);;
db.execSQL(CREATE_TABLE);
}
//更新数据库
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
如果存在//删除旧的表
db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
//再次创建表
的onCreate(DB);
}
公共无效addAlarm(报警器报警)
{
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(KEY_DESC,alarm.getDesc());
values.put(KEY_REPEAT_DAY,alarm.getRepeatDay());
values.put(KEY_REPEAT_TYPE,alarm.getRepeatType());
values.put(KEY_CALENDAR,Long.toString(alarm.getCalendarInMillis()));
values.put(KEY_APP,alarm.getApp());
values.put(KEY_ACTIVE,alarm.getActive());
db.insert(TABLE_NAME,空,价值观);
db.close();
}
我不知道是什么问题。所以我张贴了logcat的:
03-13 17:09:33.388:D / SqliteDatabaseCpp(1095):注册sqlite的记录FUNC:/data/data/com.example.devicecontrolpanel/databases/AlarmSystem
03-13 17:09:33.408:D / SqliteDatabaseCpp(1095):数据库信息:打开数据库,路径= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,标志= 6,不能与stat文件,错误号= 2,消息=没有这样的文件或目录
03-13 17:09:33.408:D / SqliteDatabaseCpp(1095):数据库的信息:PATH = /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,处理:0x8ccc98,类型:W,R / W: (0,1),模式:截断,硬盘可用空间:777中号
03-13 17:09:33.588:D / SqliteDatabaseCpp(1095):数据库的信息:关闭分贝,道路= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,手柄= 0x8ccc98,类型= W,R /瓦特=(0,0)
03-13 17:09:33.588:D / SqliteDatabaseCpp(1095):数据库信息:打开数据库,路径= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,标志= 6,文件大小= 4096
03-13 17:09:33.588:D / SqliteDatabaseCpp(1095):数据库的信息:PATH = /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,处理:0x8f3100,类型:W,R / W: (0,1),模式:截断,硬盘可用空间:777中号
03-13 17:09:33.588:D / SqliteDatabaseCpp(1095):数据库的信息:关闭分贝,道路= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,手柄= 0x8f3100,类型= W,R /瓦特=(0,0)
03-13 17:09:33.598:D / AndroidRuntime(1095):关闭虚拟机
03-13 17:09:33.598:W / dalvikvm(1095):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40ae0228)
03-13 17:09:33.618:E / AndroidRuntime(1095):致命异常:主要
03-13 17:09:33.618:E / AndroidRuntime(1095):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.devicecontrolpanel / com.example.devicecontrolpanel.Main}:java.lang.IllegalStateException:数据库/data/data/com.example.devicecontrolpanel/databases/AlarmSystem(康涅狄格州#0)已关闭
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.ActivityThread.access $ 600(ActivityThread.java:139)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1262)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.os.Handler.dispatchMessage(Handler.java:99)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.os.Looper.loop(Looper.java:156)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.ActivityThread.main(ActivityThread.java:4987)
03-13 17:09:33.618:E / AndroidRuntime(1095):在java.lang.reflect.Method.invokeNative(本机方法)
03-13 17:09:33.618:E / AndroidRuntime(1095):在java.lang.reflect.Method.invoke(Method.java:511)
03-13 17:09:33.618:E / AndroidRuntime(1095):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 17:09:33.618:E / AndroidRuntime(1095):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 17:09:33.618:E / AndroidRuntime(1095):在dalvik.system.NativeStart.main(本机方法)
03-13 17:09:33.618:E / AndroidRuntime(1095):致:java.lang.IllegalStateException:数据库/data/data/com.example.devicecontrolpanel/databases/AlarmSystem(康涅狄格州#0)已关闭
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2194)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:448)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:435)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:176)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:168)
03-13 17:09:33.618:E / AndroidRuntime(1095):在com.example.devicecontrolpanel.DataBaseAdapter.getAlarmsCount(DataBaseAdapter.java:190)
03-13 17:09:33.618:E / AndroidRuntime(1095):在com.example.devicecontrolpanel.DataBaseAdapter.getAllAlarmList(DataBaseAdapter.java:117)
03-13 17:09:33.618:E / AndroidRuntime(1095):在com.example.devicecontrolpanel.Main.onCreate(Main.java:19)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.Activity.performCreate(Activity.java:4538)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-13 17:09:33.618:E / AndroidRuntime(1095):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-13 17:09:33.618:E / AndroidRuntime(1095):11 ...更多
03-13 17:16:48.144:D / SqliteDatabaseCpp(2080):注册sqlite的记录FUNC:/data/data/com.example.devicecontrolpanel/databases/AlarmSystem
03-13 17:16:48.144:D / SqliteDatabaseCpp(2080):数据库信息:打开数据库,路径= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,标志= 6,不能与stat文件,错误号= 2,消息=没有这样的文件或目录
03-13 17:16:48.164:D / SqliteDatabaseCpp(2080):数据库的信息:PATH = /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,处理:0x86b220,类型:W,R / W: (0,1),模式:截断,硬盘可用空间:777中号
03-13 17:16:48.384:D / SqliteDatabaseCpp(2080):数据库的信息:关闭分贝,道路= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,手柄= 0x86b220,类型= W,R /瓦特=(0,0)
03-13 17:16:48.384:D / SqliteDatabaseCpp(2080):数据库信息:打开数据库,路径= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,标志= 6,文件大小= 5120
03-13 17:16:48.384:D / SqliteDatabaseCpp(2080):数据库的信息:PATH = /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,处理:0x897800,类型:W,R / W: (0,1),模式:截断,硬盘可用空间:777中号
03-13 17:16:48.384:D / SqliteDatabaseCpp(2080):数据库的信息:关闭分贝,道路= /data/data/com.example.devicecontrolpanel/databases,关键= 9lYvmWqw,手柄= 0x897800,类型= W,R /瓦特=(0,0)
03-13 17:16:48.394:D / AndroidRuntime(2080):关闭虚拟机
03-13 17:16:48.414:W / dalvikvm(2080):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40ae0228)
03-13 17:16:48.424:E / AndroidRuntime(2080):致命异常:主要
03-13 17:16:48.424:E / AndroidRuntime(2080):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.devicecontrolpanel / com.example.devicecontrolpanel.Main}:java.lang.IllegalStateException:数据库/data/data/com.example.devicecontrolpanel/databases/AlarmSystem(康涅狄格州#0)已关闭
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.ActivityThread.access $ 600(ActivityThread.java:139)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1262)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.os.Handler.dispatchMessage(Handler.java:99)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.os.Looper.loop(Looper.java:156)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.ActivityThread.main(ActivityThread.java:4987)
03-13 17:16:48.424:E / AndroidRuntime(2080):在java.lang.reflect.Method.invokeNative(本机方法)
03-13 17:16:48.424:E / AndroidRuntime(2080):在java.lang.reflect.Method.invoke(Method.java:511)
03-13 17:16:48.424:E / AndroidRuntime(2080):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 17:16:48.424:E / AndroidRuntime(2080):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 17:16:48.424:E / AndroidRuntime(2080):在dalvik.system.NativeStart.main(本机方法)
03-13 17:16:48.424:E / AndroidRuntime(2080):致:java.lang.IllegalStateException:数据库/data/data/com.example.devicecontrolpanel/databases/AlarmSystem(康涅狄格州#0)已关闭
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2194)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:448)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:435)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:176)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:168)
03-13 17:16:48.424:E / AndroidRuntime(2080):在com.example.devicecontrolpanel.DataBaseAdapter.getAlarmsCount(DataBaseAdapter.java:196)
03-13 17:16:48.424:E / AndroidRuntime(2080):在com.example.devicecontrolpanel.DataBaseAdapter.getAllAlarmList(DataBaseAdapter.java:123)
03-13 17:16:48.424:E / AndroidRuntime(2080):在com.example.devicecontrolpanel.Main.onCreate(Main.java:19)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.Activity.performCreate(Activity.java:4538)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-13 17:16:48.424:E / AndroidRuntime(2080):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-13 17:16:48.424:E / AndroidRuntime(2080):11 ...更多
在一般最好是单独包装的方法(在您的案件 addAlarm,EditAlarm为,deleteAlarm
)在一个单独的类。我建议你创建一个新的类名为 myAppDataSource
并添加所有的包装方法的类,使您的生活更轻松。
在code该类会是这样的:
公共类StudyManagerDataSource {
SQLiteOpenHelper dbHelper;
SQLiteDatabase数据库;
公共StudyManagerDataSource(上下文的背景下){
dbHelper =新DatabaseAdapter(上下文);
}
公共无效addAlarm(报警器报警)
{
//打开数据库连接
打开();
ContentValues值=新ContentValues();
values.put(KEY_DESC,alarm.getDesc());
values.put(KEY_REPEAT_DAY,alarm.getRepeatDay());
values.put(KEY_REPEAT_TYPE,alarm.getRepeatType());
values.put(KEY_CALENDAR,Long.toString(alarm.getCalendarInMillis()));
values.put(KEY_APP,alarm.getApp());
values.put(KEY_ACTIVE,alarm.getActive());
database.insert(TABLE_NAME,空,价值观);
//我会建议保持连接到数据库打开时,您的应用程序是
//运行时,建议及时关闭连接是当你的应用程序是
//要暂停/停止。
关闭();
}
公共无效的open(){
数据库= dbHelper.getWritableDatabase();
Log.i(LOG_TAG,打开数据库);
} // end方法公开
公共无效的close(){
Log.i(LOG_TAG,关闭数据库);
dbHelper.close();
} // end方法关闭
}
I am trying to insert values into SQLite
Database for Android Application.I am writing following code:
public class DataBaseAdapter extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "AlarmSystem";
// Alarm table name
private static final String TABLE_NAME = "Alarms";
//AlarmTable Columns names
private static final String KEY_ALARM_ID = "Id";
private static final String KEY_DESC = "Description";
private static final String KEY_REPEAT_DAY = "RepeatDay";
private static final String KEY_REPEAT_TYPE = "REPEAT_TYPE";
private static final String KEY_CALENDAR = "Calendar";
private static final String KEY_APP = "Device";
private static final String KEY_ACTIVE = "Active";
//creating database
public DataBaseAdapter(Context con)
{
super(con, DATABASE_NAME, null, DATABASE_VERSION);
}
//creating table
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_TABLE = "Create Table "+TABLE_NAME+"("
+KEY_ALARM_ID+" integer primary key AUTOINCREMENT, "+KEY_DESC+" TEXT, "+KEY_REPEAT_DAY+ " TEXT,"
+KEY_REPEAT_TYPE+" integer, "+KEY_CALENDAR+" TEXT, "+KEY_APP+" TEXT, "+KEY_ACTIVE+" integer );";
db.execSQL(CREATE_TABLE);
}
//updating database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
public void addAlarm(Alarm alarm)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DESC, alarm.getDesc());
values.put(KEY_REPEAT_DAY, alarm.getRepeatDay());
values.put(KEY_REPEAT_TYPE, alarm.getRepeatType());
values.put(KEY_CALENDAR, Long.toString(alarm.getCalendarInMillis()));
values.put(KEY_APP, alarm.getApp());
values.put(KEY_ACTIVE, alarm.getActive());
db.insert(TABLE_NAME, null, values);
db.close();
}
I don't know what is the problem. So I am posting the Logcat:
03-13 17:09:33.388: D/SqliteDatabaseCpp(1095): Registering sqlite logging func: /data/data/com.example.devicecontrolpanel/databases/AlarmSystem
03-13 17:09:33.408: D/SqliteDatabaseCpp(1095): DB info: open db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, flag = 6, cannot stat file, errno = 2, message = No such file or directory
03-13 17:09:33.408: D/SqliteDatabaseCpp(1095): DB info: path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle: 0x8ccc98, type: w, r/w: (0,1), mode: truncate, disk free size: 777 M
03-13 17:09:33.588: D/SqliteDatabaseCpp(1095): DB info: close db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle = 0x8ccc98, type = w, r/w = (0, 0)
03-13 17:09:33.588: D/SqliteDatabaseCpp(1095): DB info: open db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, flag = 6, file size = 4096
03-13 17:09:33.588: D/SqliteDatabaseCpp(1095): DB info: path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle: 0x8f3100, type: w, r/w: (0,1), mode: truncate, disk free size: 777 M
03-13 17:09:33.588: D/SqliteDatabaseCpp(1095): DB info: close db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle = 0x8f3100, type = w, r/w = (0, 0)
03-13 17:09:33.598: D/AndroidRuntime(1095): Shutting down VM
03-13 17:09:33.598: W/dalvikvm(1095): threadid=1: thread exiting with uncaught exception (group=0x40ae0228)
03-13 17:09:33.618: E/AndroidRuntime(1095): FATAL EXCEPTION: main
03-13 17:09:33.618: E/AndroidRuntime(1095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devicecontrolpanel/com.example.devicecontrolpanel.Main}: java.lang.IllegalStateException: database /data/data/com.example.devicecontrolpanel/databases/AlarmSystem (conn# 0) already closed
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.ActivityThread.access$600(ActivityThread.java:139)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.os.Looper.loop(Looper.java:156)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-13 17:09:33.618: E/AndroidRuntime(1095): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 17:09:33.618: E/AndroidRuntime(1095): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 17:09:33.618: E/AndroidRuntime(1095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 17:09:33.618: E/AndroidRuntime(1095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 17:09:33.618: E/AndroidRuntime(1095): at dalvik.system.NativeStart.main(Native Method)
03-13 17:09:33.618: E/AndroidRuntime(1095): Caused by: java.lang.IllegalStateException: database /data/data/com.example.devicecontrolpanel/databases/AlarmSystem (conn# 0) already closed
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2194)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:448)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:435)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:176)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:168)
03-13 17:09:33.618: E/AndroidRuntime(1095): at com.example.devicecontrolpanel.DataBaseAdapter.getAlarmsCount(DataBaseAdapter.java:190)
03-13 17:09:33.618: E/AndroidRuntime(1095): at com.example.devicecontrolpanel.DataBaseAdapter.getAllAlarmList(DataBaseAdapter.java:117)
03-13 17:09:33.618: E/AndroidRuntime(1095): at com.example.devicecontrolpanel.Main.onCreate(Main.java:19)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.Activity.performCreate(Activity.java:4538)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-13 17:09:33.618: E/AndroidRuntime(1095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-13 17:09:33.618: E/AndroidRuntime(1095): ... 11 more
03-13 17:16:48.144: D/SqliteDatabaseCpp(2080): Registering sqlite logging func: /data/data/com.example.devicecontrolpanel/databases/AlarmSystem
03-13 17:16:48.144: D/SqliteDatabaseCpp(2080): DB info: open db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, flag = 6, cannot stat file, errno = 2, message = No such file or directory
03-13 17:16:48.164: D/SqliteDatabaseCpp(2080): DB info: path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle: 0x86b220, type: w, r/w: (0,1), mode: truncate, disk free size: 777 M
03-13 17:16:48.384: D/SqliteDatabaseCpp(2080): DB info: close db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle = 0x86b220, type = w, r/w = (0, 0)
03-13 17:16:48.384: D/SqliteDatabaseCpp(2080): DB info: open db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, flag = 6, file size = 5120
03-13 17:16:48.384: D/SqliteDatabaseCpp(2080): DB info: path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle: 0x897800, type: w, r/w: (0,1), mode: truncate, disk free size: 777 M
03-13 17:16:48.384: D/SqliteDatabaseCpp(2080): DB info: close db, path = /data/data/com.example.devicecontrolpanel/databases , key = 9lYvmWqw, handle = 0x897800, type = w, r/w = (0, 0)
03-13 17:16:48.394: D/AndroidRuntime(2080): Shutting down VM
03-13 17:16:48.414: W/dalvikvm(2080): threadid=1: thread exiting with uncaught exception (group=0x40ae0228)
03-13 17:16:48.424: E/AndroidRuntime(2080): FATAL EXCEPTION: main
03-13 17:16:48.424: E/AndroidRuntime(2080): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devicecontrolpanel/com.example.devicecontrolpanel.Main}: java.lang.IllegalStateException: database /data/data/com.example.devicecontrolpanel/databases/AlarmSystem (conn# 0) already closed
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.ActivityThread.access$600(ActivityThread.java:139)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.os.Looper.loop(Looper.java:156)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-13 17:16:48.424: E/AndroidRuntime(2080): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 17:16:48.424: E/AndroidRuntime(2080): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 17:16:48.424: E/AndroidRuntime(2080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 17:16:48.424: E/AndroidRuntime(2080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 17:16:48.424: E/AndroidRuntime(2080): at dalvik.system.NativeStart.main(Native Method)
03-13 17:16:48.424: E/AndroidRuntime(2080): Caused by: java.lang.IllegalStateException: database /data/data/com.example.devicecontrolpanel/databases/AlarmSystem (conn# 0) already closed
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2194)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:448)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:435)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:176)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:168)
03-13 17:16:48.424: E/AndroidRuntime(2080): at com.example.devicecontrolpanel.DataBaseAdapter.getAlarmsCount(DataBaseAdapter.java:196)
03-13 17:16:48.424: E/AndroidRuntime(2080): at com.example.devicecontrolpanel.DataBaseAdapter.getAllAlarmList(DataBaseAdapter.java:123)
03-13 17:16:48.424: E/AndroidRuntime(2080): at com.example.devicecontrolpanel.Main.onCreate(Main.java:19)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.Activity.performCreate(Activity.java:4538)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-13 17:16:48.424: E/AndroidRuntime(2080): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-13 17:16:48.424: E/AndroidRuntime(2080): ... 11 more
In general it is better to separate the wrapper method ( in your case addAlarm, editAlarm, deleteAlarm
) in a seperate class. I suggest you to create a new class with name myAppDataSource
and add all the wrapper method in that class to make your life easier.
The code for that class will look like this:
public class StudyManagerDataSource {
SQLiteOpenHelper dbHelper;
SQLiteDatabase database;
public StudyManagerDataSource(Context context) {
dbHelper = new DatabaseAdapter(context);
}
public void addAlarm(Alarm alarm)
{
// open the database connection
open();
ContentValues values = new ContentValues();
values.put(KEY_DESC, alarm.getDesc());
values.put(KEY_REPEAT_DAY, alarm.getRepeatDay());
values.put(KEY_REPEAT_TYPE, alarm.getRepeatType());
values.put(KEY_CALENDAR, Long.toString(alarm.getCalendarInMillis()));
values.put(KEY_APP, alarm.getApp());
values.put(KEY_ACTIVE, alarm.getActive());
database.insert(TABLE_NAME, null, values);
// I will suggest to keep the connection to the database open when your app is
// running, the recommended time to close the connection is when your app is
//going to pause/stop.
close();
}
public void open() {
database = dbHelper.getWritableDatabase();
Log.i(LOG_TAG, "Database Opened");
} // end method open
public void close() {
Log.i(LOG_TAG, "Database Closed");
dbHelper.close();
} // end method close
}
这篇关于Android的SQLite的ContentValues不插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!