0
votes

I stuck with Xamarin.Forms SQLite.

i use this NuGet package : sqlite-net-pcl

My SQLITE Tables

I want to save the Data from my Table to a Variable with this following code

        SQLiteConnection myconnection = new SQLiteConnection(Constants.DatabasePath);

        SQLiteCommand cmd = new SQLiteCommand(myconnection);
        myconnection.CreateTable<Modul>();

        var Mods = myconnection.Query<Modul>("SELECT * FROM Modul");

Mods returns with = Count=0; But i have saved Data. Click here

My modul class: MoulID has Primary Key on Top [Primarykey]...

   public int ModulID { get; set; }

    public string Modul_Name { get; set; }

I will include my existing Database from Here

with this class

public class{

   public const string DatabaseFilename = "VocalDB.db";



    public static string DatabasePath
    {
        get
        {
            var basePath = "D:/C#-Projets/Vocabul/Vocabul/Vocabul/DataBase/";
            return Path.Combine(basePath, DatabaseFilename);
        }
    }

}

1
are you saving data from within your app? Or are you pre-populating the db with data using a query tool? - Jason
i have saved mock data to my db SQLitestudio. And i want simply read the data from Xamarin.Forms in my db. Later i want display the data in my app. - Denexful
If you want to include an existing db in your app, you generally need to copy it from the app bundle to a writable path at startup. If a db does not already exist at your path, SQLiteConnection will create an emtpy db for you. This is probably what is happening. - Jason
Thank you. When i give the SQLiteconnection the path to my Database: (cannot open) - Denexful
What path are you using? If you are getting an error, then how were you able to execute a query in your original post? - Jason

1 Answers

0
votes

In your code you create the table then you execute the query... I think the table is empty after you have created it.

  myconnection.CreateTable<Modul>();

        var Mods = myconnection.Query<Modul>("SELECT * FROM Modul");