Select a Random row in realm table - android

I want to select a random row from the realm table. Something like -
SELECT * FROM table ORDER BY RANDOM() LIMIT 1;

Something like this would do, yes?
Random random = new Random();
RealmResults<YourTable> list = realm.where(YourTable.class).findAll();
YourTable yourTable = list.get(random.nextInt(list.size()));

Depends on what you want to do:
Do you want to get a random row from a table?
Or A (random) row from a random table?
I guess you mean the former:
If you have an id in your table, you could just:
SELECT * FROM table b WHERE id = FLOOR(RAND() * (3 - 0 + 1)) + 0
You should place a min and max here like so:
FLOOR(RAND() * (<max> - <min> + 1)) + <min>
(as found here)

SWIFT 5
I do it this way and it works perfect:
let results = realm.objects(MyObject.self) // Get all the objects
let randomIndex = Int.random(in: 0 ..< results.count) // Get a random number within the number of objects returned
let randomObject = results[randomIndex] // Get a random object

Here's how I do it in .NET, takes 7ms for 70K entries.
public IEnumerable<Entry> GetRandomEntries()
{
var randomizer = new Random();
var entries = GetRealmInstance().All<Entry>().AsEnumerable();
// Takes random entries and shuffles them to break the natural order
var randomEntries = entries
.OrderBy(x => randomizer.Next(0, DbQueryRandomEntriesLimit))
.Take(DbQueryRandomEntriesLimit)
.OrderBy(entry => entry.GetHashCode());
return randomEntries;
}
I use AsEnumerable() to allow using .Select in LINQ

String query =String.format("SELECT * FROM realm ORDER BY %d LIMIT 1", random());
databaseHelper = new DatabaseHelper(this);
database = databaseHelper.getWritableDatabase();
Cursor cursor = database.execSQL(query);
It works assuming that you have a class DatabaseHelper which extends SQLiteOpenHelper

Related

SQL search multiple values in same field

I have a list of id's to search.
I want to call all data matching the values in this list.
Matching data is returned as a list.
I tried to do this in a loop and using IN but it didn't work.
When I write the values in parentheses one by one, it gives results, but how can I give the data in the list as a parameter?
db.query(_tableName, where: "id IN ('0001','00002','00003')")
When I type in the form, the query takes place. Can I send the incoming array directly into parentheses, not one by one? For example,
db.query(_tableName, where: "id IN (_ids)")
Query method:
Future<List<ContentModel>> getDiscoverContent(List<String> _ids) async {
Database db = await instance.database;
var contents = await db.query(_tableName, where: "id IN ('')");
List<ContentModel> _recommendedList = contents.isNotEmpty
? contents.map((e) => ContentModel.fromMap(e)).toList()
: [];
return _recommendedList;
}
The simplest solution I found:
var contents = await db.query(_tableName,
where:
"id IN('${_ids[0]}','${_ids[1]}','${_ids[2]}','${_ids[3]}','${_ids[4]}',"
"'${_ids[5]}','${_ids[6]}','${_ids[7]}','${_ids[8]}','${_ids[9]}')");
If you have a different solution, feel free to tell me.
You may try like this. This worked for my use case.
List<int> _ids = [1, 2, 3, 4];
// Loop through the _ids list and append quotes to each id
// add the element into a new List called new_Ids
List<String> new_Ids = [];
for (var i = 0; i < _ids.length; i++) {
new_Ids.add("'" + _ids[i].toString() + "'");
}
// use join to make a string from the new list where ids are separated by comma
String ids_Str = new_Ids.join(',');
Use db.query and pass ids string in where itself. Other columns in where condition can be passed in whereArgs if you need.
var resultList = await db.query('your_table_name',
columns: ['id', 'column2'],
where: 'id IN ($ids_Str)');
OR use rawQuery
var resultList = await db.rawQuery("SELECT id, column2 FROM your_table_name WHERE id IN(" + ids_Str + ")");
You may need to be careful of SQL Injection as we are passing dynamic values. Not sure about the best practices here.

Is it possible to 'randomly' select a row from a table using ormlite?

