Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if table exist using SQLite-PCL in a UWP

Tags:

c#

sqlite

uwp

I'm stuck with how to check if a table already exists. I have been searching but like many times before I can't find good examples.

The ones I find on SQLite don't work with the PCL version. I can't understand why though, so if anyone has a good site where to go please feel free to add them.

These are the ones I have used:

  • http://blogs.u2u.be/diederik/post/2015/09/08/Using-SQLite-on-the-Universal-Windows-Platform.aspx

  • https://code.msdn.microsoft.com/windowsapps/Implement-SQLite-Local-8b13a307#content

This is my code showing how I have tried to check it, but it only checks the path, which always exists. Not a smart solution when I thought about it.

private void LikeItButton_Click(object sender, RoutedEventArgs e)
        {
            var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Filmdb.sqlite");

            using (SQLite.Net.SQLiteConnection conn =
                new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))
            {
                if (File.Exists(sqlpath))
                {
                    AdMovieID();
                }
                else
                {
                    conn.CreateTable<MovieID>();
                    AdMovieID();
                }
            }
        }
like image 737
Newbie1337 Avatar asked Sep 24 '15 10:09

Newbie1337


People also ask

What is SQLite PCL?

SQLite-net is an open source and light weight library providing easy SQLite database storage for . NET, Mono, and Xamarin applications. This version uses SQLitePCLRaw to provide platform independent versions of SQLite.


1 Answers

You could execute a query:

SELECT name FROM sqlite_master WHERE type='table' AND name='MovieId';

by doing

var tableExistsQuery = "SELECT name FROM sqlite_master WHERE type='table' AND name='MovieId';"
var result = conn.ExecuteScalar<string>(tableExistsQuery);
like image 184
Igor Ralic Avatar answered Oct 16 '22 13:10

Igor Ralic