locked
[UWP][SQLite][C#]My app frezze when use SQLite? RRS feed

  • Question

  • I'm using SQLite to read all records of a Table. I have put this code in a Click_button

    using(conn = SQLiteconnection )
    {
       //Read data from Table
    }

    The first time I tap Click_button it's work OK, but when I tap it one more time my app frezze?


    Tuesday, March 29, 2016 7:38 AM

Answers

  • Use Async & await instead of using "Result" for async functions.

    string path;        
            SQLiteConnection conn;
     private async void btLuu_Click(object sender, RoutedEventArgs e)
            {           
                string result = String.Empty;
                using (conn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
                {
                    if (await FileExists("db1.sqlite"))
                    {
                        conn.CreateTable<KilometManager>();
                    }                
                    CultureInfo viVn = new CultureInfo("vi-VN");
                    var toDay = DateTime.Now.Date;
                    //Convert.ToDateTime(toDay);
                    conn.Insert(new KilometManager() { Ngay = toDay.ToString("d",viVn), SoKmDiDuoc = double.Parse(txtDistance.Text) });                
                    conn.Dispose();
                }
                txtKetQua.Text = "Save done!";
            }

    Also it would be better if you  check whether "db1.sqlite" file exists or not, and CreateTable when app get started i.e. in Application.OnLaunched event instead of having that in every button's click event. 

    https://code.msdn.microsoft.com/Local-Data-Base-SQLite-for-5e6146aa#content

    https://msdn.microsoft.com/library/windows/apps/br242335


    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor


    Wednesday, March 30, 2016 1:22 PM

All replies

  • Hi,

    Please see: Using SQLite on the Universal Windows Platform

    >>The first time I tap Click_button it's work OK, but when I tap it one more time my app frezze?

    Are you getting any exeptions? Perhaps you can follow this MVVM pattern. 

    Please see: https://github.com/XamlBrewer/UWP-SQLite-Sample

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, March 30, 2016 8:49 AM
  • But when I run Debug it's work OK everytime? I don't know where are issue!
    Wednesday, March 30, 2016 9:20 AM
  • Can you paste here some more code to see what actually happens before & after that using statement.

    http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8

    Wednesday, March 30, 2016 10:29 AM
  • string path;       
            SQLiteConnection conn;

    private void btLuu_Click(object sender, RoutedEventArgs e) { string result = String.Empty; using (conn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path)) { if (FileExists("db1.sqlite").Result) { conn.CreateTable<KilometManager>(); } CultureInfo viVn = new CultureInfo("vi-VN"); var toDay = DateTime.Now.Date; //Convert.ToDateTime(toDay); conn.Insert(new KilometManager() { Ngay = toDay.ToString("d",viVn), SoKmDiDuoc = double.Parse(txtDistance.Text) }); conn.Dispose(); } txtKetQua.Text = "Save done!"; }

    And Read_Button

     

    string path;       
            SQLiteConnection conn;

    using (conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path)) { if (FileExists("db1.sqlite").Result) { string result = String.Empty; double TongCong = 0; CultureInfo viVn = new CultureInfo("vi-VN"); string queryString = "Select * From KilometManager Where Ngay >= '" + TuNgay.Date.ToString("d", viVn) + "' And Ngay <= '" + DenNgay.Date.ToString("d", viVn) + "'"; List<KilometManager> listKM = conn.Query<KilometManager>(queryString).ToList<KilometManager>(); foreach (var item in listKM) { TongCong += item.SoKmDiDuoc; } txtKetQua.Text = TongCong.ToString() + " Km"; } conn.Dispose(); }


    It's happen with 2 button, but when I run Debug it work fine


    Wednesday, March 30, 2016 12:49 PM
  • Use Async & await instead of using "Result" for async functions.

    string path;        
            SQLiteConnection conn;
     private async void btLuu_Click(object sender, RoutedEventArgs e)
            {           
                string result = String.Empty;
                using (conn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
                {
                    if (await FileExists("db1.sqlite"))
                    {
                        conn.CreateTable<KilometManager>();
                    }                
                    CultureInfo viVn = new CultureInfo("vi-VN");
                    var toDay = DateTime.Now.Date;
                    //Convert.ToDateTime(toDay);
                    conn.Insert(new KilometManager() { Ngay = toDay.ToString("d",viVn), SoKmDiDuoc = double.Parse(txtDistance.Text) });                
                    conn.Dispose();
                }
                txtKetQua.Text = "Save done!";
            }

    Also it would be better if you  check whether "db1.sqlite" file exists or not, and CreateTable when app get started i.e. in Application.OnLaunched event instead of having that in every button's click event. 

    https://code.msdn.microsoft.com/Local-Data-Base-SQLite-for-5e6146aa#content

    https://msdn.microsoft.com/library/windows/apps/br242335


    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor


    Wednesday, March 30, 2016 1:22 PM
  • Thank you, Khanna Gaurav!
    Wednesday, March 30, 2016 2:46 PM
  • Thanh you, Vineet24!
    Thursday, March 31, 2016 3:39 AM
  • Thank you, Krunal Parekh!
    Thursday, March 31, 2016 3:39 AM