I would like to select a row from a table, but I only want to get one row and the row will be selected randomly.
I would like to select a row from a table, but I only want to get one row and the row will be selected randomly.
There isn't any special method calls which do this but there are ways that you can use the QueryBuilder to accomplish this.
If you have a numerical id, something like like should work:
qb = dao.queryBuilder();
qb.where().raw("id >= (ABS(RANDOM()) % MAX(id) + 1)");
entity = qb.queryForFirst();
Sqlite RANDOM() returns a large positive or negative integer. ABS() makes it just positive, MAX(id) allows us to then mod by the maximum id value. The +1 allows us to actually choose the max id row.
You can use queryRaw:
Relation result = null;
String query = "SELECT * from relations ORDER BY RANDOM() LIMIT 1";
try {
result = queryRaw(query, getRawRowMapper()).getFirstResult();
} catch (SQLException e) {
e.printStackTrace();
}
What I actually did was to get the List<> of the selected items from the database and then use a Random object, using the size() of the return list as the maxInt. And yes I did check for nulls.
Dao dao = getHelper().getObjectDao();
PreparedQuery<Object> query = dao.queryBuilder().[query].prepare();
Object object = null;
List<Object> resultList = dao.query(query);
if(resultList != null && resultList.size() > 0){
Random random = new Random();
int selected = random.nextInt(resultList.size() - 1);
object = resultList.get(selected);
}
It works for me:
dao.queryBuilder().orderByRaw("RANDOM()").queryForFirst();

SQL MAX-MIN in ORMLITE - ANDROID

I want to know how can i use MAX MIN command with ORMLITE.
For example lets say we have this table
Table Name = Example
Column 1 = id
Column 2 = name
In ORMLITE how can i get max id ? I looked here but i didnt't understand exactly..
Can someone show me example about Max min in ORMLITE ?
QueryBuilder<Account, Integer> qb = accountDao.queryBuilder();
qb.selectRaw("MIN(orderCount)", "MAX(orderCount)");
// the results will contain 2 string values for the min and max
results = accountDao.queryRaw(qb.prepareStatementString());
String[] values = results.getFirstResult();
I found this from documentation
This is how I query for max ID in my code:
QueryBuilder<Example, String> builder = dao.queryBuilder();
builder.orderBy("id", false); // true or false for ascending so change to true to get min id
Example example = dao.queryForFirst(builder.prepare());
String id = null;
if (example == null)
id = "-1";
else
id = example.getId();
A couple of alternative answers can also be found here:
ORMLite - return item w/ maximum ID (or value)
You can use:
dao.queryRawValue("select MAX(columnName) from tableName")
It will directly return long value.
refer: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_5.html#DAO-Methods
queryRawValue(String query, String... arguments)
Perform a raw query that returns a single value (usually an aggregate function like MAX or COUNT). If the query does not return a single long value then it will throw a SQLException.

Ormlite orderBy max(ColumnName)

Hi I need to use order by max(columnName) in ORMLite. I have the SQL query but I need to know how this query is used. This is my query:
SELECT * FROM table where place = 'somePlace' group by name
order by MAX (statusDate)
statusDate column contains date in "yyyy-dd-mm" format. The result I got is the list with recentDates.
Use a query builder, and function where and orderBy to preoceed
QueryBuilder<YourObject, Integer> q = yourDaoObject.queryBuilder();
Where<YourObject, Integer> wh = q.where();
wh.eq("place", "some_place");
q.orderBy("statusDate", false);
List<YourListOfObects> yourList = q.query();
But before that you should store a long instead to store your Date https://stackoverflow.com/a/6993420/2122876
i got same names with different dates and i need only the recent date.
If you are trying to get element from Table with the maximum statusDate then you should be doing an descending order-by with a limit of 1. Something like:
QueryBuilder<Foo, Integer> qb = fooDao.queryBuilder();
qb.where().eq("place", "some_place");
qb.orderBy("sttusDate", false); // descending sort
// get the top one result
qb.limit(1);
Foo result = qb.queryForFirst();
I did something like this. Please create your own query builder on the first line.
QueryBuilder<MyRowObject, Integer> queryBuiler = "Get Query builder" //getDaoXXX().queryBuilder();
MyRowObject firstLatestRow = queryBuiler.orderBy("dateColoumn", false).queryForFirst();
Hope this helps

How can I reset a autoincrement sequence number in sqlite

How to update table sqlite_sequence in Ormlite ? I just need update seq. How can I get that table via ORMLite ?
EDIT
I can't find ORLite tool to do this, so instead I use simple sqlite query. In my class extends OrmLiteSqliteOpenHelper I use SQLiteDatabase to make that update.
EDIT2 ;)
In my project I persist class Lesson and class WeekDefinition.
class Lesson{
#DatabaseField(generatedId=true)
private int id;
...
}
class WeekDefinitions{
#DatabaseField(generatedId=true)
private int id;
#DatabaseField(foreign=true, columnName="lesson_id")
private Lesson lesson;
...
}
Now , when I add new lessons, id is increment. For example
id = 1 Math
id = 2 English
id = 3 Medicine
and in weekDefinition :
id = 1 lesson_id = 1 nr = 20
id = 2 lesson_id = 1 nr = 22
id = 3 lesson_id = 2 nr = 32
...
id = 12 lesson_id = 3 nr = 3
SQLite add this row into sqlite_sequence ( when use autoincrement )
rowId = 1 name = lesson seq = 3
rowId = 2 name = weekDefinition seq = 12
Now, I delete all rows from tables Lesson and WeekDefinition. Lesson and WeekDef are empty after that, but sqlite_sequence is still the same. And this is problem because id in table lesson start from value 4 ( seq from sqlite_sequence for lesson and add 1 ) :
id = 4 Math
id = 5 English
id = 6 Medicine
and weekDefinition
id = 13 lesson_id = 1 nr = 20
id = 14 lesson_id = 1 nr = 22
id = 15 lesson_id = 2 nr = 32
and for lesson id = 4 , Math i should get weekDefinitios, but in weekDefinitions lessons_id has value only from 1 to 3
And this is my problem. I need "reset" sqlite_sequence table ( or there is better solution ?)
Building on Marcos Vasconcelos' answer:
UPDATE sqlite_sequence SET seq = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"
This query will set seq to the largest value in the col identity column in the Tbl table, so there is no risk of violating constraints.
Inside your .db file there's an table called sqlite_sequence
Each row has two columns
name which is the name of the table
seq a integer indicating the current last value at this table
You can update it to 0
But beware if your table use this id as the unique identifier.
UPDATE SQLITE_SEQUENCE SET SEQ= 'value' WHERE NAME='table_name';
If you want to issue general database commands in ORMLite, you can use the updateRaw method. See the javadocs. There is also executeRaw for other commands.
lessonDao.updateRaw("delete from 'lesson';");
lessonDao.updateRaw("delete from sqlite_sequence where name='lesson';");
weekDefinitionDao.updateRaw("delete from 'weekdefinition';");
weekDefinitionDao.updateRaw(
"delete from sqlite_sequence where name='weekdefinition';");
You could also drop and recreate the table as well:
TableUtils.dropTable(WeekDefinition.class);
TableUtils.dropTable(Lesson.class);
TableUtils.createTable(Lesson.class);
TableUtils.createTable(WeekDefinition.class);
I think the real question is why is your application depending on this database internal number? It really shouldn't care.
How about not displaying the number at all so it can be 1 or 1001 and your application won't matter?
You could also never remove the lessons at all but maybe add a hidden boolean field. So if they get re-added, the hidden field could be set to false and Math would still be at id #1.
This worked for me in my database : (I Set the id before one, then after deleting one row, when I add new data row again, the auto increment serial remain ok ) :
public void updateSerialNumber ( long memberId){
String query = "UPDATE SQLITE_SEQUENCE SET SEQ= '"+(memberId-1)+"' WHERE NAME='"+ReportBigHelper.TABLE_MEMBER+"'";
database.execSQL(query);
}

Categories

Resources