Database Help

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Database Help

Post by icesteruk »

For some reason, I made a database but seems it not 'saving' the information, Could someone point me where I'm going wrong.

the creating of the database
Code: [show] | [select all] lua
db:create("playerstatus", {
	players = {
		name = "",
		status = "",
		class = "",
		_unique = { "name" },
		_violations = "REPLACE"
	},
})

mayhem.playerstatus.db = db:get_database("playerstatus")
setStatus()
Code: [show] | [select all] lua
function setStatus(player, status)
	echo(player:title() .. " set to " .. status:title())
	local Uname = player:title()
	local class = getClass(player)
	db:merge_unique(mayhem.playerstatus.db.players, {name=Uname, status=status:lower(), class=class})
end
getStatus
Code: [show] | [select all] lua
function getStatus(player)
	local Uname = player:title()
	local results = db:fetch(mayhem.playerstatus.db.players, db:eq(mayhem.playerstatus.db.players.name, Uname))
	if not results[1] then
		return "Unknown"
	else
		return results[1].status:title()
	end
	return "Unknown"
end
So, if I do, lua setStatus("Batman", "undead") it echos - Batman set to Undead - to show its working right?

but when I do, display(mayhem.playstatus) it shows

{
db = {
_db_name = "playerstatus",
players = {
_sht_name = "players",
name = {
database = "playerstatus",
type = "string",
name = "name",
sheet = "players"
},
_db_name = "playerstatus"
}
}
}

Any help or advise will be greatly appreciated

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Database Help

Post by keneanung »

You can't do a display on a database like that as the data is written to a file and not stored in lua tables. What you can do is
Code: [show] | [select all] lua
result = db:fetch(mayham.playerstatus.db.players)
display(result)
Or use an external Program to open the SQlite file in the profile folder.

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: Database Help

Post by icesteruk »

well I also have a listStatuses() which shows but nothing is being shown
Code: [show] | [select all] lua
function listStatuses()
	cecho("\n<white>        Known Statuses\n<red>==================================")
	local results = db:fetch(mayhem.playerstatus.db.players)
	table.sort(results, function(a,b) return a.name < b.name end)
	cecho("\n<red>Name" .. string.rep(" ", 11) .. "Status" .. string.rep(" ", 4) .. "Class")
	for _,row in ipairs(results) do
		cecho("\n<white>" .. row.name:title() .. string.rep(" ", 15-#row.name))
		cecho("<" .. (temp.statuscolor[row.status:lower()] or "orange") .. ">" .. row.status:title() .. string.rep(" ", 10-#row.status))
		cecho("<" .. (temp.classcolor[row.class:lower()] or "orange") .. ">" .. (row.class == "" and "unknown" or row.class):title())
	end
	cecho("\n<red>==================================")
end
like the database isnt saving the information and isnt giving me no errors

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Database Help

Post by keneanung »

Looking at the documentation a tad closer and into the code of the merge_unique function, I tink I found the issue: merge_unique expects a table of database entries to merge, not a single one. If you change your merge_unique call to
Code: [show] | [select all] lua
db:merge_unique(mayhem.playerstatus.db.players, {{name=Uname, status=status:lower(), class=class}})
you should be fine. (Mind the extra curly braces around the database entry).

Generally speaking, it's always a good idea to do confirmatory echoes after the action is done. That way the the code is aborted before you get the impression that everything worked. (Even though it would not have helped here.)

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: Database Help

Post by icesteruk »

still not working :/ Guess I'll go back to making it a variable! Thanks for the help

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Database Help

Post by keneanung »

The following setStatus function works for me:
Code: [show] | [select all] lua
function setStatus(player, status)
        echo(player:title() .. " set to " .. status:title())
        local Uname = player:title()
        local class = getClass(player)
        db:merge_unique(mayhem.playerstatus.db.players, {{name=Uname, status=status:lower(), class=class}})
end
I ran exactly your examples (with getClass always returning "Test" and temp.statuscolor being an empty table).

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: Database Help

Post by icesteruk »

object:<run lua code> function:<Alias5>
<./mudlet-lua/lua/DB.lua:1096: attempt to index local 'field' (a nil value)>



is error im getting :/ when I run listStatuses() ...

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Database Help

Post by keneanung »

Try updating you db module like vadi outlined in http://forums.mudlet.org/viewtopic.php? ... 270#p19825 we did quite a number of fixes to the module since the last update.

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: Database Help

Post by icesteruk »

I did, thats when I started to get the error, before updating I didn't get no errors..

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Database Help

Post by keneanung »

According to the error message you pasted it uses he build in DB.lua. And the most current DB.lua file has an "end" at line 1096 and no reference to a "field".

That's where my confusion stems from.

Post Reply