android sqlite 随机查询数据,从SQLite数据库中获取10个随机记录(Get 10 Random Records From SQLite Database)...

从SQLite数据库中获取10个随机记录(Get 10 Random Records From SQLite Database)

我无法找到一个从sqlite数据库中获取10个随机记录的好方法。 看到一些与mysql一起使用的例子,但是即使我尝试使用Random()而不是rand(),它们似乎也不适用于sqlite。

我试图获取随机数,然后通过id获取记录,但由于某种原因,我得到的结果是其中几个。 如果有更好的方法,我很好奇。

另外作为旁注,这是我使用Ruby和Rails的第3天,所以还有点新鲜。

I haven't been able to find a very good way of getting 10 random records out of a sqlite database. Seen a few examples that work with mysql, but they don't seem to work well with sqlite even though I am trying to us Random() instead of rand().

I have tried to get random numbers and then get the records by id, but for some reason I am getting nil result for a couple of them. I was curios if there was a better approach.

Also as a side note this is day 3 for my using Ruby and Rails so still a bit new to it.

原文:https://stackoverflow.com/questions/3540354

更新时间:2019-10-14 18:58

最满意答案

怎么样

User.all.sort_by{rand}.slice(0,10)

这只是为了测试,对吗?

编辑 :不再是数据库独立的,但是,对于sqlite3:

User.find(:all, :order => "RANDOM()", :limit => 10)

对于mysql:

User.find(:all, :order => "RAND()", :limit => 10)

How about

User.all.sort_by{rand}.slice(0,10)

?

This is just for testing, right?

Edit: No longer database-independent, but, for sqlite3:

User.find(:all, :order => "RANDOM()", :limit => 10)

and for mysql:

User.find(:all, :order => "RAND()", :limit => 10)

2010-08-22

相关问答

你是否一次插入所有800个元素? 如果你是这样,在事务中插入会大大加速这个过程。 请参阅http://www.sqlite.org/faq.html#q19 SQLite可以处理非常大的数据库。 请参阅http://www.sqlite.org/limits.html Are you inserting all of the 800 elements at once? If you are, doing the inserts within a transaction will speed up

...

怎么样 User.all.sort_by{rand}.slice(0,10)

? 这只是为了测试,对吗? 编辑 :不再是数据库独立的,但是,对于sqlite3: User.find(:all, :order => "RANDOM()", :limit => 10)

对于mysql: User.find(:all, :order => "RAND()", :limit => 10)

How about User.all.sort_by{rand}.slice(0,10)

? This is

...

你检查过那个绑定的返回类型了吗? 它可能会返回错误代码(该语句仍然可能在此事件中执行,只是错误地执行)。 作为一种解决方法,您是否尝试过创建ZEROBLOB,然后使用SQLite文档中描述的BLOB I / O例程写入它? 这将是我在查看错误代码后尝试的下一件事。 I've found the perpetrator in this wild good chase... I was taking the value of column 1 instead of column 0, which wa

...

将数据库文件从assets文件夹复制到内部存储器,然后打开DB并从那里执行CRUD: private void copyDB() throws IOException{

InputStream myInput = m_Context.getAssets().open(DB-SQLITE_FILE_NAME_KEPT_IN_ASSET_FOLDER);

String outFileName = "/data/data/your.package.name/databa

...

表中的每一行都有一个唯一的ID ,您可以将其用于查询: SELECT

*

FROM

ContentMaster

WHERE

ContentAddedByUserID='%@'

AND

HiveletCode='%@'

ORDER BY

rowid DESC

LIMIT 10

Every row in a table has an unique ID, which you can use for your query: SELECT

*

FROM

...

似乎没有任何方法可以限制SmsProvider的结果。 然而,你不应该真的需要,因为你使用CursorAdapter并且不要尝试将内容读入你自己的数据结构。 你试过这个吗? There doesn't seem to be any way to limit your results from the SmsProvider. However you shouldn't really need to, given that you use CursorAdapter and don't try to

...

您可以从临时表中多次查询相同的随机集,如下所示: create temporary table tmp AS select id from tableQ ORDER BY random() LIMIT 20;

Select id from tmp;

Select id from tmp;

drop table tmp

You can query the same random set multiple times from a temporary table like this: create

...

getView方法是在需要呈现屏幕时调用的方法。 所以getView方法可以被调用多次。 出于这个原因,你应该在这个方法之外进行数据库操作。 例如在你的活动课上: ...

contactListView (ListView)findViewById(R.id.lv_contacts_container);

contactList = db.getAllContacts();

adapter = new ContactListViewAdapt

...

它不会删除记录,因为您只是准备查询,而不是执行它。 要执行准备好的查询,必须调用sqlite3_step 或者,您可以使用sqlite3_exec ,它是执行准备,步骤和最终化的便捷函数。 It's not deleting records because you're only preparing the query, never executing it. To execute a prepared query, you must call sqlite3_step Alternatively

...

尝试这样的事情 $prices, $index_of_price = [], 0

#index_of_price is the index of the field in your result

def select(item_code)

begin

db = SQLite3::Database.new "test.db"

results = db.get_first_row "SELECT * FROM Products WHERE Product_code = ?"

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值