MySQL Tutorial

From GMod Wiki

Revision as of 21:01, 12 August 2011 by Bambö (Talk | contribs)
Jump to: navigation, search

Contents

Introduction

So in this tutorial I will attempt to show you how you can save and load data such as money or scores using Andy Vincent's Gm_MySql Module[1]

From here on I'm assuming that you have installed the module correctly,know a little bit about Lua and that you have setup a mysql database.

This tutorial will just be a rough outline on how you would use MySQL so its probably not the best and anyone who can fix/patch/update my snippets are more than welcome to.

This is basically just the example script that Andy give's you in the thread but with a practical use I guess...

Settings

To connect the module has to have the information what you used to make your database and account, don't worry this is all server side so none can see it!

This is the easiest way of organising the settings in my eyes.

 
require ("mysqloo")                // Include the modules
 
local DATABASE_HOST = "www.freesql.org"   // database host (can be an ip)
local DATABASE_PORT = 3306                // port to the database, you probably wont need to change this unless you get told to
local DATABASE_NAME = "bamboserver"       // name of the database
local DATABASE_USERNAME = "bambo"        // username which you use to access it
local DATABASE_PASSWORD = "root"          // password of the username
 

Connecting

Before we can start ripping things from our database we have to actually connect to it, here's how.

 
function connectToDatabase()
	databaseObject = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT)
	databaseObject.onConnected = function() print("Database linked!") end
	databaseObject.onConnectionFailed = function() print("Failed to connect to the database.") end
	databaseObject:connect()
end
 
connectToDatabase()
 

Line 2 - Connect to the database using the info from before

Line 3 - When its connected do something, in our case print "Database Linked!"

Line 4 - If it doesn't connect do something else

Line 5 - Tell it to connect

Line 7 - Run the function when the script is ran

First Join

Yippe! we are now connected to our database, now we can start to get things or save things, here we will save the users SteamID when s/he first joins.

My database looks like this...

   Table Name = Players
       First Column = ID
       Second Column = Money
 
	local query1 = databaseObject:query("SELECT * FROM Players WHERE ID = '" .. ply:SteamID() .. "'")
	query1.onSuccess = function(q) print("Checked!") end
	query1.onData = function(q)	
			local query2 = databaseObject:query("INSERT INTO Players(ID, money) VALUES ('" .. plyID .. "', " .. 15 .. ")")		// else create the bugger
			query2.onSuccess = function(q)  print("Created you!") end
			query2.onError = function(q,e) print("something went wrong") end
			query2:start()	
	end
	query1.onError = function(q,e) print("something went wrong when checking") end
	query1:start()
 
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox