Android inserting into SQLite JSON object
- by Nizmon
I'm trying to insert the below json response from a server into my sqlite DB and then read from the DB. The problem I am getting is that when I run my code it compiles fine and runs with no errors. But when trying to read from the DB I just get back what seems like an empty string so I know that the table is being created.
I have the correct permissions within the manifest. I have also reference all my classes within there.
{"locations": [{"locations":"Newcastle","location_id":"1"},{"locations":"London","location_id":"2"},{"locations":"Sunderland","location_id":"3"}]}
Below where I use:
Log.v("one", one);
Log.v("two", two);
below It only prints out the first set within the JSON object so Newcastle and 1. I don't get any errors at all which is stumping me. When I call the method getName within the Location class I just seem to get a blank string back!
// This class creates the table as well as inserts and returns data from the sqlite DB
public class Location {
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static final String vd_location = ("CREATE TABLE " + TABLE_VD_LOCATION + " ("
+ LOCATION + " TEXT,"
+ LOCATION_ID + " TEXT " +");");
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(vd_location);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public Location(Context ctx) {
this.mCtx = ctx;
}
public Location open() throws SQLException {
this.mDbHelper = new DatabaseHelper(this.mCtx);
this.mDb = this.mDbHelper.getWritableDatabase();
return this;
}
public void close() {
this.mDbHelper.close();
}
public void addOffer (JSONObject json){
try{
JSONArray earthquakes = json.getJSONArray("offers");
for(int i=0;i<earthquakes.length();i++){
open();
JSONObject e = earthquakes.getJSONObject(i);
String one = e.getString("locations");
String two = e.getString("location_id");
Log.v("one", one);
Log.v("two", two);
mDb.execSQL("INSERT INTO " + TABLE_VD_LOCATION + " ( locations, location_id ) " +
"VALUES (?, ?)",
new Object [] {
e.getString("locations"),
e.getString("location_id")});
close();
}
}catch(Exception e){
}finally{
close();
}
}
public String getName(long l) throws SQLException{
String[] columns = new String[]{ LOCATION, LOCATION_ID};
Cursor c = mDb.query(TABLE_VD_LOCATION, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(LOCATION);
int iName = c.getColumnIndex(LOCATION_ID);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + "\n";
}
return result;
}
}
// This code reads from the DB, this is just some very hacked together code so excuse it, it also works when used on other tables
public void getData(){
boolean didItWork = true;
try {
Location loc = new Location(this);
loc.open();
String data = loc.getName(0);
loc.close();
Dialog t = new Dialog(this);
t.setTitle("get" + data);
t.show();
} catch (Exception e) {
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
} finally {
if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Heck Yea!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
//entry.close();
}
}
}