How to Update/Edit data in existing recorded data using Sqlite and Xamarin android?

ozkaro 66 Reputation points
2021-04-18T23:28:44.507+00:00

Hi I'm having an issue using sqlite using my old code in my curren application it doesn´t update nothing
I'm trying to using the id of the selected data to set up the especified database row by the id but looks like it dont working

using (SQLite.SQLiteConnection con = new SQLite.SQLiteConnection(dbPath))
                {
                    SQLite.SQLiteCommand com = new SQLite.SQLiteCommand(con);
                    //com = con.CreateCommand("UPDATE JDictTable WHERE ID == ? SET isFavorite = '?'", id_, true);
                    com.CommandText = "UPDATE JDictTable SET isFavorite ='" + true + "' WHERE ID = '" +id_+ "'";

                    com.ExecuteNonQuery();
                    con.Close();
                }
               /* var tabcon = new SQLite.SQLiteAsyncConnection(dbPath);
                var sel = from i in tabcon.Table<DictDatabase>() where i.ID == id_ select i;
                var cur = sel.FirstOrDefaultAsync();
                cur.Result.isFavorite = true;
                tabcon.UpdateAsync(cur);*/

the multyline commented code also don't works

I see some tutorials on youtube and they create a new instance of the database class and update them but my database class has a lot of data and already recorded on the database file.

i think which my old windows phone application code still working but not

thanks a lot in advance

Developer technologies .NET Xamarin
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-04-19T02:44:53.44+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You should delete the " ` " symbol in your CommandText like following code.

       public bool UpdateTable(JDictTable jDictTable)  
               {  
                   try  
                   {  
                       using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "JDictTable.db")))  
                       {  
                            
         
                           SQLite.SQLiteCommand com = new SQLite.SQLiteCommand(connection);  
                            
                           com.CommandText = "UPDATE JDictTable SET isFavorite =" + true + " WHERE ID = " + jDictTable.Id + "";  
         
                           com.ExecuteNonQuery();  
                          // con.Close();  
         
         
                           return true;  
                       }  
                   }  
                   catch (SQLiteException ex)  
                   {  
                       Log.Info("SQLiteEx", ex.Message);  
                       return false;  
                   }  
               }  
    

    I make a test(I update item, id=2, name= test2). it is working, here is running screenshot.

    88912-image.png

    88913-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.