title: Pagination with SQLite using LIMIT
body: <p>I'm writing my own SQLIteBrowser and I have one final problem, which apparently is quite often discussed on the web, but it doesn't seem to have a good general solution.</p>
  
  <p>So currently I store the SQL which the user entered. Whenever I need to fetch rows, I execute the SQL by adding "Limit n, m` at the end of the SQL.</p>
  
  <p>For normal SQLs, which I mostly use, this seems good enough. However if I want to use limit myself in the query, this will obviously give an error, because the resulting sql can look like this:</p>
  
  <pre><code> select * from table limit 30 limit 1,100
  </code></pre>
  
  <p>which is obviously wrong. Is there some better way to do this?</p>
  
  <p>My idea was, that I could scan the SQL and check if there is a limit clause already used and then ignore it. Of course it's not as simlpe as that, because if I have an sql like this:</p>
  
  <pre><code> select * from a where a.b = ( select x from z limit 1)
  </code></pre>
  
  <p>it obviously should still apply my limit in such a case, so I could scan the string from the end and look if there is a limit somehwere. My question now is, how feasable this is. As I don't know who the SQL parser works, I'm not sure if LIMIT has to be at the end of SQL or if there can be other commands at the end as well.</p>
  
  <p>I tested it with <code>order by</code>and <code>group by</code> and I get SQL errors if <code>limit</code> is not at the end, so my assumption seems to be true.</p>