Ram Prasad

Ram Prasad

  • NA
  • 326
  • 16.8k

Compare data with the SQLite database

Jan 20 2021 6:17 AM
Compare data with the SQLite database, display the output if data exist in database and insert in database if does not exist in database
 
I am getting a string of words as output in `returnValue` which I want to compare to a SQLite database. If the value of `returnValue` exists and starts with the letter 'k' in the database then output gets displayed. If `returnValue` starts with the letter 'k' and does not exist in database then it gets added into the database. Any other string starting with any other letter does not do anything.
 
For example, assuming 'Kevin' already exists in the database, if the output in `returnValue` is Kevin then Kevin must show in `Console.Write`. If output in `returnValue` is 'Kelly' (which does not exist in the database) then it gets added into the database. If the output of `returnValue` is 'John', nothing happens.
 
I tried the following code, but it is not working.
  1. string returnValue = string.Join("", dataResult.ToArray());  
  2. Database databaseObject = new Database();  
  3. databaseObject.OpenConnection();  
  4. SQLiteCommand cmd = new SQLiteCommand();  
  5. SQLiteDataReader dataReader;  
  6. if (returnValue.StartsWith('k'))  
  7. {  
  8. string value = cmd.ExecuteScalar() as string;  
  9. if (returnValue.Equals(value))  
  10. {  
  11. databaseObject.OpenConnection();  
  12. using (cmd = new SQLiteCommand("SELECT * FROM k", databaseObject.myConnection))  
  13. {  
  14. dataReader = cmd.ExecuteReader();  
  15. while (dataReader.Read())  
  16. {  
  17. Console.Write(Data);  
  18. }  
  19. }  
  20. }  
  21. else  
  22. {  
  23. query = "INSERT INTO k ('Data') VALUES (@Data)";  
  24. cmd.CommandText = query;  
  25. cmd.Parameters.AddWithValue("@Data", returnValue);  
  26. }  
  27. databaseObject.CloseConnection();  
  28. }  
  29. else  
  30. {  
  31. return;  
  32. }  

Answers (4)