luasql in mudlet

ixokai
Posts: 63
Joined: Fri Dec 25, 2009 7:43 am

Re: luasql in mudlet

Post by ixokai »

Basically (I had a longer post explaining details :( but Chrome crashed), db:add takes a variable number of arguments. The first is the sheet to add a record to; each successive is one individual record.

It does not take a "array like table" containing a set of records to add at once. But! Lua to the rescue :) Lua has a built in function 'unpack' which converts a table like array into actual arguments, so:

Code: Select all

db:add(mydb.folks, unpack(persontable))
Should be what you're looking for.

This API is a bit.. fuzzy, and I apologize for that ;-) Lua's single composite data structure and the desire to shield as much true SQL from the masses who weren't interested in learning, led to a lot of .. judgement calls on my part, to try to decide what was Easiest for the Most Common Usage, while still Easy Enough for The Rest. Not all were necessarily perfect :)

Skylark
Posts: 46
Joined: Mon Feb 22, 2010 12:38 am

Re: luasql in mudlet

Post by Skylark »

Excellent, thanks. You all have been very helpful, and that works perfectly.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: luasql in mudlet

Post by tsuujin »

Vadi wrote:Lua doesn't allow duplicate table values. Has nothing to do with db at all here. You create a table, then overwrite name Abby with Bob and then with Carlos. So you end up with a table of 1 element.
I caught that last night, then left the window open and went to bed without saving. Heh.

User avatar
Vadi
Posts: 4867
Joined: Sat Mar 14, 2009 3:13 pm

Re: luasql in mudlet

Post by Vadi »

No worries :D

kkerwin1
Posts: 22
Joined: Tue Apr 20, 2010 7:37 am

Re: luasql in mudlet

Post by kkerwin1 »

Ok. I used two aliases, as was advised elsewhere.

First, init:

Code: Select all

echo( "Initializing.\n")
db:create("people", {folks={"name"}})
local mydb = db:get_database("people")
local persontable= {{name="Abby"}, {name="Bob"}, {name="Carlos"}}
db:add(mydb.folks, unpack(persontable))
And then, "^find (\w+)$":

Code: Select all

echo("Querying " .. matches[2] .. ".\n")
local mydb = db:get_database("people")
local searching = capitalize(matches[2])
local results= db:fetch(mydb.folks, db:eq(mydb.folks.name, searching))
display(results)
The only output that I got was:
Initializing.
Querying Abby.
No results to speak of.

I am on Linux x86-64bit, Ubuntu.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: luasql in mudlet

Post by tsuujin »

Open a debug window (click on triggers, then debug is a ladybug icon on the lefthand side).

Run the aliases with that window open, and check for errors, post those here.

kkerwin1
Posts: 22
Joined: Tue Apr 20, 2010 7:37 am

Re: luasql in mudlet

Post by kkerwin1 »

Wow. Had never noticed the debug console. That's a huge help. Here's the output from the first command, init (see above for the code):

Code: Select all

Alias name=Init DB(^init$) matched.
Alias: capture group #1 = <init>
LUA: ERROR running script Init DB (Alias2) ERROR:/home/kris/.config/mudlet/db.lua:517: Failed to 
add item: this is probably a violation of a UNIQUE index or other constraint.Alias name=query 
And the second, from "^find (\w+)$"; which was passed "Abby":

Code: Select all

db(^find (\w+)$) matched.
Alias: capture group #1 = <find Abby>
Alias: capture group #2 = <Abby>
LUA: ERROR running script query db (Alias3) ERROR:[string "function Alias3()..."]:4: attempt to 
call global 'capitalize' (a nil value)

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: luasql in mudlet

Post by tsuujin »

well, on the second part, you're trying to call a function (capitalize) that doesn't exist. Did you pull it from someone elses system?

As for the data insertion: it's returning some error on trying to add the record. I don't use the db wrapper myself, so I can't tell for certain what the error is there because it looks ok. My question would be: is the unpack method supported? Are you certain that's how they want you to add records?

Try
[syntax=lua]
echo( "Initializing.\n")
db:create("people", {folks={"name"}})
local mydb = db:get_database("people")
db:add(mydb.folks, {name="Abby"}, {name="Bob"}, {name="Carlos"})
[/syntax]

Also, is "get_database" the correct term? The mudlet manual shows "fetch_database"

kkerwin1
Posts: 22
Joined: Tue Apr 20, 2010 7:37 am

Re: luasql in mudlet

Post by kkerwin1 »

Ok. Output is now:

Code: Select all

Alias name=Init DB(^init$) matched.
Alias: capture group #1 = <init>
LUA: ERROR running script Init DB (Alias2) ERROR:/home/kris/.config/mudlet/db.lua:517: Failed to 
add item: this is probably a violation of a UNIQUE index or other constraint.Alias name=query 
db(^find (\w+)$) matched.
Alias: capture group #1 = <find Abby>
Alias: capture group #2 = <Abby>
LUA: ERROR running script query db (Alias3) ERROR:[string "function Alias3()..."]:3: attempt to 
call method 'fetch_database' (a nil value)
Using the following code for init:

[syntax="lua"]
echo( "Initializing.\n")
db:create("people", {folks={"name"}})
local mydb = db:get_database("people")
db:add(mydb.folks, {name="Abby"}, {name="Bob"}, {name="Carlos"})
[/syntax]

And find (\w+):

[syntax="lua"]
echo("Querying " .. matches[2] .. ".\n")
local mydb = db:fetch_database("people")
--local searching = capitalize(matches[2])
local results= db:fetch(mydb.folks, db:eq(mydb.folks.name, matches[2]))
display(results)
[/syntax]

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: luasql in mudlet

Post by tsuujin »

yeah, fetch_database is wrong, and should be corrected in the manual. Go back to get_database

Post Reply