Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android sqlite数据库错误_Android_Database_Sqlite - Fatal编程技术网

Android sqlite数据库错误

Android sqlite数据库错误,android,database,sqlite,Android,Database,Sqlite,我正在尝试向我的应用程序添加数据库。 然而,我不能运行它没有错误。 我正在关注这个 这是我的密码 首先我要上设置课 public class Settings { int _id; int _update_interval; public Settings(){ } public Settings(int id,int update_interval){ this._id = id; this._update_interval = update_interval; }

我正在尝试向我的应用程序添加数据库。 然而,我不能运行它没有错误。 我正在关注这个 这是我的密码

首先我要上设置课

public class Settings {

int _id;
int _update_interval;

public Settings(){

}

public Settings(int id,int update_interval){
    this._id = id;
    this._update_interval = update_interval;
}

public Settings(int update_interval){
    this._update_interval = update_interval;
}

public int getID(){
    return this._id;
}

public void setID(int id){
    this._id = id;
}

public Integer getUpdateInterval(){
    return this._update_interval;
}

public void setUpdateInterval(int update_interval){
    this._update_interval = update_interval;
}
之后是处理程序

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "app_data";
private static final String TABLE_SETTINGS = "settings";
private static final String KEY_ID = "id";
private static final String KEY_UPDATE_INTERVAL = "update_interval";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_SETTINGS_TABLE = "CREATE TABLE " + TABLE_SETTINGS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_UPDATE_INTERVAL + " INTEGER " + ")";
    db.execSQL(CREATE_SETTINGS_TABLE);

        ContentValues values = new ContentValues();
        values.put(KEY_ID, 1);
        values.put(KEY_UPDATE_INTERVAL, 60);

        db.insert(TABLE_SETTINGS, null, values);
        db.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTINGS);

    onCreate(db);
}

Settings getSettings(int id) {
    SQLiteDatabase dbа = this.getReadableDatabase();

    Cursor cursor = dbа.query(TABLE_SETTINGS, new String[] { KEY_ID,
                    KEY_UPDATE_INTERVAL }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Settings settings = new Settings(cursor.getInt(0),cursor.getInt(1));

    return settings;
}

void addSettings(Settings settings) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ID, settings.getID());
    values.put(KEY_UPDATE_INTERVAL, settings.getUpdateInterval());

    db.insert(TABLE_SETTINGS, null, values);

    db.close();

}
我就是这样用的

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DatabaseHandler db = new DatabaseHandler(this);

    Settings settings = db.getSettings(1);
}
}

但是,每次尝试使用处理程序中的函数时,我都会在SQLiteDatabase db=this.getWritableDatabase()之后出现错误; 错误本身是:尝试重新打开已关闭的对象:SQLiteDatabase: 这告诉我对象是闭合的,但我只是在再次创建它?! 我的代码与教程完全相同。。
如果有关系的话,我正在使用android studio。

删除
onCreate()中的
db.close()
。您不拥有作为参数传递的数据库,您不应该关闭它。

您需要删除任何使用
db.close()
onCreate()

SQLiteOpenHelper
拥有数据库对象,当Android关闭Helper时,它将为您关闭数据库,就像它为您打开数据库一样

此外,您不应该在主线程中调用从数据库获取内容(即
onCreate()
中的
getSettings()
),因为它使用
getReadableDatabase()
getWriteableDatabase
,这些可能需要很长时间才能返回并导致ANR。

不仅在
onCreate
上,而且在
addSettings
上。在
addSettings()
中的
close()
不是问题。对于将来:如果您打开了连接,您应该关闭它;如果您未打开连接,请不要尝试关闭它。