[odb-users] Constructing native queries without a where clause fails (sqlite)

Feisal Ahmad feisal.ahmad at ps-tech.com
Thu Mar 13 16:50:19 EDT 2014


Hello all,

I've been trying to implement the following query type to work, without 
success:

SELECT * FROM table LIMIT 10 OFFSET 10;

This is what my relevant code looks like to construct the query 
statically for sqlite only:

typedef odb::result<T> result;
typedef odb::query<T> query;
std::vector<T> out;

try
{
	boost::shared_ptr<odb::transaction> t = beginTransaction();

	if(m_db->id() == odb::id_sqlite)
	{
		// Sqlite LIMIT clause implementation
		typedef odb::sqlite::query<T> sq_query;
		odb::sqlite::database& sq_db (static_cast<odb::sqlite::database&> 
(*m_db));
		sq_query selectAll;
		result r = m_db->query<T>(selectAll + " LIMIT " + 
sq_query::_val(maxResults) + " OFFSET " + sq_query::_val(first));
		for (result::iterator i (r.begin ()); i != r.end (); ++i)
		{
			out.push_back(*i);
		}
	}
	else
	{
		...
	}
	t->commit();

	return out;
}
catch(odb::exception& e)
{
	std::cerr << "Exception: " << e.what() << std::endl;
	throw;
}

The following exception is thrown when the query is made: 1: near 
"LIMIT": syntax error

If I add a clause to the query, such as id > 0, the limit clause works 
fine...

Does anyone have an idea what is going wrong, and how I can fix this?

Also, any help on converting the constructed query to a string for 
debugging would be very welcome.

Greetings,
Feisal



More information about the odb-users mailing list