Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

checking if an account already exists in Sqlite

Discussion in 'Scripting' started by chemsoun, Apr 11, 2015.

  1. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    hi , i'm creating a register menu for my unity game, so i want to check if the account to create is not already existing in the DataBase, i thaught i made it work but it shows an error.

    here's my code

    Code (CSharp):
    1. string conn = "URI=file:E:\\PFE\\sqlite\\DataBase"; //Path to database.
    2.                 IDbConnection dbconn;
    3.                 dbconn = (IDbConnection)new SqliteConnection (conn);
    4.                 Debug.Log ("before");
    5.                 dbconn.Open (); //Open connection to the database.
    6.                 Debug.Log ("after");
    7.                 IDbCommand dbcmd = dbconn.CreateCommand ();
    8.  
    9.                 String req= "select Email from Users where Email = '"+Email+"' ";
    10.                 dbcmd.CommandText=req;
    11.                 IDataReader reader = dbcmd.ExecuteReader();
    12.  
    13.                 Debug.Log(Email);
    14.  
    15.                 while(reader.Read()){
    16.  
    17.                     string e = reader.GetString(3);
    18.  
    19.                     Debug.Log(e);
    20.                
    21.                     if (Email == e){
    22.                        
    23.                         i++;
    24. }
    25. }
    26.  
    27. if(i>0) {
    28.  
    29.                     Debug.Log("not existing");
    30.                
    31.                 dbcmd.CommandText = "insert into Users (Nom,Prénom,Email,Password) values ('" + nom + "' , '" + prenom + "' , '" + Email + "' , '" + Password + "')";
    32.  
    33.                 dbcmd.ExecuteNonQuery ();
    34.                 dbcmd.Dispose ();
    35.                 dbconn.Close ();
    36.  
    37.                 }
    the errors says
     
  2. Adew95

    Adew95

    Joined:
    May 11, 2016
    Posts:
    1
    Hi Chemsoun, i'm not sure if you already resolve your problem, but i will tell you something that can be wrong with your code: You are using "reader.GetString(3);" but you are only getting one field from your Users table. The GetString(int i) method, Works whit the index 0 to x, then if you get only one field you have to use the 0 as a paramether. I hope i can help you. Greetings :D
     
  3. Hodaka31

    Hodaka31

    Joined:
    Aug 19, 2022
    Posts:
    1
    Where is the variable i?
     
  4. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    You do realize this thread is 7 years old correct?
     
    MelvMay likes this.