1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

Using Gomitung's MySQL Connector

Discussion in 'Tutorials/Documentations' started by TakeiT, Jan 3, 2017.

  1. TakeiT

    TakeiT Member

    Messages:
    40
    Likes Received:
    15
    Joined:
    Dec 28, 2016
    I noticed a lot of people trying to use Gomitung's MySQL Connector, and seem to be having difficulties making it work. Mainly because there is a lack of documentation out of the box, and there are issues with the resource. In this tutorial, I will outline the basics to get this resource installed and working.

    Step 1: Download the resource source file. We need to download the source and not the compiled (.dll) version, as there is a problem with the script that we need to fix. You can download it here

    Step 2: In your server/resources directory, create a new directory called "database". Note: the folder MUST be named "database" or it will not work!

    Step 3: Export the contents of the source folder to the new directory you just created.

    Step 4: Using your favorite code editor (I'm using Notepad++), edit database.cs. Press CTRL+F and search for MySqlCommand cmd = new MySqlCommand(sql, conn);. On the next line after each MySqlCommand line, add conn.Open();. Save and close the file.

    Step 5: While we're in this resource folder, now is a good time to set up the database information. Edit the meta.xml file, and replace the default data that is in there with your database connection information. Save and close this file. If you do not have a database, you can download XAMPP.

    Step 6: We now need to go into our gamemode's resource folder, and open that meta.xml. We need to include the database resource so we can use it in our gamemode. This can be done by adding this line right under the <info> line.
    <include resource="database" />

    Step 7: We also need to include some .NET features to work with the database. Under the line you just added, add
    <assembly ref="System.Data.dll"/>
    <assembly ref="System.Xml.dll"/>
    You can now save & close the meta file.

    Step 8: We can now start working on the actual gamemode. We are going to make a simple function to insert a value into a table, then read that value and print it back to the console. Start by opening your gamemode. I will use the default freeroam.cs as an example.

    Step 9: The database resource uses DataTables to store the results. To use that, we need to include System.Data, by adding using System.Data;

    Step 10: Under the first function (Should be called Public Classname(), in the freeroam gamemode it is public FreeroamScript()) we are going to make a query. I have gone ahead and created a table called "testtable" and a column with "testvalue". We are going to add the number 3 in testvalue, then read it and print it back to the server. To do this, add the following:

    Code:
    API.exported.database.executeQuery("INSERT INTO testtable (testvalue)VALUES(3)"); //this will set testvalue to 3.
     
    Step 11: Now we want to read that value from the table and print it to the console, on the next line, add:
    Code:
    string query = "SELECT * FROM testtable"; //set up our query. this line isn't necessarily necessary however it keeps the code clean
            DataTable result = API.exported.database.executeQueryWithResult(query); //dump the results into a datatable
          
            if(result.Rows.Count != 0) //count the rows in the datatable, if there arent any (nothing in the database), it will skip this
            {
                foreach(DataRow row in result.Rows) //loop through each row
                {
                    Console.WriteLine("The number in the database is " + row["testvalue"]); //get the value of testvalue and print it to the console
                }
            }
    By this point, your basic code should look something like this:

    Code:
    public FreeroamScript()
        {
            API.onClientEventTrigger += onClientEventTrigger;
          
            API.exported.database.executeQuery("INSERT INTO testtable (testvalue)VALUES(3)"); //this will set testvalue to 3.
          
            string query = "SELECT * FROM testtable"; //set up our query. this line isn't necessarily necessary however it keeps the code clean
            DataTable result = API.exported.database.executeQueryWithResult(query); //dump the results into a datatable
          
            if(result.Rows.Count != 0) //count the rows in the datatable, if there arent any (nothing in the database), it will skip this
            {
                foreach(DataRow row in result.Rows) //loop through each row
                {
                    Console.WriteLine("The number in the database is " + row["testvalue"]); //get the value of testvalue and print it to the console
                }
            }
        }
    Step 12: There is one more thing to do to set the server up. In your database resource, there is a a library named "MySql.data.dll". We need to copy that file and paste it in the server's root directory (Where the server executable is located).

    Step 13: Assuming your database is online, you are ready to test it out! Run the server. If all went as planned, you should see "The number in the database is 3", after all the loading messages. Congratulations! You have set up your server's mysql connection!
     
    Last edited: Jan 11, 2017
    Christian J. E and VFRZ like this.
  2. Starlank

    Starlank New Member

    Messages:
    3
    Likes Received:
    0
    Joined:
    Dec 24, 2016
    I have added conn.Open() like you said, but also added conn.Close() at the end too. Not sure if it makes a difference but it's been working okay for me for the past week.
     
  3. wshbR.

    wshbR. Member

    Messages:
    21
    Likes Received:
    0
    Joined:
    Dec 23, 2016
    Awesome, thank you.
     
  4. Inked

    Inked Member Language Board Moderator

    Messages:
    43
    Likes Received:
    4
    Joined:
    Jan 3, 2017
    How can i find source folders?
     
  5. Christian J. E

    Christian J. E Member

    Messages:
    26
    Likes Received:
    0
    Joined:
    Aug 16, 2016
    A thousand kisses your way.
     
  6. TakeiT

    TakeiT Member

    Messages:
    40
    Likes Received:
    15
    Joined:
    Dec 28, 2016
    When you download the package it is in there.
     
  7. Inked

    Inked Member Language Board Moderator

    Messages:
    43
    Likes Received:
    4
    Joined:
    Jan 3, 2017
    Can you send me this package link?
     
  8. TakeiT

    TakeiT Member

    Messages:
    40
    Likes Received:
    15
    Joined:
    Dec 28, 2016
  9. Inked

    Inked Member Language Board Moderator

    Messages:
    43
    Likes Received:
    4
    Joined:
    Jan 3, 2017
  10. Clicked

    Clicked Member

    Messages:
    41
    Likes Received:
    7
    Joined:
    Dec 29, 2016
    Hey bro, i followed the tutorial and try a lot of things but i cant with this error: http://pastebin.com/QrSz8ijS

    Can you help me? regards!
     
  11. SLaYz

    SLaYz Member

    Messages:
    10
    Likes Received:
    4
    Joined:
    Dec 28, 2016
    Have you added <assembly ref="MySql.Data.dll"/> in your meta.xml?
     
  12. Clicked

    Clicked Member

    Messages:
    41
    Likes Received:
    7
    Joined:
    Dec 29, 2016
    I put it in the meta.xml of my gamemode, but continue the problem: http://pastebin.com/rCiyw3Rt

    I read and some things changed but i dont understand the errors :=(
     
  13. ZiiM

    ZiiM Member

    Messages:
    27
    Likes Received:
    5
    Joined:
    Dec 28, 2016
    You didnt follow the tutorial really well, lmfao.

    Step 12: There is one more thing to do to set the server up. In your database resource, there is a a library named "MySql.data.dll". We need to copy that file and paste it in the server's root directory (Where the server executable is located).
     
  14. MrPancakers

    MrPancakers Active Member

    Messages:
    90
    Likes Received:
    19
    Joined:
    Aug 2, 2016
    In step 4, doesn't leaving your connection open without closing it leave you vulnerable to sqli attacks?
     
  15. m4a_X

    m4a_X Member

    Messages:
    30
    Likes Received:
    2
    Joined:
    Jan 23, 2017
    System.InvalidOperationException: Connection must be valid and open.

    string query = "UPDATE `users` SET `fraktion` = '" + fraktion + "', `leader` = '" + leader + "', `geld` = '" + geld + "', `admin` = '" + admin + "', `bank` = '" + bank + "' WHERE `name` = '" + player.socialClubName + "'";
    API.exported.database.executeQuery(query);

    idk if i read the databse i dont have problems?
     
  16. Mogli1337

    Mogli1337 Member

    Messages:
    9
    Likes Received:
    1
    Joined:
    Jan 28, 2017
    Same problem here! Idk how to fix this
     
  17. Gomitung

    Gomitung Member

    Messages:
    44
    Likes Received:
    36
    Joined:
    Aug 2, 2016
    No longer needs step 4
     
    Mendes likes this.

Share This Page