我的SQLite数据库中有两个表。我试图从table1中选择三个随机行,从table2中选择三个随机行,并在listView中交替使用它们。
(前) table1随机行 table2随机行 table1随机行 table2随机行 table1随机行 table2随机行
由于某种原因,它不会交替listView中每个表的行。它按照自己想要的顺序放置它们。
这是我选择行并将它们插入所选表格的代码:
while (x <= 2) {
cursor1 = db.rawQuery("SELECT * FROM table1 ORDER BY RANDOM() LIMIT 1", null);
cursor1.moveToFirst();
rowId = cursor1.getInt(cursor1.getColumnIndex("_id"));
dbHandler.getWritableDatabase().execSQL("INSERT INTO chosen SELECT * FROM table1 WHERE _id = '" + rowId + "'");
dbHandler.getWritableDatabase().execSQL("UPDATE chosen SET _id = '" + newID + "' WHERE _id = '" + rowId + "'");
newID++;
cursor2 = db.rawQuery("SELECT * FROM table2 ORDER BY RANDOM() LIMIT 1", null);
cursor2.moveToFirst();
rowId = cursor2.getInt(cursor2.getColumnIndex("_id"));
dbHandler.getWritableDatabase().execSQL("INSERT INTO chosen SELECT * FROM table2 WHERE _id = '" + rowId + "'");
dbHandler.getWritableDatabase().execSQL("UPDATE chosen SET _id = '" + newID + "' WHERE _id = '" + rowId + "'");
newID++;
x++;
}
cursor1.close();
cursor2.close();
这是我用来选择填充listView的数据
Cursor cursor = db.rawQuery("SELECT _id, value FROM chosen ORDER BY _id", null);
非常感谢任何帮助!
答案 0 :(得分:0)
想出来。我让它变得比它需要的更复杂。这是我最终做的事情:
<div class="parent">
<div class="first">Hello </div><div class="second"><a href="#">ManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManManMan</a>
</div>
</div>
确保所选表中的行id列是自动增量,并保持listview查询相同:
while (x <= 2) {
dbHandler.getWritableDatabase().execSQL("INSERT INTO chosen (value1, value2, value3) SELECT value1, value2, value3 FROM table1 ORDER BY RANDOM() LIMIT 1, null);
dbHandler.getWritableDatabase().execSQL("INSERT INTO chosen (value1, value2, value3) SELECT value1, value2, value3 FROM table2 ORDER BY RANDOM() LIMIT 1, null);
x++;
}
简单得多 - 而且效果很好!