当前位置: 首页 > 面试题库 >

Android SQLite ContentValues未插入

史意致
2023-03-14
问题内容

我正在尝试将值插入SQLiteAndroid应用程序数据库。我正在编写以下代码:

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();
}

我不知道是什么问题。所以我发布了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

问题答案:

通常,最好将包装方法(在您的情况下addAlarm, editAlarm, deleteAlarm)分隔在单独的类中。我建议您使用名称创建一个新类,myAppDataSource并在该类中添加所有包装方法,以使您的生活更轻松。

该类的代码如下所示:

 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
}


 类似资料:
  • 问题内容: 我一直使用类似于以下内容的方法来实现它: …但是一旦加载,就会发生主键冲突。这是唯一插入到该表中的唯一语句。那么这是否意味着上述陈述不是原子的? 问题在于,这几乎不可能随意重建。 也许我可以将其更改为以下内容: 虽然,也许我使用了错误的锁,或者使用了过多的锁之类的东西。 看到了其他问题,那里的答案都提示“ IF(SELECT COUNT(*)…INSERT”等),但我始终处于(也许不正

  • 问题内容: 我正在使用以下内容尝试将记录插入到postgresql数据库表中,但是它不起作用。我没有任何错误,但是表中没有记录。我需要提交或其他东西吗?我正在使用随Bitnami djangostack安装安装的postgresql数据库。 问题答案: 如果不想将每个条目都提交到数据库,则可以添加以下行: 因此,您得到的代码将是:

  • 问题内容: 我在使用mgo将mongodb中保留golang结构时遇到问题。 问题是,在完成Insert()调用后,唯一保留在数据库中的是空文档 我检查struct字段是否确实已设置,并且不为空。关于为什么这样的任何想法。提示表示赞赏:)谢谢 问题答案: 您需要通过以大写字母开头的字段名称来导出字段。

  • 我的插入符号包有问题,请查看下面的错误, 错误:Dyn.load(file,DLLpath=DLLpath,...)中“cert”的包或命名空间加载失败:无法加载共享对象“/library/framework/R.framework/versions/4.0/resources/library/rcpp/libs/rcpp.so”:dlopen(/library/framework/R.frame

  • 嗨,我正在用spring boot hibernate测试MySQL中的记录删除-插入-删除操作,我注意到一个问题,当插入发生时,记录似乎没有被完全删除。 3在hibernate kickstart上创建初始记录(通过类路径中的data.sql) 操作为 (1)创建的初始记录 (2)删除所有记录 (3)添加新记录 (4)删除所有记录 当执行断言时,发现结果列表中仍然包含最后一条记录,因此断言失败。

  • 我有一个问题插入不工作。读取(选择)工作正常。但是,在我的@Transactional方法的末尾没有执行insert。即使我确实看到在实体管理器上调用了事务commit/close。我已经尝试了不同的配置,但我仍然无法获得记录(插入)在事务结束时工作。而且我没有看到任何错误产生。我启用了hibernate.transaction调试,但没有看到任何Hibernate事务消息。 我正在使用Tomca