Hello Stackoverflowers!
I created a simple sqlite DB like so.
//event table name
private static final String TABLE_EVENTS = "events";
//event table columns names
private static final String KEY_ID = "id";
private static final String KEY_EVENT = "event";
@Override
public void onCreate(SQLiteDatabase db)
{
//String to define our database
String CREATE_EVENT_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_EVENT + " TEXT" + ")";
//create our database
db.execSQL(CREATE_EVENT_TABLE);
}
Which I understand to be a table with an id column and an event column.
in the event column I am storing a JSONObject converted to a string which I would like to retrieve from the table in FIFO(First in First Out) order, so that if my application crashes I can send the JSONObjects that I have stored next time the app is opened.
I can insert to the table without a problem using:
public void addEvent(JSONObject event)
{
//get our database so we can add to it
SQLiteDatabase db = this.getWritableDatabase();
//define content values to store our values
ContentValues values = new ContentValues();
//add our event to the ContentValues
values.put(KEY_EVENT, event.toString());
//insert a row
db.insert(TABLE_EVENTS, null, values);
//close the database connection
db.close();
}
I am having trouble querying for a specific row or event and getting the event from the database as well as a good method to keep track the order in which the events were inserted, so that I can pull them out in a FIFO order.
Sample code is especially helpful seeing as when I try to query I am getting syntax errors, and am not very familiar with SQLite.
Any help is much appreciated! Thank you for your time.
Hello VinC the best approach is to auto increment id, which is your primary key so you can do it by,
Now you can query database with orderby Id in Ascending order