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 ?

1
  • What's the value of ConnectionString, it's not obvious from your code? Commented Jul 1, 2014 at 21:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Is SQLitePlatformWinRT() supports WP8.1? If yes, where is it? If no, where is the platform for WP8.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.
@JohnCroneh You can find the source on github
@JohnCroneh I think I used the 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.
Thank you. It works now with the WinRT folder. I'm not testing the methods but the project built without error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.