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.

MySQL only insert if player isn't existing

Discussion in 'Archive' started by BOMBER1337, Apr 20, 2017.

  1. BOMBER1337

    BOMBER1337 Member

    Messages:
    7
    Likes Received:
    1
    Joined:
    Apr 15, 2017
    Hi.
    So i don't want more then one row with the same name so they query should only be executed once and when there is already an entry in the database it shouldn't be executed.

    Edit: Ok i've got the solution for the problem. :=P

    SOLUTION:
    Code:
                sqlConnection = new MySqlConnection(dbConnect);
                sqlConnection.Open();
                string NAME = player.name;
                if (sqlConnection.State == System.Data.ConnectionState.Open) //Connection is open
                {
                    MySqlCommand commandRead = new MySqlCommand("SELECT * FROM TABLE WHERE `Name`='" + NAME + "'", sqlConnection);
                    MySqlCommand Create = new MySqlCommand("INSERT INTO TABLE (`Name`) VALUES ('" + NAME + "')", sqlConnection);
                    sqlReader = commandRead.ExecuteReader();
                    if (sqlReader.HasRows)
                    {
                        //Entry was found
                    }
                    else
                    {
                        //Entry wasn't found
                        sqlReader.Close(); //Close the reader
                        Create.ExecuteNonQuery(); //Execute the insert string as query
                        API.consoleOutput("Created Entry for: " + NAME);
                    }
                    sqlConnection.Close();
                }
            }
    
     
    Last edited: Apr 20, 2017
  2. root

    root Well-Known Member

    Messages:
    244
    Likes Received:
    88
    Joined:
    Aug 15, 2016
    Please post the solution for your problem so other users with the same problem can profit from it.
     
  3. BOMBER1337

    BOMBER1337 Member

    Messages:
    7
    Likes Received:
    1
    Joined:
    Apr 15, 2017
    Hi, i've posted the solution above as code.
     

Share This Page