luasql in mudlet

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

Re: luasql in mudlet

Post by Skylark »

It describes how to add tables to a database, yes. However, that's not really what I'm asking, I should have explained myself. Like tsuujin, I take in a large number of names separated by commas as a string. I then use the split function to create a table with all the names in it. It's then easy to modify this table so that each entry is of the name="person" form, so the end result is

persontable={name="Abby", name="Bob", name="Carlos", name="Dylan"}

I could then do a db:add with persontable[1], persontable[2], etc. The problem is that the number of names I capture will vary, and so I don't know how to write a script that will db:add only the number of times that there are entries in persontable.

Edit: This is easy to do with multiple db:adds, but that's exactly what I'm trying to avoid due to speed issues

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

Re: luasql in mudlet

Post by Vadi »

db:add(mydb, persontable) doesn't work? As the example shows, you can pass it multiple rows to be added at once, not just per-row.

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

Re: luasql in mudlet

Post by Skylark »

If I do that then only the last name is added to the database- or at least it's the only one I can find when I query it. Perhaps the others are being overwritten?

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

Re: luasql in mudlet

Post by tsuujin »

Skylark wrote:If I do that then only the last name is added to the database- or at least it's the only one I can find when I query it. Perhaps the others are being overwritten?
Possible, but impossible for us to know without knowing the DB structure and the exact commands you're sending.

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

Re: luasql in mudlet

Post by Skylark »

I've been using an alias to test Vadi's suggestion out:

Code: Select all

local mydb = db:get_database("people")
local persontable= {name="Abby", name="Bob", name="Carlos"}
db:add(mydb.folks, persontable)
Then to see if it worked, I use another alias:

^find (\w+)$

Code: Select all

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 find alias is working fine, or at least it has been in the past. However, if I attempt to find Abby or Bob after running the first alias to input the data, they don't appear to exist. Carlos appears normally. Anytime I try something of this form only the last name is added to the database.

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

Re: luasql in mudlet

Post by tsuujin »

what's the database structure? Got uniques, or primary keys?

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

Re: luasql in mudlet

Post by Skylark »

db:create("people", {folks={name="", city="", enemied=0, infamous=0, mark=0, watching=0, class="", _unique= {"name" }, _violations="IGNORE" }})

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

Re: luasql in mudlet

Post by tsuujin »

Might have to do with the DB wrapper then. I honestly don't use it, so I'm not familiar with it's issues. If the code is supposed to look the way you have it written, I don't see a problem with the structure.

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

Re: luasql in mudlet

Post by Vadi »

Code: Select all

local persontable= {name="Abby", name="Bob", name="Carlos"}
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.

As per the example of:
Code: [show] | [select all] lua
 db:add(mydb.friends,
                {name="Ixokai", city="Magnagora"},
                {name="Vadi", city="New Celest"},
                {name="Heiko", city="Hallifax", notes="The Boss"}
        )
Use

Code: Select all

local persontable= {{name="Abby"}, {name="Bob"}, {name="Carlos"}}

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

Re: luasql in mudlet

Post by Skylark »

Thanks for the pointers, that was incredibly silly of me.

Unfortunately, attempting to add that table as suggested earler,

Code: Select all

local mydb = db:get_database("people")
local persontable= {{name="Abby"}, {name="Bob"}, {name="Carlos"}}
db:add(mydb.folks, persontable)
..results in this error:
Failed to add item: this is probably a violation of a UNIQUE index or other constraint.

Post Reply