- Home /
Is there anybody who can look into my code to help me in resolving my error? Data in Database is not updating.
HI I have an issue with regards to my update function. Is there someone who can look into my code and tell me my mistake? Here is my code:
public void UpdateUser()
{
string OU = OldUser.text;
string NU = NewUser.text;
if (OU == "" || NU == "")
{
Debug.Log("Don't Leave Any Blank");
}
else
{
using (IDbConnection dbConn = new SqliteConnection(con))
{
dbConn.Open();
using (IDbCommand dbCmd = dbConn.CreateCommand())
{
string sqlQuery = string.Format("SELECT * FROM UsersInformation WHERE username = (\"{0}\")", OU);
dbCmd.CommandText = sqlQuery;
using (IDataReader reader = dbCmd.ExecuteReader())
{
if (reader.Read() == true)
{
sqlQuery = string.Format("UPDATE UsersInformation set username = (\"{0}\") WHERE username = (\"{1}\")", NU, OU);
Debug.Log("One User Matched");
dbCmd.ExecuteScalar();
}
else
{
Debug.Log("No User Matched!");
}
reader.Close();
dbCmd.Dispose();
dbConn.Close();
}
}
}
}
}
I transferred dbCmd.ExuteScalar(); under reader.Close(); I don't have any error now but database is not updating.
You construct the update query command string at line 23, but you never use it. Looks like you probably need to add dbCmd.CommandText = sqlQuery;
at line 24. You do that at line 18 but that's irrelevant since you change sqlQuery afterwards.
An error occur, unity says "Cannot set CommandText while a DataReader is Active"
Ah yes, that'll be because you're trying to use dbCmd for something else while you're still using it. Just make a second call to CreateCommand so as to avoid that
Your answer
![](https://koobas.hobune.stream/wayback/20220612183551im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Looking for step-by-step manual how to configure SQL Lite on Unity 2017 ( or other database) 1 Answer
Firebase with Unity instantiate new data 1 Answer
Unity/C#/MongoDB - attempt to read past the end of the stream? 0 Answers
what database should i use 3 Answers
problem updating info to data base 0 Answers