gpt4 book ai didi

java - 如何将数据插入到 SQLite 数据库表的开头?

转载 作者:行者123 更新时间:2023-11-30 06:24:45 25 4
gpt4 key购买 nike

我是 SQLite android 开发的新手。我想使用 SQLite 保存数据。我还轻松扩展了 SQLiteOpenHelper 类。我在表上使用了自动递增键ID。将数据插入表后,当我使用 Cursor.moveToFirst() 打印所有数据到 Cursor.moveToLast() (不使用任何按命令排序)时,我发现数据按插入顺序打印(或者,根据键 ID 升序)。

嗯,这很好!但问题是我还需要在同一个表的开头追加数据(或者在旧数据之前先打印新插入的数据)。

我搜索了互联网,但没有找到任何解决方案。我怎样才能做到这一点?

帮我描述一下功能:

insertDataToTheBiggening(String data)

insertDataToTheEnd(String data)

我的代码是:

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

您可能已经观察到这种情况的发生,但不能保证情况总是如此。一般来说,SQL 表是根据无序记录集建模的,也就是说,任何表的记录都没有内部顺序。因此,问题的解决方案就是在表中执行正常的 SQLite 插入,如果您想要一个顺序,则使用一个或多个列通过 ORDER BY 子句强加该顺序。例如,您可以有一个时间戳列,它可以表示较早或较晚的记录,并按照您想要的顺序查询记录。

关于java - 如何将数据插入到 SQLite 数据库表的开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47427772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com