Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("mysqloo")
- local config = {
- host = "104.236.142.151", -- the IP/hostname of the server running the database
- username = "imkillingmyself", -- credentials for the database
- password = "",
- database = "test", -- the name of the database you are connecting to
- }
- local db
- -- set up a table to hold some of our 'prepared queries'
- -- a prepared query is similar to a function in that you
- -- give it some parameters and it will allow you to reuse
- -- a query you've already written, just with different parameters
- local prepared_queries = {}
- -- I put this code into a function because I don't believe
- -- you can prepare a query before you've connected to the DB.
- -- I may be wrong.
- local function setup_prepared_queries()
- -- create a prepared query for inserting a player record
- -- later we can simply call :SetString on this object to
- -- set the values of the fields with the question marks
- prepared_queries.insert_player_info = db:prepare([[
- INSERT INTO player_data (`steamid`, `ip`)
- VALUES (?, ?)
- ]])
- end
- local function connect_to_database(success, failure)
- -- mysqloo.connect makes the connection to the database server and returns
- -- a database object with methods you can use to write queries and such.
- db = mysqloo.connect(
- config.host,
- config.username,
- config.password,
- config.database
- )
- db.onConnected = success
- db.onConnectionFailed = failure
- db:connect()
- end
- local function insert_player_info(steamid, ip, success, failure)
- local query = prepared_queries.insert_player_info
- -- now we can simply use the :setString method to set the fields
- -- on the prepared query we made earlier
- query:setString(1, steamid) -- set the 1st question mark to the steamid
- query:setString(2, ip) -- set the 2nd question mark to the ip address
- -- now we set our callback functions for handling the query's result and/or error
- query.onSuccess = success
- query.onError = failure
- -- fire off the prepared query
- query:start()
- end
- local function get_player_info(success, failure)
- -- it could be considered overkill to create a prepared query
- -- for a query without any parameters, so I left this as just
- -- a plain string.
- local query = db:query("SELECT * FROM player_data")
- query.onSuccess = success
- query.onFailure = failure
- query:start()
- end
- function TestOutMySQLOO()
- connect_to_database(function(db)
- print("Setting up prepared queries...")
- setup_prepared_queries()
- print("Getting player information from the database...")
- get_player_info(function(query, players)
- for i, row in ipairs(players) do
- print("retrieved row:", row.ip, row.steamid)
- end
- print("\nNow we are going to insert a new player record...")
- insert_player_info("STEAM_BLAH_BLAH", "127.0.0.1", function()
- print("The row has been created!")
- end, function(query, err)
- print("Error occurred in insert_player_info query:", err)
- end)
- end, function(query, err, sql)
- print("Error occurred in get_player_info query:", err)
- end)
- end, function(db, err)
- print("Failed to connect to the database:", err)
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement