local mydb = db:HELP ME!!

Yue
Posts: 8
Joined: Wed Nov 21, 2012 7:18 am

local mydb = db:HELP ME!!

Post by Yue »

I created a database in a script called 'database':
Code: [show] | [select all] lua
db:create("worldstock", {
	elixirs={
		type="",
		shop="",
		cost=0
	},
	tonics={
		type="",
		shop="",
		cost=0
	},
	herbs={
		type="",
		shop="",
		cost=0
	},
	minerals={
		type="",
		shop="",
		cost=0
	},
	inks={
		type="",
		shop="",
		cost=0
	},
	sigils={
		type="",
		shop="",
		cost=0
	},
	bombs={
		type="",
		shop="",
		cost=0
	},
	enchantments={
		type="",
		shop="",
		cost=0
	}
})
Then I created a trigger to update the elixirs section:
Code: [show] | [select all] lua
-- patern: ^\s+tun\d+ an elixir of (\w+) \(refill only\)\s+\d+\s+(\d+)gp$

updateElixir(matches[2],mmp.currentroomname,matches[3])
In my main script, I have two functions defined:
Code: [show] | [select all] lua
function updateElixir (name,where,price)
local mydb = db:get_database("worldstock")
db:add(mydb.elixirs, {type=""..name.."", shop=""..where.."",cost=price})
end

function listElixirs()
local mydb = db:get_database("worldstock")
local dbResults = db:fetch(mydb.elixirs)
display(dbResults)
end
I know the trigger fires, because I had it echo me the results. So I assume everything is working, except my alias, DBLIST ELIXIRS, which calls listElixirs(), does not produce any results. Can someone possibly diagnose from what is given where my problem lies? Thanks so much for any help!

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

Re: local mydb = db:HELP ME!!

Post by Vadi »

There's something wrong with db:add atm - I've reliably been using db:merge_unique instead. Just wrap what you want to add in a table, since it can take in multiple results unlike a single one as db:add.

Yue
Posts: 8
Joined: Wed Nov 21, 2012 7:18 am

Re: local mydb = db:HELP ME!!

Post by Yue »

Can you show me what that would look like? I thought what I'm adding is already in a table.

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

Re: local mydb = db:HELP ME!!

Post by Vadi »

db:merge_unique(mydb.elixirs, {{type=""..name.."", shop=""..where.."",cost=price}}) ,but I didn't check if anything else is wrong.

Yue
Posts: 8
Joined: Wed Nov 21, 2012 7:18 am

Re: local mydb = db:HELP ME!!

Post by Yue »

Can I pass a whole table to db:merge_unique? the table has 9 subtables, each of which have key/value entries of 1/table, 2/table, 3/table, etc.. with the tables containing data like this, "location = "The Twelve Tun Hub", itemname="Star tarot", groupsize="10", cost="5000".

like:

db:merge_unique(mydb, myhugetable)

would that work?

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

Re: local mydb = db:HELP ME!!

Post by Vadi »

Yes, sounds right if I'm getting you correctly. It should translate to db:merge_unique(mydb.field, {{location = "The Twelve Tun Hub", itemname="Star tarot", groupsize="10", cost="5000"}, location = "asdsd", itemname="Star tarot", groupsize="10", cost="5034343400"}}) and so on.

Yue
Posts: 8
Joined: Wed Nov 21, 2012 7:18 am

Re: local mydb = db:HELP ME!!

Post by Yue »

maybe.. here's what the table looks like

myhugetable = {}
myhugetable.elixirs = {}
myhugetable.inks = {}
myhugetable.bombs = {}
etc.
etc.

and entries, in each subtable, are added (as tables) with table.insert so:

display(myhubetable.bombs[1]) --> {location="The Twelve Tun Hub", item="web bomb", groupsize="10", cost="400"}

Yue
Posts: 8
Joined: Wed Nov 21, 2012 7:18 am

Re: local mydb = db:HELP ME!!

Post by Yue »

If you have a nifty way to save tables (with subtables that have subtables) to disc in any format like yaml/xml that can loaded into a database and operated on, I would be happy with that solution to. Otherwise, I'm stuck trying to get mudlet's database to work and then hopefully I'll be able to SQL my way into some useful analysis.

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

Re: local mydb = db:HELP ME!!

Post by Vadi »

You don't have to use db: to use databases, it's just an easier layer to use them without knowing SQL. You can use LuaSQL's sqlite driver that's available within Mudlet directly. See also tsujinn's db containers that do some light wrapping for you and otherwise allow direct SQL.

Yue
Posts: 8
Joined: Wed Nov 21, 2012 7:18 am

Re: local mydb = db:HELP ME!!

Post by Yue »

nifty.. I assume, though, that db:merge_unique(mydb, myhugetable) is still going to work after giving you a better idea of how the table is structured, since you didn't say otherwise?

Post Reply