Domanda

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.

È stato utile?

Soluzione

Hello VinC the best approach is to auto increment id, which is your primary key so you can do it by,

String CREATE_EVENT_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_EVENT + " TEXT" + ")";

Now you can query database with orderby Id in Ascending order

database.query("TABLE_EVENTS", new String[]{"KEY_EVENT"},null,null, null, null,"KEY_ID ASC");

Altri suggerimenti

You could also have another field called "last_updated" which inserts a timestamp in milliseconds durring the insert. When you query for the cursor you could then sort by this field.

typically the first row inserted is at the top on the table so if you want to query the entire table with FIFO in your query you can set the sort order to be in ascending order by the row id, meaning the newest row will be at the bottom and the oldest at the top

getContentLoader().query(DATABASENAME,new String[] {ID,EVENT},null,null,ID+" COLLATE LOCALIZED ASC");

that way when you step through the cursor you will have the list in the order you want and you can do whatever you want with it

EDIT

in regards to the error you are getting you need to do this

if(cursor != null && cursor.moveToFirst()){
    do{
         String event = cursor.getString(0);
         //loops through all the elements in the cursor 
    }while(cursor.moveTONext());
}
cursor.close();

this prevents force closes because it checks if the cursor is null (nothing in it) and the cursor gets moved to the first element

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top