Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have a small doubt about using SQLite database and local settings. I have an idea about how to use local settings for an app. When we use Application data(local settings) in our application we can restore the previous state of the application. Can we use sqlite local database to store and retrieve the values while we use local settings.

C#
Windows.Storage.ApplicationDataContainer data = Windows.Storage.ApplicationData.Current.LocalSettings; 
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            
            if (data.Values["check"] != null)
            {
                this.Frame.Navigate(typeof(BlankPage1));
            }
                
          if (data.Values["add"] != null)
          {
              list_view.Items.Add(data.Values["add"].ToString());
          }
              
            if (data.Values["remove"] != null)
            {
                list_view.Items.Remove(data.Values["remove"]);
            }
            var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
            var con = new SQLiteAsyncConnection(dbpath);

            await con.CreateTableAsync<list>();
                
        }
       

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           if(!mypop.IsOpen)
           {
               mypop.IsOpen = true;
           }
            
        }

        public async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
            var con = new SQLiteAsyncConnection(dbpath);
            
            
            list l = new list();
            data.Values["add"] = l.list1;
            l.list1 = text_input.Text.ToString();
            await con.InsertAsync(l);
            await con.UpdateAllAsync(l.list1);
            data.Values["add"] = l.list1;
            list_view.Items.Add(data.Values["add"].ToString());
             

            
           // list_view.Items.Add(text_input.Text.ToString());
          mypop.IsOpen = false;
          
        }
       

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if(mypop.IsOpen)
            {
                mypop.IsOpen = false;
            }
        }

        private async void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
            var con = new SQLiteAsyncConnection(dbpath);
            
            list l = new list();
            
            data.Values["remove"] = l.list1;
            
            l.list1 = list_view.SelectedItem.ToString();
            
            list_view.Items.Remove(list_view.SelectedItem);
            List<list> mylist = await con.QueryAsync<list>("delete from list where list1='" + list_view.SelectedItem + "'");
            mylist.Remove(l);


Please check and verify the code. I can only store single value to local settings. When I restart my application only one value will be shown. It wont show the values inserted in the listview. Please also check delete query. It is not working.. please help me...
Posted
Comments
Richard Deeming 17-Sep-15 13:50pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
sandy.L 18-Sep-15 2:04am    
So please help me how to insert and delete the list items from database. Only one value is restored while I restart my app from local settings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900