sqlite variables

Posted by Miloh on Sun 20 Nov 2011 04:56 AM — 4 posts, 16,138 views.

#0
Greetings all,

I am attempting to create a mob database with sqlite. The db is very simple - I just have two fields: mobname and roomname. Both fields go into the sqlite db. The problem is that one of the variables (MyRoom) is going into the db as the variable name itself. I am guessing this is a syntax thing? Any insight would be most appreciated. Here is what I am using:


<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="mobdb"
   match="^You get (.*) gold coin? from the dead body of (.*)\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>local MyRoom
  
--RoomName set by another trigger elsewhere that captures roomname.
MyRoom = GetVariable("RoomName")

--ensure vars are correct
Note ("%2 is in ",MyRoom)

--begin db stuff

DatabaseOpen ("mobs", GetInfo (66) .. "mobs.db", 6)

-- insert a record
DatabasePrepare ("mobs", "INSERT INTO mobtable (mobname, roomname) VALUES ('%2','MyRoom')")  --&gt; returns 0 (SQLITE_OK)
DatabaseStep ("mobs")   -- returns 101 (SQLITE_DONE)
DatabaseFinalize ("mobs")  -- returns 0 (SQLITE_OK)
      
DatabaseClose ("mobs")  -- close it </send>
  </trigger>
</triggers>


And when I check the db, the "%2" or mobname goes in as correctly. But, for roomname, I get "MyRoom".

Thanks much in advance!
Amended on Sun 20 Nov 2011 05:02 AM by Miloh
USA Global Moderator #1
Quote:
VALUES ('%2','MyRoom')

I'm not sure what you expect that to do other than what is happening.

Perhaps you want instead
"INSERT INTO mobtable (mobname, roomname) VALUES ('%2','"..MyRoom.."')"
Amended on Sun 20 Nov 2011 05:14 AM by Fiendish
#2
Hi Fiendish, you are the man!! That worked like a charm. Thank you very much. I had a feeling it was something like that. I did fail to mention that I tried everything I could think of with that pesky variable... Double quotes, no quotes at all, a ".." before the variable, a dollar sign...

Anywho, again, thank you :)



USA Global Moderator #3
Values for %1,%2,etc get mapped into place by MUSHclient before the Lua code gets sent to the script parser. That's the only reason you can use %2 in the way you did without breaking the query string apart into chunks (and which is why people need to wrap them in quotation marks to compare the values with strings). But MyRoom is just a Lua variable containing a string. In order to get the value from MyRoom you have to treat it as a variable and not just like some letters, which means pulling it outside of the string and using concatenation to merge the parts together.

You had
DatabasePrepare("mobs", "a_string")
when you needed
DatabasePrepare("mobs", "first_part_of_the_string"..your_variable.."the_rest_of_the_string")