5

I've been trying to create a windows phone and I'd like to use SQLite to both store my data and learn how to use it on windows phone apps. For this purpose I'm using "SQLite.Net-PCL", but I keep getting a file not found exception. This the code I've written:

        String ConnectionString = Path.Combine(ApplicationData.Current.LocalFolder.Path, Connection);
        if (File.Exists(ConnectionString))
        {
            SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 e = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8();
            Con = new SQLiteConnection(e,ConnectionString);
        }

        else {
            SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 e = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8();
            File.Create(ConnectionString);
            Con = new SQLiteConnection(e, ConnectionString);               
        }

I thought maybe I get this error because I manually create an empty file but if that is the problem, how can I create a DB in case no database exists in the phone ?

JAX
  • 1,540
  • 3
  • 15
  • 32

1 Answers1

1

You don't need to create the file yourself, as the SQLiteConnection constructor manages that for you.

public SQLiteConnection(ISQLitePlatform sqlitePlatform, string databasePath, bool storeDateTimeAsTicks = false, IBlobSerializer serializer = null)
    : this(
        sqlitePlatform, databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks, serializer)
{
}

So you should just open the connection, create the tables and that should be that.

class ExampleDataContext
{
    public const string DATABASE_NAME = "data.sqlite";
    private SQLiteConnection connection;

    public TableQuery<Foo> FooTable { get; private set; }
    public TableQuery<Bar> BarTable { get; private set; }

    public ExampleDataContext()
    {
        connection = new SQLiteConnection(new SQLitePlatformWinRT(), Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, DATABASE_NAME));

        Initialize();

        FooTable      = connection.Table<Foo>();
        BarTable       = connection.Table<Bar>();
    }

    private void Initialize()
    {
        connection.CreateTable<Foo>();
        connection.CreateTable<Bar>();
    }
}

Don't worry about that Initialize, the tables only get created when they're not there yet.

Benjamin Diele
  • 1,177
  • 1
  • 10
  • 26
  • Is SQLitePlatformWinRT() supports WP8.1? If yes, where is it? If no, where is the platform for WP8.1? – Vahid Dec 16 '14 at 13:51
  • 1
    @JohnCroneh I think it was not included by default at the time of writing. I downloaded the source and added the build target manually. – Benjamin Diele Dec 16 '14 at 14:00
  • Thanks for the reply. Where is the source exactly? – Vahid Dec 16 '14 at 14:02
  • 1
    @JohnCroneh You can find the source [on github](https://github.com/oysteinkrog/SQLite.Net-PCL) – Benjamin Diele Dec 16 '14 at 14:41
  • Unfortunately the WP8.1 Folder is empty! What am I suppose to do? – Vahid Dec 17 '14 at 05:55
  • 1
    @JohnCroneh I **think** I used the [WinRT](https://github.com/oysteinkrog/SQLite.Net-PCL/tree/master/src/SQLite.Net.Platform.WinRT) folder for windows phone. Changing the build target made it work. If it doesn't, let me know and I'll try to look into it this week. – Benjamin Diele Dec 17 '14 at 06:55
  • 1
    Thank you. It works now with the WinRT folder. I'm not testing the methods but the project built without error. – Vahid Dec 17 '14 at 09:39