Binding SQLite3 parameters

Posted by Artel on Mon 10 Oct 2016 08:36 PM — 3 posts, 16,293 views.

USA #0
Is there a MUSHclient database function that binds values to a SQLite prepared statement? I.e., something that corresponds to the sqlite3_bind_*() functions in the SQLite3 C api.

Here are the other related methods in MUSHclient (from https://www.gammon.com.au/forum/?id=9262). I was expecting to find a DatabaseBind(BSTR Name, ...) or similar, but I can't find it.

long DatabasePrepare(BSTR Name, BSTR Sql);
long DatabaseStep(BSTR Name);
long DatabaseFinalize(BSTR Name);
long DatabaseReset(BSTR Name);
Australia Forum Administrator #1
You can do it with the Lua SQLite3 interface rather than the generic one. The generic interface would be fiddly to implement because of the different data types, but in Lua you can pass different types down to a function.


http://www.gammon.com.au/scripts/doc.php?lua=stmt:bind

http://www.gammon.com.au/scripts/doc.php?lua=stmt:bind_names

http://www.gammon.com.au/scripts/doc.php?lua=stmt:bind_values
USA #2
Thanks! That's working perfectly.

Edit: added example snippet for reference


  local target_query = 'thalos'
  local select_statement = assert(db:prepare([[SELECT keyword FROM areas
    WHERE keyword LIKE ?]]))

  select_statement:bind_values(target_query)

  for row in select_statement:nrows() do
    -- do stuff
  end