Reputation: 9
I have one registration form which is connected to database.When i insert the record the recent record comes first in table.I want recent record to be appeared below the last entered record. Can you help me out there?
long flag = 0;
int id = 1;
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query("tbl_countries", new String[]{"count(*) phone"}, null, null, null, null, null);
while(cursor.moveToNext())
{
int idFromDatabase = cursor.getInt(cursor.getColumnIndex("phone"));
if(idFromDatabase != 0)
{
id = 1 + idFromDatabase;
}
}
ContentValues values = new ContentValues();
//values.put("ID", id);
values.put("phone", Long.parseLong((phone.getText().toString())));
values.put("fname", fnametxt.getText().toString().trim());
values.put("lname", lnametxt.getText().toString().trim());
if(male.isChecked())
{
values.put("gender","male");
}
else
values.put("gender", "Female");
values.put("email", emailtxt.getText().toString());
values.put("mainpin",pin1.getText().toString()+pin2.getText().toString()+pin3.getText().toString()+pin4.getText().toString());
flag = db.insert("tbl_countries", null, values);
if(flag != -1)
{
Toast toast = Toast.makeText(getApplicationContext(), "You have successful inserted this record into database! "+flag, Toast.LENGTH_LONG);
toast.show();
db.close();
return;
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), "An error occured when insert this record into database!", Toast.LENGTH_LONG);
toast.show();
db.close();
return;
}
Please Suggest me if anything is wrong in my code.
Upvotes: 0
Views: 569
Reputation: 1005
What can you do is give id (auto increment) column in your Registration table and when you query your data at that time get it by ascending order for id column thats it.
Upvotes: 0
Reputation: 20706
The SQL standard does not guarantee any particular order for previously inserted records upon retrieval. This means that even though you may be seeing those records in what looks like a predictable order, this may change at any time, without any warning. The DBMS in question may decide to defragment its storage at some point, and reorder records in the progress - this is perfectly legal behavior, and you must be prepared for it. The only way of getting records in a reliable consistent order is if you use (surprise!) an ORDER BY
clause in your retrieval query. Data in the table does not have a defined ordering, it only gets ordered when you retrieve it.
If you want to show your data in order of insertion, consider using an auto-increment column and order by that (either ascending to list oldest first, or descending for newest first).
TL;DR: insertion order is meaningless in SQL; if you want your data ordered, use SELECT ... ORDER BY
.
Upvotes: 0
Reputation: 26332
Your problem is not with the insertion of the records, but with the retrieval.
Strictly theoretically data in a database doesn't have an order. It is retrieved unsorted, or sorted according to whatever criteria you specify.
In this case you'd want to sort on a date field, or possibly the ID, to get the order you want.
Upvotes: 1