bind_names Database function

Posted by Bast on Thu 25 Jun 2009 07:45 PM — 2 posts, 12,707 views.

#0
In lua, using the sqlite3 stuff, you can do something like this


db = assert(sqlite3.open("somedb.db"))

db:exec([[CREATE TABLE mobs(
        mob_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        mobname TEXT,
        mobarea TEXT
      )]])

t = {
   {name = "Some mob", room = "Some room"},
   {name = "Another mob", room = "Another room"},
}
stmt = db:prepare("INSERT INTO mobs VALUES (NULL, :name, :room)")

for i,v in ipairs(t) do
  stmt:bind_names(v)
  stmt:step()
  stmt:reset()
end
  
stmt:finalize()


This makes it easy to write a for loop to insert a lua table into a sql table without having to rebuild the insert string for each new item. I know the database API for MUSHclient includes a prepare, step, reset, and finalize. Is there an equivalent way to bind_names in the API or could it be added?

Thanks,
Bast
Amended on Thu 25 Jun 2009 07:47 PM by Bast
Australia Forum Administrator #1
There isn't, partly because it is available in Lua, and partly because I thought in other languages you could achieve the same effect by simply using the equivalent to Lua's string.format (to make up a statement at runtime), or just concatenating values together.