[英]How do i insert data to SQLite database at the biggening of the Table?
我是SQLite android開發中的新手。 我想使用SQLite保存數據。 我還輕松地擴展了SQLiteOpenHelper類。 我在表上使用了自動增量鍵ID。 當我使用Cursor.moveToFirst()到Cursor.moveToLast()打印所有數據(不使用命令的任何命令)將數據插入到表中之后,我發現數據按插入順序打印(或者,基於鍵ID升序訂購)。
好吧,那很好! 但是問題是我還需要在同一張表的開頭附加數據(或在舊數據之前先打印新插入的數據)。
我搜索了互聯網,但沒有找到任何解決方案。 我怎樣才能做到這一點?
幫我描述一下功能:
insertDataToTheBiggening(字符串數據)
insertDataToTheEnd(字符串數據)
我的代碼是:
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("");
}
我發現數據按插入順序打印
您可能已經觀察到了這種情況,但是不能保證總是如此。 通常,SQL表是按無序記錄集建模的,也就是說,任何表的記錄都沒有內部順序。 因此,解決問題的方法是只在表中進行普通的SQLite插入,如果需要訂購,則可以使用一個或多個列通過ORDER BY
子句強加該訂購。 例如,您可能有一個timestamp列,該列可以表示一個較早或較晚的記錄,並按所需順序查詢記錄。
在開始處給出負 ID以增加插入值,在結束處增加正 ID並在結尾增加插入值
在描述表時使用id最小-最大-正
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.