I'm new in SQLite android developing. I want to save my data using SQLite. I also did that easily extending SQLiteOpenHelper class. I used auto-increment key ID on the table. After insert data to the Table when I print all data using Cursor.moveToFirst() to Cursor.moveToLast() (not using any ordered by command) I found that the data are printing in the insertion order (Or, based on key ID ascending order).
Well, that is good! But the problem is that I also need to append data at the beginning of the same table (or Print the newly inserted data first before the old data).
I searched internet but I didn't find any solution. How can I do that?
Help me to describe the functions:
insertDataToTheBiggening(String data)
insertDataToTheEnd(String data)
My code is:
public class StoreData extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "AllData";
private static final String TABLE = "Data";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "versity_name";
private static final String KEY_TITLE = "title";
private static final String KEY_INFO = "info";
private static final String KEY_DATE = "date";
private static final String KEY_LINK = "link";
public StoreData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public StoreData(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public StoreData(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE = "CREATE TABLE " + TABLE + "("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null, "
+ KEY_TITLE + " text not null, "
+ KEY_INFO + " text not null, "
+ KEY_DATE + " text not null, "
+ KEY_LINK + " text not null"
+ ")";
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(sqLiteDatabase);
}
public void addNewData(String VersityName, String Title, String Info, String Time, String Link)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, VersityName);
values.put(KEY_TITLE, Title);
values.put(KEY_INFO, Info);
values.put(KEY_DATE, Time);
values.put(KEY_LINK, Link);
db.insert(TABLE, null, values);
db.close();
}
}
public void PrintAllData()
{
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... strings) {
SQLiteDatabase db = getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE ;
Cursor cursor = db.rawQuery(selectQuery, null);
try{
if (cursor.moveToFirst()) {
do{
publishProgress(cursor.getString(0)+" "+cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5));
Thread.sleep(100);
}while (cursor.moveToNext());
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
db.close();
cursor.close();
}
return null;
}
@Override
protected void onProgressUpdate(String... strings)
{
ManagePanes.addPane(strings[0],strings[1],strings[2],strings[3],strings[4]);
}
}.execute("");
}
I found that the data are printing in the insertion order
You may have observed this happening, but there is no guarantee that this will always be the case. In general SQL tables are modeled after unordered sets of records, that is to say there is no internal order to the records of any table. So the solution to your problem is to just do normal SQLite inserts into your table, and if you want an order, then use one or more columns to impose that order via an ORDER BY
clause. For example, you could have a timestamp column which can represent an early or late record, and to query out records in the order you want.