简体   繁体   English

哪种方法是使用SQLite DB的最佳方法?

[英]Which is the best way to use SQLite DB?

am developing a Windows Store 8.1 app. 正在开发Windows Store 8.1应用程序。

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. 我想在我的应用程序中本地保存一些数据,所以我使用SQLite创建了一个数据库并在其中添加了一些表,并从代码中添加了一些数据。

Which one is the best option from the following? 以下哪一项是最佳选择?

1) Creating the DB from the code for the first time opening the app 1)首次通过代码从代码创建数据库

2) Attaching the DB file with predefined structure in the app with in the solution folder. 2)在解决方案文件夹中的应用程序中附加具有预定义结构的DB文件。

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 . 无需担心每次应用启动时都会不必要地创建表,因为SQLiteConnection.CreateTable 在幕后使用了create if not exists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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