Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. require("mysqloo")
  2.  
  3. local config = {
  4. host = "104.236.142.151", -- the IP/hostname of the server running the database
  5. username = "imkillingmyself", -- credentials for the database
  6. password = "",
  7. database = "test", -- the name of the database you are connecting to
  8. }
  9.  
  10. local db
  11.  
  12. -- set up a table to hold some of our 'prepared queries'
  13. -- a prepared query is similar to a function in that you
  14. -- give it some parameters and it will allow you to reuse
  15. -- a query you've already written, just with different parameters
  16. local prepared_queries = {}
  17.  
  18. -- I put this code into a function because I don't believe
  19. -- you can prepare a query before you've connected to the DB.
  20. -- I may be wrong.
  21. local function setup_prepared_queries()
  22. -- create a prepared query for inserting a player record
  23. -- later we can simply call :SetString on this object to
  24. -- set the values of the fields with the question marks
  25. prepared_queries.insert_player_info = db:prepare([[
  26. INSERT INTO player_data (`steamid`, `ip`)
  27. VALUES (?, ?)
  28. ]])
  29. end
  30.  
  31. local function connect_to_database(success, failure)
  32. -- mysqloo.connect makes the connection to the database server and returns
  33. -- a database object with methods you can use to write queries and such.
  34. db = mysqloo.connect(
  35. config.host,
  36. config.username,
  37. config.password,
  38. config.database
  39. )
  40. db.onConnected = success
  41. db.onConnectionFailed = failure
  42. db:connect()
  43. end
  44.  
  45. local function insert_player_info(steamid, ip, success, failure)
  46. local query = prepared_queries.insert_player_info
  47.  
  48. -- now we can simply use the :setString method to set the fields
  49. -- on the prepared query we made earlier
  50. query:setString(1, steamid) -- set the 1st question mark to the steamid
  51. query:setString(2, ip) -- set the 2nd question mark to the ip address
  52.  
  53. -- now we set our callback functions for handling the query's result and/or error
  54. query.onSuccess = success
  55. query.onError = failure
  56.  
  57. -- fire off the prepared query
  58. query:start()
  59. end
  60.  
  61. local function get_player_info(success, failure)
  62. -- it could be considered overkill to create a prepared query
  63. -- for a query without any parameters, so I left this as just
  64. -- a plain string.
  65. local query = db:query("SELECT * FROM player_data")
  66. query.onSuccess = success
  67. query.onFailure = failure
  68. query:start()
  69. end
  70.  
  71. function TestOutMySQLOO()
  72. connect_to_database(function(db)
  73. print("Setting up prepared queries...")
  74.  
  75. setup_prepared_queries()
  76.  
  77. print("Getting player information from the database...")
  78.  
  79. get_player_info(function(query, players)
  80. for i, row in ipairs(players) do
  81. print("retrieved row:", row.ip, row.steamid)
  82. end
  83.  
  84. print("\nNow we are going to insert a new player record...")
  85.  
  86. insert_player_info("STEAM_BLAH_BLAH", "127.0.0.1", function()
  87. print("The row has been created!")
  88. end, function(query, err)
  89. print("Error occurred in insert_player_info query:", err)
  90. end)
  91. end, function(query, err, sql)
  92. print("Error occurred in get_player_info query:", err)
  93. end)
  94. end, function(db, err)
  95. print("Failed to connect to the database:", err)
  96. end)
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement