I'm using SQLite for the first time in Android. I would like to add new crises can be done in the background. But I encounter an error when I call the reference to write to the database.I try to add new crises in my database using an asyncTasck method. The code that isn't working is in the method void addCrisi(Crisi crisi). Here is it:
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Full code is:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "crisisManager";
// Crisis table name
private static final String TABLE_CRISIS = "crisis";
//Singleton
private static DatabaseHandler instance;
public static DatabaseHandler getInstance(Context context) {
if (instance == null) {
instance = new DatabaseHandler(context.getApplicationContext());
}
return instance;
}
// Crisis Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_START_DATE = "start_date";
private static final String KEY_END_DATE = "end_date";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
public void onCreate(SQLiteDatabase db) {
String CREATE_CRISIS_TABLE = "CREATE TABLE " + TABLE_CRISIS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_START_DATE + " TEXT,"
+ KEY_END_DATE + " TEXT,"
+ KEY_LATITUDE + " DOUBLE,"
+ KEY_LONGITUDE + " DOUBLE" + ")";
db.execSQL(CREATE_CRISIS_TABLE);
}
// Upgrading database
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CRISIS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new crisi
void addCrisi(Crisi crisi) {
Log.d("Add Crisi", crisi.toString());
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_START_DATE, crisi.getStartDate()); // Start Date
values.put(KEY_END_DATE, crisi.getEndDate()); // End Date
values.put(KEY_LATITUDE, crisi.getLatitude()); // Latitude
values.put(KEY_LONGITUDE, crisi.getLongitude()); // Longitude
// Inserting Row Async Inserting
//AsyncTask is anonymous class
new AsyncTask<ContentValues, Void, Void>() {
@Override
protected Void doInBackground(ContentValues... params) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 3. insert
db.insert(TABLE_CRISIS, null, params[0]);
// 4. close
db.close();
return null;
}
}.execute(values);
//db.insert(TABLE_CRISIS, null, values);
//db.close(); // Closing database connection
}
// Getting All Crisis
public List<Crisi> getAllCrisis() {
List<Crisi> crisiList = new ArrayList<Crisi>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CRISIS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Crisi crisi = new Crisi();
crisi.setId(Integer.parseInt(cursor.getString(0)));
//crisi.setName(cursor.getString(1));
//crisi.setPhoneNumber(cursor.getString(2));
crisi.setStartDate(cursor.getString(1));
crisi.setEndDate(cursor.getString(2));
crisi.setLatitude(Double.parseDouble(cursor.getString(3)));
crisi.setLongitude(Double.parseDouble(cursor.getString(4)));
// Adding crisi to list
crisiList.add(crisi);
} while (cursor.moveToNext());
}
// return crisi list
return crisiList;
}
// Updating single crisi
public int updateCrisi(Crisi crisi) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_START_DATE, crisi.getStartDate());
values.put(KEY_END_DATE, crisi.getEndDate());
values.put(KEY_LATITUDE, crisi.getLatitude()); // Latitude
values.put(KEY_LONGITUDE, crisi.getLongitude()); // Longitude
values.put(KEY_LOCALITY, crisi.getLatitude()); // Locality
values.put(KEY_COUNTRY, crisi.getLongitude()); // Country
// updating row
return db.update(TABLE_CRISIS, values, KEY_ID + " = ?",
new String[] { String.valueOf(crisi.getId()) });
}
// Deleting single crisi
public void deleteCrisi(Crisi crisi) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CRISIS, KEY_ID + " = ?",
new String[] { String.valueOf(crisi.getId()) });
db.close();
}
// Getting crisis Count
public int getCrisisCount() {
String countQuery = "SELECT * FROM " + TABLE_CRISIS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
Can someone please help? Thanks! Ps: I apologize in advance for my bad English.
Aucun commentaire:
Enregistrer un commentaire