Mudlet Databases

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Mudlet Databases

Post by Rakon »

I see that Mudlet includes an implementation of LuaSQL, which has support for sqlite3 databases.

How would I go about connecting to a previously created sqlite3 database, or importing the data from it, to a Mudlet compatible version?

Example, I have a sqlite3 database created with python and sqlite libs, called 'shop.db' . I want to be able to access this DB from Mudlet, through builtin functions or the luasql. Is this possible?

When I try to access the database (it is in Mudlet home DIR, under my profile name) I get the following in the 'errors' console

Code: Select all

[ERROR] object:<deletethis> function:<Alias25>
         </home/chris/.config/mudlet/db.lua:532: attempt to concatenate local 's_name' (a nil value)>
The code I'm using to try and access the database, is :

Code: Select all

 local mydb = db:fetch(getMudletHomeDir() .. "/shop.db")
 db:fetch(mydb)
Thanks.

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

Re: Mudlet Databases

Post by Vadi »

I wouldn't expect it to work, as db.lua does it's best job to provide an interface as lua-like as possible, it does use it's own format for the schema setup and expect that to work. It's up to ixokai to answer this properly though.

What should work is using LuaSQL directly: http://www.keplerproject.org/luasql/manual.html

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Mudlet Databases

Post by Rakon »

Thanks for the response, that's exactly what I was looking for.

Using the following code, I am able to access/loop over the previously created database:
Code: [show] | [select all] lua
require("luasql.sqlite3")
env = assert(luasql.sqlite3())

conn = assert(env:connect(getMudletHomeDir() .. '/scripts/shop.db'))

cur = assert(conn:execute('select * from items'))

row = cur:fetch({},"a")

while row do
 display(row)
 row = cur:fetch(row,"a")
end
cur:close()
con:close()
env:close()
Neatly displays:

Code: Select all


...

table {
  'itemno': 'armour319003'
  'price': '3000'
  'owner': 'Rakon'
  'id': '885'
  'sold': 'N'
}

table {
  'itemno': 'armour28243'
  'price': '10000'
  'owner': 'Rakon'
  'id': '886'
  'sold': 'N'
}
...
Now to work on changing, and displaying values in a more useful manner.

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

Re: Mudlet Databases

Post by ixokai »

The "db" frontend is a wrapper around LuaSQL, and is not meant for providing direct SQL access. In fact, it goes out of its way to keep the reality of the database away from you -- though it could use work on a number of rough spots.

Its intended for people who don't really know databases, and don't want to learn them. It creates tables, columns, alters tables as columns are added, converts to/from Lua types, and does a few other convenient things for people who may want to store a bunch of data but don't need to really harness the full power of SQL -- or in your case, access a database created by some other program.

So, yeah. Vadi's right, using luasql directly is how you'd approach your problem.

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Mudlet Databases

Post by Rakon »

Here's another question, regarding LUA tables...

How do I compare two lua tables? IE compare table1 to table2, putting the values of table1 into table3, that are not in table2.

Python example:

Code: Select all

table1 = ["some","stuff","here"]

table2 = ["some","other","stuff","here"]


table3 = [i for i in table1 if i not  in table2]

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

Re: Mudlet Databases

Post by Vadi »

It's just Lua (not an abbreviation), and I believe what you're looking for is http://mudlet.org/asciidoc/manual.html# ... tersection?

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

Re: Mudlet Databases

Post by ixokai »

You do it all manually; Lua is extremely bare-bones. There's no convenient handling of just about anything: you iterate over 'em a bunch. The data-types are *extremely* minimalistic.

Now, that said, Mudlet provides a number of utility functions to make such things happier in our LuaGlobal.lua. And one recently added will help you:

For a table like that (indexed, vaguely array-like), use table.n_complement(first, second); it will return a table that contains the difference (in Python set terms) or complement (the terribly unfortunate math set terms) of the two, the elements in A that are not in B.

There's also table.n_union, and table n_intersection.

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Mudlet Databases

Post by Rakon »

Thanks for the response, both.

I've done something to mess up, and crashed mudlet.
Now LuaGlobal is empty....and I' need to figure out how to get all my stuff back.

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

Re: Mudlet Databases

Post by Vadi »

It's all saved in snapshots, so if it's not all back on restart, select an older snapshot to load in the connection screen.

Only known crasher atm is coroutines which aren't supported yet.

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Mudlet Databases

Post by Rakon »

I went to a previous 'snapshot'.
I went up to 15, previous snap shots. It's still not loading, in the state it was before the crash.

Code: Select all

[ERROR:] object:<prompt> function:<Trigger2>
         </home/chris/.config/mudlet/LuaGlobal.lua:483: bad argument #1 to 'pairs' (table expected, got nil)>
*Edit* ended up going to an older, snapshot about 30 previous ones from the list and it appears to be working, but still have lost a bit of work.

Post Reply