Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. local oldprint = print;
  2.  
  3. P = "";
  4. --Overwrite print with a print that stores the text in a variable instead
  5. --We can retrive this variable in nwn later
  6. function print (val1, val2)
  7. P = P .. tostring(val1);
  8. if val2 ~= nil then
  9. P = P .. " " .. tostring(val2);
  10. end
  11. P = P .. "\n";
  12. end
  13.  
  14. --Define test globally
  15. function test()
  16.  
  17. --Start a timer
  18. local timer = Timer.New();
  19. timer:Start();
  20.  
  21. local server = "10.9.23.254";
  22. local user = "lua";
  23. local password = "Kah9LpSp9UEZA6qf";
  24. local db = user;
  25. local port = 3306;
  26.  
  27. --connect to DB with above credentials
  28. local sql = MySQL.Connect(server,user,password,db,port);
  29. if(sql == nil)then
  30. print("Unable to connect to database");
  31. else
  32. print("Connection OK");
  33.  
  34. --Select our gff from the database
  35. sql:Query("SELECT Data FROM test WHERE Name='Test';");
  36. if not sql:Fetch() then
  37. print("Unable to fetch data");
  38. end
  39.  
  40. --Get the row by index and deserialize the gff into tables
  41. local gff = sql:GetRow(1);
  42. local bic = GFF.OpenString(gff);
  43.  
  44. --Find FirstName in the topstruct and print it
  45. for n=1,#bic.Fields do
  46. if bic.Fields[n].Label == "FirstName" then
  47. print("First Name: " .. bic.Fields[n].Data.Strings[1].String);
  48. break;
  49. end
  50. end
  51.  
  52. --Find Cha in the topstruct and print the current and increment it by 1
  53. for n=1,#bic.Fields do
  54. if bic.Fields[n].Label == "Cha" then
  55. print("Charisma: " .. bic.Fields[n].Data);
  56. bic.Fields[n].Data = bic.Fields[n].Data + 1;
  57. break;
  58. end
  59. end
  60.  
  61. --Serialize the gff back into a string, encode it for sql and save
  62. print(sql:Query("UPDATE test SET Data="..sql.EncodeString(GFF.SaveToString(bic)).." WHERE Name='Test';"));
  63. end
  64. --Stop timer and print the time it took
  65. timer:Stop();
  66. print("SQL + GFF took "..tostring(timer:Elapsed()).." ms");
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement