cubesoft cubesoft - 2 months ago 21
Java Question

How to create sqlite prepared statement in OrmLite?

Is it possible to create a sqlite prepared statement in OrmLite?
If so, how to bind the query values which may change across different queries.

Answer

Is it possible to create a sqlite prepared statement in OrmLite?

You need to RTFM since ORMLite's online documentation is pretty extensive. If you look in the index for "prepared statement" you find out about the QueryBuilder which @Egor pointed out.

how to bind the query values which may change across different queries.

A little further in that section you learn about select arguments which is how you bind query values that change across queries. This is in the index under "arguments to queries".

To quote from the docs here's how you prepare a custom query:

QueryBuilder<Account, String> queryBuilder = dao.queryBuilder();
Where<Account, String> where = queryBuilder.where();
SelectArg selectArg = new SelectArg();
// define our query as 'name = ?'
where.eq("name", selectArg);
// prepare it so it is ready for later query or iterator calls
PreparedQuery<Account> preparedQuery = queryBuilder.prepare();

When you are ready to run the query you set the select argument and issue the query:

selectArg.setValue("foo");
List<Account> accounts = dao.query(preparedQuery);

Later, you can set the select argument to another value and re-run the query:

selectArg.setValue("bar");
accounts = accountDao.query(preparedQuery);