Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. require( "mysqloo" )
  2.  
  3. local auth = {
  4. hostname = '37.122.209.16',
  5. username = 'AC2',
  6. password = 'Rockslol',
  7. database = 'scoreboardhudftwchudlol_elevel',
  8. port = 3306
  9. }
  10.  
  11.  
  12. local db = mysqloo.connect( auth.hostname, auth.username, auth.password, auth.database, auth.port )
  13. db:connect()
  14. function db:onConnected()
  15. local q = self:query( "CREATE TABLE IF NOT EXISTS `elevel` ( `ID` char(64) NOT NULL, `JSONData` text NOT NULL, PRIMARY KEY (`ID`) )" )
  16. q:start()
  17. MsgC( Color( 255, 50, 50 ), "[*]", Color( 255, 255, 255 ), "Serverlog: ", Color( 255, 255, 255 ), "Connection to database succesful\n" )
  18. end
  19.  
  20. function db:onConnectionFailed( err )
  21. MsgC( Color( 255, 255, 255 ), "Serverlog: ", Color( 255, 255, 255 ), "Connection to database failed\n" )
  22. MsgC( Color( 255, 255, 255 ), "Serverlog: ", Color( 255, 255, 255 ), "Error: "..err.."\n" )
  23. end
  24.  
  25. function EL_loadInfo( steamID, callback )
  26. local query = db:query( "SELECT * FROM elevel WHERE ID = '" .. steamID .."'" )
  27. function query:onSuccess( data )
  28. if table.Count( data ) == 0 then
  29. callback( {}, 0 )
  30. return
  31. end
  32. callback( util.JSONToTable( data[ 1 ].JSONData ) )
  33. end
  34.  
  35. function query:onError( err )
  36. print( "An error occured while executing the query: " .. err )
  37. end
  38. query:start()
  39. end
  40.  
  41. function EL_saveInfo( steamID, inventory )
  42. local query = db:query( "SELECT * FROM elevel WHERE ID = '" ..steamID.."'" )
  43.  
  44. function query:onSuccess( data )
  45. if data && data[ 1 ] then
  46. local updateQuery = db:query( "UPDATE elevel SET JSONData = '" ..inventory.."' WHERE ID = '" ..steamID.. "'" )
  47. function updateQuery:onError( err, sql )
  48. print("An error occured while executing the query: " .. err)
  49. end
  50. updateQuery:start() -- update user information, because he exists.
  51.  
  52. else
  53. local insertQuery = db:query( "INSERT INTO elevel (`ID`,`JSONData`) VALUES('"..steamID.."' ,'"..inventory.."')" )
  54. function insertQuery:onError( err, sql )
  55. print("An error occured while executing the query: " .. err)
  56. end
  57. insertQuery:start() -- insert user if he doesn't exist.
  58. end
  59. end
  60.  
  61. function query:onError( err, sql )
  62. print("An error occured while executing the query: " .. err)
  63. end
  64. query:start() -- select user from the database
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement