A simpler DB container.
Re: A simpler DB container.
Using the above code in an alias, then calling it, creates both the container file and the table properly.
Re: A simpler DB container.
Okay, I removed the last argument to 'assert' so that I could get back more meaningful errors. Fixed up my code, and added in a 'display(results)' to see if I got anything back. Then I ran the following dba function:
Which (properly I think) returned :
So I assume that means that the phpTable results, has been created correctly. However, this is a local variable and not accessiable once the code is done running. So, how do I access the phpTable results to modify them, or display them instead of the call to the phpTable function?
Code: Select all
/dba.query([[SELECT * FROM weapons where type="rapier" and spd>='227']])
Code: Select all
table {
'count': function
'pairs': function
}
So I assume that means that the phpTable results, has been created correctly. However, this is a local variable and not accessiable once the code is done running. So, how do I access the phpTable results to modify them, or display them instead of the call to the phpTable function?
Re: A simpler DB container.
edit: Sorry for the delay on that, by the way. Been nursing an injured hand.
Re: A simpler DB container.
No worries, thanks. I've done that via query, and gotten a script to properly display results, that is the results:phpTable() items using a
Thanks again for the code, and assistance Tsuujin.
Using your above code, again, only the pairs/count phpTable function is returned as a table, not the actual information inside of it.
Thanks again for the code, and assistance Tsuujin.
Re: A simpler DB container.
Okay, I've gotten the majority of this script working, with your help and thanks to you.
I can query and return results:
if not col
if #col == 0
And just about everything else testing for existance. nil, len(), #, display(), echo(), table.contains() ... just everything I could think of.
Also tried this code to no avail:
Thanks
I can query and return results:
And I can execute statements against the database:
But, using the following code, I cannot TEST for exsistance of a value in the database:
The thing is the statement 'if not col.count' ... I don't know how to test for this 'name' existing in the results phpTable. I've tried to use:
if not col
if #col == 0
And just about everything else testing for existance. nil, len(), #, display(), echo(), table.contains() ... just everything I could think of.
Also tried this code to no avail:
Any ideas or working code to test the results phpTable for an item??
Thanks
Re: A simpler DB container.
Ok, A couple of things here.
the php styled table doesn't return data like you may expect. It keeps data sorted by actually adding whatever you insert into the table into a sub-table, indexed by number. As you may or may not know, Lua will sort numerically indexed tables by the order in which they were added for you, but if you use keyed indexes (like a string) it actually sorts it by the hash value of the key. php:Table is an answer to this.
So, if you do phptable['me'] = "tsuujin", the metatable information on phptable diverts the information into a hidden subtable, in order to maintain organization. This is why you can't just display(phptable), because display looks at the raw table information without respecting the metatable data.
Here's a piece of code I just verified as working from my actual system:
php:Table is actually public code that I've just modified in a small way, but one of those modifications is very important to another of your questions. I added a count() feature, which returns the number of rows in the table. So, if you wanted to quickly see how many results you returned, do:
On a side note: learn to use string.format. Your life will be much less complex once you realize how damned useful it is, and how much cleaner your code is to read.
Instead of:
the php styled table doesn't return data like you may expect. It keeps data sorted by actually adding whatever you insert into the table into a sub-table, indexed by number. As you may or may not know, Lua will sort numerically indexed tables by the order in which they were added for you, but if you use keyed indexes (like a string) it actually sorts it by the hash value of the key. php:Table is an answer to this.
So, if you do phptable['me'] = "tsuujin", the metatable information on phptable diverts the information into a hidden subtable, in order to maintain organization. This is why you can't just display(phptable), because display looks at the raw table information without respecting the metatable data.
Here's a piece of code I just verified as working from my actual system:
This calls display not on the base table, but on the results table for each entry in it. You can use this to see each and every result, it's kind of handy. The key to remember here is that when you call pairs() on a php:Table, you're going to get the numerical index as the first argument ("row") and a table containing that rows data ("results") that you can work with.
php:Table is actually public code that I've just modified in a small way, but one of those modifications is very important to another of your questions. I added a count() feature, which returns the number of rows in the table. So, if you wanted to quickly see how many results you returned, do:
You could of course expand that into expanded error checking or data reporting.
On a side note: learn to use string.format. Your life will be much less complex once you realize how damned useful it is, and how much cleaner your code is to read.
Instead of:
do
While I know the requirement of escaping percents with a double percent is kind of ugly, you won't always run into this, and it's still much easier to read than breaking the string down into parts and concatenating. Also helps because a lot of syntax highlighters get confused with the opening and closing quotations. Finally, string.format tends to be faster, and avoids a bug in Lua's core coding regarding bulk concatenation and the garbage collector.
Re: A simpler DB container.
Okay, thank you for that code clarification. I'll use the string.format technique from now on, it is easier to ready.
Question though, how do I utilize the transaction functions in the script, IE
begin()
commit()
I've tried using them as shown in my previous post (modified to the below):
'Unable to begin transaction'
'Unable to commit transaction'
or just
'Unable to commit transaction'
Am I using them wrong?
Question though, how do I utilize the transaction functions in the script, IE
begin()
commit()
I've tried using them as shown in my previous post (modified to the below):
But I get :
'Unable to begin transaction'
'Unable to commit transaction'
or just
'Unable to commit transaction'
Am I using them wrong?
Re: A simpler DB container.
I changed the way the transactions are handled, and have modified the first post in this thread to reflect the changes. Try them out, see if they work better for you.
All you need to do is call dba.begin() before executing in bulk, and dba.commit() afterwards. From one of my files:
All you need to do is call dba.begin() before executing in bulk, and dba.commit() afterwards. From one of my files:
Note that dba.begin() returns false if a transaction is already in the works, and the first few lines of that snippet give example on how to wait until the current transaction is finished before beginning a new one.
Re: A simpler DB container.
Right, okay. When I do like I stated:
I don't know why this happens considering that's the point of transaction/commiting a bulk set.
The first one completes, but I get an assert error on the second dba.execute for 'LuaSQL database locked'. In order to resolve this, I have to completely shut down Mudlet, then restart.
I don't know why this happens considering that's the point of transaction/commiting a bulk set.
Re: A simpler DB container.
When I'm trying to use being(), execute()*40, then call commit(), an 'lsof RAKON.db' command returns ... a lot of open connections to the file; not good. And sometimes, I get the error:
</.config/mudlet/profiles/Rakon/dba.lua:70: LuaSQL: SQL logic error or missing
database>
Also getting this error at times, for what reason... I do not know:
</.config/mudlet/profiles/Rakon/dba.lua:44: calling 'execute' on bad self
(LuaSQL: connection is closed)>
How do I prevent this, when utilizing luaSQL transaction history, without turning on autocommit and using the begin() ' BEGIN TRANSACTION' statements?
</.config/mudlet/profiles/Rakon/dba.lua:70: LuaSQL: SQL logic error or missing
database>
Also getting this error at times, for what reason... I do not know:
</.config/mudlet/profiles/Rakon/dba.lua:44: calling 'execute' on bad self
(LuaSQL: connection is closed)>
How do I prevent this, when utilizing luaSQL transaction history, without turning on autocommit and using the begin() ' BEGIN TRANSACTION' statements?