简体   繁体   中英

Which is the best way to use SQLite DB?

am developing a Windows Store 8.1 app.

I want to save some data locally in my app, so am using SQLite i created a DB and added few tables in that and i have added some data to that from Code.

Which one is the best option from the following?

1) Creating the DB from the code for the first time opening the app

2) Attaching the DB file with predefined structure in the app with in the solution folder.

Please suggest me the best solution for this.

For my own apps I use the first option: creating the database when the app's been opened for the first time.

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

        Initialize();
    }

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

There's no need to worry that you'll needlessly create tables everytime the app starts, because SQLiteConnection.CreateTable uses a create if not exists behind the scenes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM