How to edit items in sqlite.net with Xamarin

Asked

Viewed 110 times

-1

I’m learning to use sqlite.net in the Xamarin, but when editing an item is not working, I debug the code and gave a breakpoint in the EditItens() and saw that the values passed to conexao.Query<Itens> are correct, but received in the variable Teste below continue the same as before, I’m missing something at the time of editing the values in sqlite? Also there may be something wrong in GetItens(), but I find it kind of hard because it works normally when used to take the values and show in the ListView of MainActivity. Below the methods cited

string pasta = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
public List<Itens> GetItens()
    {
        try
        {
            var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Itens.db3"));
            return conexao.Table<Itens>().ToList();
        }
        catch (Exception)
        {
            return null;
        }
    }

public bool EditItens(Itens itens)
    {
        try
        {
            var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Itens.db3"));
            conexao.Query<Itens>("UPDATE Itens set Nome=?,Preco=? Where Id=?", itens.Nome, itens.Preco, itens.Id);
            Teste = GetItens();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

If you could tell me where I can find the pasta, "Itens.db3" by Windows File Explorer, it will also help because I can check if the item really isn’t being edited.

  • I could not solve, but I rewrote another project from scratch and the error did not happen, I will leave the project here to analyze in the future pq still n understood pq was not going, but at least I know that "understood" sqlite

  • I was able to solve it, the error was in the past id, I already caught the id when doing Getitens() (in another part of the code), but even so I equalized again, I think somehow it caused the error, I realized that when I started the other project, agr I have two very similar projects and both working. The good is I fixed the content really

1 answer

1

Anderson, use this to update the data:

var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Itens.db3"));
            conexao.Execute("UPDATE Itens set Nome=?,Preco=? Where Id=?", 
itens.Nome, itens.Preco, itens.Id);

Optional and preferably, since you have the items object, use this:

conexao.Update(itens);

Your database will be located on the smartphone, in the folder that was set in

var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Itens.db3"));
  • I tried these models and it also didn’t work, the worst is that I tried the same line of code in a smaller project and it worked, just here it didn’t work, I updated the question with the address of the variable folder, can you tell me where that is? If I can know for sure if you are editing or cannot facilitate resolution

  • Is Getitens() returning any valid value? If this same section works in another application, check the previous statements of the variables outside this scope, for example, check if the connection is being called.Createtable<T>.

  • About checking the folder, put a Console.Writeline(path) in the application and check the output of your Visual Studio. You can try to access through a mobile client if the folder is visible, or through the solution of Andy’s answer in this other post: https://stackoverflow.com/questions/19194576/how-do-i-view-the-sqlite-database-on-an-android-device

  • the code I am creating serves both to write, as edit and delete, if I add any Item it updates normally, if I delete it updates normally, but when I edit, even not returning any exception, it remains intact, I’ll try to get the same code to edit and pick up items and simplify to see if the error continues, and on the folders I’m taking a look at this link q sent me

  • I’ll rewrite my project from scratch to see if the error continues

  • 1

    Since you are studying Xamarin it is still valid to rewrite the project to "train", try using methods with Sqlite objects, this will help you when you need to use inheritance in generic repositories.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.