I'm trying to add a database to my app. However I cant run it without an error. I'm following this article Here's my code.
First I have the Settings class
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;
}
After that comes the Handler
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();
}
And Im using it like that
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);
}
}
However every time when I try to use a function from the handler I'm getting an error just after SQLiteDatabase db = this.getWritableDatabase(); And the error itself is : attempt to re-open an already-closed object: SQLiteDatabase: Which tells me that the object is closed but I'm just creating it again ?! My code is completely the same as the tutorial.. I'm using android studio if it does matter.
Aucun commentaire:
Enregistrer un commentaire