Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ mysql connections

mysql connections

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #15 on Mon 17 Aug 2009 08:44 AM (UTC)
Message
Easily done. But it still spits errors. Check on that line number it gave me there is this function



function MakeSandbox ()

  local function ReportDisabled (pkg, func)
     return function ()
       error (string.format (
        "Function '%s.%s' disabled in Lua sandbox - see MUSHclient global preferences",
        pkg, func), 2)
       end -- function
  end -- ReportDisabled 

  package.loadlib = ReportDisabled  ("package", "loadlib") -- disable loadlib function
  package.loaders [3] = nil  -- disable DLL loader
  package.loaders [4] = nil  -- disable all-in-one loader

  for k, v in pairs (io) do
    if type (v) == "function" then
      io [k] = ReportDisabled ("io", k)
    end -- type is function
  end -- for



So.. you check the box, add the world.. and do something else? If it was false I would just flick it over to true, but i have no idea what to do about this nil business.

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #16 on Mon 17 Aug 2009 08:53 AM (UTC)
Message
Here is how I got it to work on a new MUSHclient installation:




Now, this works in my Immediate window:


 assert (package.loadlib ("mysql.dll", "luaopen_luasqlmysql")) ()

 env = assert (luasql.mysql())
    
 con = assert (env:connect ("databasename", "username", "password", "site_address"))

 -- retrieve a cursor
 cur = assert (con:execute ("SELECT count(*) from some_table" ))

 -- print all rows, the rows will be indexed by field names
 row = cur:fetch ({}, "a")

 while row do
   table.foreach (row, print)
   row = cur:fetch (row, "a")
 end

 -- close everything
 cur:close()
 con:close() 
 env:close()



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #17 on Mon 17 Aug 2009 08:54 AM (UTC)
Message
Quote:

And thanks for all the responses, btw! Such a lovely client.


*blush*

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #18 on Mon 17 Aug 2009 09:26 AM (UTC)
Message
Hot skippy peanut butter.


With a little fiddling I am able to connect to my other computer and run queries on tables. Hurray for progress!!

You guys are awesome!

It was able to count my 6 records no problem!


,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #19 on Tue 18 Aug 2009 06:43 AM (UTC)

Amended on Tue 18 Aug 2009 06:46 AM (UTC) by Lilbopeep

Message
So now that I have a connection, I've been playing around with a few of the simple databases I have. What I am really trying to figure out now is how to call specific records through aliases and also how to format them. As an example, my current script looks very similar to the one provided -



cur = assert (con:execute"SELECT * from race_pc")
-- print all rows, the rows will be indexed by field names
row = cur:fetch ({}, "a")
while row do
  print(string.format("Name: %s, Remort: %s", row.Name, row.Remort))
  -- reusing the table of results
  row = cur:fetch (row, "a")
end


This gives me the basic output of:

Name: Human1, Remort: Non-Remort
Name: Dwarf, Remort: Non-Remort
Name: Elf, Remort: Non-Remort
Name: Firbolg, Remort: Non-Remort
Name: Big Dwarf, Remort: 1st-Remort
Name: Bigger Dwarf, Remort: 2nd-Remort

However, I have more information than this in my databases, it just doesn't "look" very pretty like this, and it is calling every entry, which is fine right now, but I may like to get more selective in the future.

The additional columns I have are 10 'stat' entries with positive and negative values as racial bonuses, which language they speak, what size they are, and the skills they start with.

How do I make all this information look a little 'neater' on the output? For instance

Name: <show name> Remort: <show remort>
Size: <show size> Language: <how language>
Skills: <show skills>
 St   Qu   Pr   Em   In   Co   Ag   Sd   Me   Re
<st> <qu> <pr> <em> <in> <co> <ag> <sd> <me> <re>


And I also need some help figuring out how to query based on wildcards. Lets say I had an alias 'lookup race *' and it would pull the record or records that match? I was thinking I would have to somehow store whatever was given as a variable and put that into the select statement?

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #20 on Tue 18 Aug 2009 06:58 AM (UTC)

Amended on Tue 18 Aug 2009 06:59 AM (UTC) by Nick Gammon

Message
The neater formatting is just the judicial use of string.format. For example:


print (string.format ("%-3s %-3s %-3s", "a1", "b", "c")) --> Outputs: a1  b   c  


Note that each entry takes 3 spaces, left justified.

Quote:

I also need some help figuring out how to query based on wildcards


Just add a WHERE clause to the SELECT, like this:


race = "dwarf"  -- for example

