简体   繁体   English

Windows 10 Universal App SQLite

[英]Windows 10 Universal App SQLite

I'm currently trying to port a working Windows 8 JavaScript app to a Windows 10 UAP app. 我目前正在尝试将工作的Windows 8 JavaScript应用程序移植到Windows 10 UAP应用程序。 In my Windows 8 app, I heavily used this SQLite wrapper and library: https://github.com/doo/SQLite3-WinRT . 在我的Windows 8应用程序中,我大量使用了这个SQLite包装器和库: https//github.com/doo/SQLite3-WinRT However, after adding SQLite3-WinRT to my Windows 10 UAP app as per the setup instructions in the readme of the repo, I get a "WinJS is not defined" error coming from the SQLite3.js source file that I added to my /js directory in the app (the way it works fine in the windows 8 app). 但是,根据repo自述文件中的设置说明将SQLite3-WinRT添加到我的Windows 10 UAP应用程序后,我收到来自我添加到my / js的SQLite3.js源文件的“WinJS未定义”错误应用程序中的目录(它在Windows 8应用程序中正常工作的方式)。 Am I doing something wrong here, or wild this SQLite3-WinRT not work with Win 10 UAP and is there some better way of using SQLite in a JavaScript Windows 10 UAP app? 我在这里做错了什么,或者这个SQLite3-WinRT无法与Win 10 UAP一起使用,是否有更好的方法在JavaScript Windows 10 UAP应用程序中使用SQLite? Thanks a lot! 非常感谢!

I tried using https://github.com/doo/SQLite3-WinRT on Windows 10 and found VS2015 Community Edition couldn't even load the project. 我尝试在Windows 10上使用https://github.com/doo/SQLite3-WinRT ,发现VS2015 Community Edition甚至无法加载项目。 Every time I tried to load it, VS would hang with "unloading project" showing in the status bar. 每次我尝试加载它时,VS都会挂起状态栏中显示的“卸载项目”。 Killing it via task manager was the only way out. 通过任务管理器杀死它是唯一的出路。

I found this sample app which implements SQLite in a Universal App. 我找到了这个在通用应用程序中实现SQLite的示例应用程序。 This compiles and runs fine for me on Windows 10, although I did have to update the references to SQLite 3.8.4.3 with the version I had, SQLite 3.8.11.1 这在Windows 10上为我编译并运行正常,尽管我必须使用我的版本更新对SQLite 3.8.4.3的引用,SQLite 3.8.11.1

  1. Download and unzip Universal JavaScript SQLite Sample 下载并解压缩Universal JavaScript SQLite示例
  2. Open in Visual Studio 在Visual Studio中打开
  3. Click on the "Shared App" project group 单击“共享应用程序”项目组
  4. Expand "SQLite.Windows" > "References" 展开“SQLite.Windows”>“参考”
  5. Remove the reference to "SQLite.WinRT81, Version=3.8.4.3 删除对“SQLite.WinRT81,Version = 3.8.4.3”的引用
  6. Right-click > "Add Reference" 右键单击>“添加引用”
  7. From Windows 8.1 > Extensions, select "SQLite for Windows Runtime (Windows 8.1) 从Windows 8.1> Extensions中,选择“SQLite for Windows Runtime(Windows 8.1)
  8. Expand "SQLite.WindowsPhone" > "References" 展开“SQLite.WindowsPhone”>“参考”
  9. Remove the reference to "SQLite.WP81, Version=3.8.5 删除对“SQLite.WP81,Version = 3.8.5”的引用
  10. Right-click > "Add Reference" 右键单击>“添加引用”
  11. From Windows 8.1 > Extensions, select "SQLite for Windows Runtime (Windows 8.1) 从Windows 8.1> Extensions中,选择“SQLite for Windows Runtime(Windows 8.1)
  12. Compile

If you are looking for a step by step procedure, you can take a look at this post . 如果您正在寻找一步一步的程序,您可以看一下这篇文章

You can use it with: 您可以使用它:

  • Windows 10: SQLite for Universal App Platform Windows 10:适用于通用应用平台的SQLite
  • Windows Phone 8.1: SQLite for Windows Phone 8.1 Windows Phone 8.1:适用于Windows Phone 8.1的SQLite
  • Windows 8.1: SQLite for Windows Runtime (Windows 8.1) Windows 8.1:适用于Windows运行时的SQLite(Windows 8.1)

After setting up the packages and extensions, you are able to create the database and the models classes using: 设置包和扩展后,您可以使用以下命令创建数据库和模型类:

using System.IO;
using System.Threading.Tasks;
using SQLiteModernApp.DataModel;
using SQLite.Net.Async;
using System;
using SQLite.Net;
using SQLite.Net.Platform.WinRT;

namespace SQLiteModernApp.DataAccess
{
    public class DbConnection : IDbConnection
    {
        string dbPath;
        SQLiteAsyncConnection conn;

        public DbConnection()
        {
            dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Sample.sqlite");

            var connectionFactory = new Func<SQLiteConnectionWithLock>(() => new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: false)));
            conn = new SQLiteAsyncConnection(connectionFactory);
        }

        public async Task InitializeDatabase()
        {
            await conn.CreateTableAsync<Department>();
            await conn.CreateTableAsync<Employee>();
            await conn.CreateTableAsync<EmployeeDepartment>();
        }

        public SQLiteAsyncConnection GetAsyncConnection()
        {
            return conn;
        }
    }
}

Now you can create a CRUD, following the example: 现在您可以按照示例创建一个CRUD:

using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite.Net.Async;
using SQLiteModernApp.DataAccess;
using SQLiteModernApp.DataModel;
using SQLiteNetExtensionsAsync.Extensions;

namespace SQLiteModernApp.Repository
{
    public class EmployeeRepository : IEmployeeRepository
    {
        SQLiteAsyncConnection conn;

        public EmployeeRepository(IDbConnection oIDbConnection)
        {
            conn = oIDbConnection.GetAsyncConnection();
        }

        public async Task InsertEmployeeAsync(Employee employee)
        {
            await conn.InsertOrReplaceWithChildrenAsync(employee);
        }

        public async Task UpdateEmployeeAsync(Employee employee)
        {
            await conn.UpdateWithChildrenAsync(employee);
        }

        public async Task DeleteEmployeeAsync(Employee employee)
        {
            await conn.DeleteAsync(employee);
        }

        public async Task<List<Employee>> SelectAllEmployeesAsync()
        {
            return await conn.GetAllWithChildrenAsync<Employee>();
        }

        public async Task<List<Employee>> SelectEmployeesAsync(string query)
        {
            return await conn.QueryAsync<Employee>(query);
        }
    }
}

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

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