Database error - gsub

Post Reply
naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Database error - gsub

Post by naftali »

Hey. I'm trying to create a database for the purpose of remembering room names/numbers and some other things. This is my first attempt at using databases, and right off the bat I seem to be getting an odd error:
Code: [show] | [select all] lua
local roomdb = db:create("AetoliaRooms",
	{
		rooms = {
			num = "",
			title = "",
			environment = "",
			exits = { { "", "" } },
			marks = { "" },
			_index = { "title" },
			_unique = { "num" }
		}
	}
)
trying to run that gives the following error:
Lua syntax error:/Users/Josh/.config/mudlet/db.lua:189: attempt to call method 'gsub' (a nil value)
I checked, and on line 189 of db.lua it does indeed call gsub. Should this be string.gsub? Is there an error in my code?

Using 1.0.6pre3. Feel free to close topic/mark as solved if this problem is fixed in the final version of 1.0.6

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

Re: Database error - gsub

Post by ixokai »

My first question is:

Code: Select all

                        exits = { { "", "" } },
                        marks = { "" },
What are you trying to do here?

You can't use tables as the default / data-type of a sheet column. It only supports basic types-- strings and numbers basically, though timestamps are sort of Special Numbers that it wraps.

I believe that's the problem here.

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: Database error - gsub

Post by naftali »

Drat. See, each room has exits, each of which lead to another room. I was hoping for the exits entry to contain a list of lists, where each sub-list has in it the direction of the exit (north, south, etc) and the number of the room it goes to. Would I just have to create entries for each direction, so that

Code: Select all

north = 12345,
east = false,
south = 45678,
west = false
would be what that part of the sheet looks like for a room with exits to the north and south?

Also, the other one was to contain info on any special attributes of the room, like whether it was a shop or something like that. I suppose for that I could just make that one string of comma-separated words.

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

Re: Database error - gsub

Post by ixokai »

There's a couple different ways you could approach the problem. One, is you could just create a north/south/east/west field as you show here-- that may be workable, if you're on a MUD with very consistant exit-naming.

Another would be to add a separate sheet, like:

Code: Select all

local roomdb = db:create("AetoliaRooms",
        {
                rooms = {
                        num = "",
                        title = "",
                        environment = "",
                        _index = { "title" },
                        _unique = { "num" }
                },
                room_exits = {
                        num = "",
                        exit = "",
                        target_num = "",
                        _index = { "num" },
                        _unique = { {"num", "exit"} },
               },
               room_attributes = {
                       num = "",
                       mark = ""
                      _index = {"num"},
                      _unique = { {"num", "mark"} },
               }
        }
)
Generally speaking, databases can only hold scalar values in a given field. If you want complex values, that's usually an indication that you actually want a w hole new table-- er, sheet-- and a relationship between the two sheets. In the above, the "num" field creates the relationship. Now, in real SQL, you'd create a foreign key and define certain protections to ensure said relationships are always valid. Like, "if I delete a room, all the exits and marks related to it should be deleted too." SQLite doesn't support that, and the db frontend doesn't add that on top-- so you have to be sure that if you do delete a room, you go clean up the related tables too.

Now, that said, I'm tempted to tweak db.lua to serialize tables into strings, and vice-versa, so you can store arbitrary table-ish-stuff in fields. But that would only work for basically simple tables (e.g., not things with metatables or weird custom types).

Daiger
Posts: 7
Joined: Sat Mar 13, 2010 8:16 pm

Re: Database error - gsub

Post by Daiger »

Trying to run this from an alias:

Code: Select all

local testsdb = db:create("tests", {vnums={vnum="", title="", _index = { "title" }, _unique = { "vnum" }}})
local results = db:get_database("tests")
db:add(results.vnums, {vnum=atcp.RoomNum, title=atcp.RoomBrief})
display(results)
gives me the following error:

Code: Select all

LUA: ERROR running script test (Alias29) ERROR:C:/Documents and Settings/myname/.config/mudlet/db.
lua:1046: Attempt to access sheet '__customtype'in db 'tests' that does not exist.

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

Re: Database error - gsub

Post by ixokai »

First, the result of db:get_database is a database instance. Calling it "results" is going to lead to a lot of confusion. display()'n it will lead to more-- that's not even supported.

Second, I think your whole problem is just that display. I rewrote it to debug as:

Code: Select all


local testsdb = db:create("tests", 
	{
		vnums={
			vnum="", 
			title="", 
			_index = { "title" }, 
			_unique = { "vnum" }
		}
	}
)

local testdb = db:get_database("tests")
db:add(testdb.vnums, {vnum="1234", title="My Test"})
And it works fine.

So, I think you're trying to accomplish something with that display which you can't do.

Do you intend:

Code: Select all

display(db:fetch(testdb.vnums))
Which will display the full contents of the database?

Daiger
Posts: 7
Joined: Sat Mar 13, 2010 8:16 pm

Re: Database error - gsub

Post by Daiger »

Ohh, so that was it. Thanks, I couldn't figure out why it wouldn't work - the display was just my attempt to test if it was working. -Now- it does. :)

Post Reply