cur = assert (con:execute (string.format ("SELECT * from race_pc WHERE race = '%s' ", race))

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #21 on Tue 18 Aug 2009 07:19 AM (UTC)
Message

local charOutput =[[Name: %s Remort: %s
Size: %s Language: %s
Skills: %s
 St   Qu   Pr   Em   In   Co   Ag   Sd   Me   Re
 %s   %s   %s   %s   %s   %s   %s   %s   %s   %s
]]
cur = assert (con:execute(string.format("SELECT * FROM race_pc WHERE %s LIKE '%%%s%%'", ColumnVarSetbyAlias, FilterVarSetbyAlias)
-- print all rows, the rows will be indexed by field names

local row = {}

while cur:fetch (row, "a") do
  print(string.format(charOutput, row.Name, row.Remort, row.Size, 
			row.Language, row.Skills, row.St, row.Qu, row.Pr, 
			row.Em, row["In"], row.Co, row.Ag, row.Sd, row.Me, row.Re))
end

cur:close()



SQL (MySql, SQLite, MS SQL Server, Oracle, yadda yadda) use % as a wildcard, the "like" keyword means a pattern search. Since string.format uses % for special characters as well, you need to double them up if you want a literal % character. So, if ColumnVarSetbyAlias is "language", and FilterVarSetbyAlias is "orc" then the format function becomes "...WHERE language LIKE '%orc%'" which will give you a match anywhere the string orc is in that field. http://www.mushclient.com/scripting has a walkthrough with screenshots (though the shreenshots are for a really old version of MC) if you have questions on setting up an alias.

Lua uses [[ ]] for long strings.

foo = "This produces
errors." -- since Lua wants the close quote on the same line.

bar = [[This doesn't
produce errors, since lua will put everything into the variable upto the ]]


Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #22 on Tue 18 Aug 2009 07:31 AM (UTC)

Amended on Tue 18 Aug 2009 07:35 AM (UTC) by Lilbopeep

Message
Ah. I think I got the basics of using that string.format. Thanks Nick!


I'm still a little lost on the wildcards -

Say for instance I wanted to be able to look up race information based on the master race, I would provide this when I typed the alias like:

lookup Dwarves

and this would essentially do

"SELECT * FROM race_PC WHERE Race = Dwarves"

Your example also added (string.format to the mysql query, which my script currently does not. It currently looks like:


ur = assert (con:execute("SELECT * from race_pc"))
-- print all rows, the rows will be indexed by field names
row = cur:fetch ({}, "a")
while row do
  print(string.format("Master Race: %-3s", row.Race))
  print(string.format("Name: %-3s Remort: %-3s Size: %-3s", row.Name, row.Remort, row.Size))
  print(string.format("Creation Cost: %-3s", row.Creation_Cost))
  print("St Qu Pr Em In Co Ag Sd Me Re")
  print(string.format("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s", row.Strength, row.Quickness, row.Presence, row.Empathy, row.Intuition, row.Constitution, row.Agility, row.Self_Discipline, row.Memory, row.Reasoning))
  -- reusing the table of results
  row = cur:fetch (row, "a")
end



Edit: Just saw WillFA's post, will give that a good read and see if I can get anywhere!

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #23 on Tue 18 Aug 2009 07:41 AM (UTC)
Message
Quote:

and this would essentially do

"SELECT * FROM race_PC WHERE Race = Dwarves"


For a start, you need to quote strings, so that should be:


"SELECT * FROM race_PC WHERE Race = 'Dwarves'  "


Then, as Willfa said, to get wildcards (if you want them) you can do LIKE:


"SELECT * FROM race_PC WHERE Race LIKE '%Dwarves%'  "


You still need the quotes. And he is right about the % symbols inside string.format.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Lilbopeep   USA  (42 posts)  Bio
Date Reply #24 on Tue 18 Aug 2009 08:01 AM (UTC)
Message
Hmm. I tried WillFa's way and its giving me


[string "Alias: "]:14: bad argument #2 to 'format' (string expected, got nil)
stack traceback:
	[C]: in function 'format'
	[string "Alias: "]:14: in main chunk


which is line

cur = assert (con:execute(string.format("SELECT * FROM race_pc WHERE %s LIKE '%%%s%%'", ColumnVarSetbyAlias, FilterVarSetbyAlias)))



At first I was getting some 'expected ")" to close yada' on that line, so I added two ")" at the end.

I also am not sure what my alias is supposed to look like. From the link provided, I would guess I need to focus on the information provided within the 'target' example of setting up an alias?

,.~`'~.,Dance Magic Dance,.~`'~.,
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #25 on Tue 18 Aug 2009 08:03 AM (UTC)
Message
Well, argument #2 is ColumnVarSetbyAlias.

Since you haven't quoted it, that is a variable. Does it contain a string? Sounds like not.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


82,872 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.