4

I'm currently trying to port a working Windows 8 JavaScript app to a Windows 10 UAP app. In my Windows 8 app, I heavily used this SQLite wrapper and library: 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). 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? Thanks a lot!

cloudcrypt
  • 769
  • 5
  • 22
  • I wonder what the status is now. See this one week old comment: http://stackoverflow.com/questions/31733553/sqlite-windows-10-rtm-universal-app#comment51649951_31779999 – Eivind Gussiås Løkseth Aug 15 '15 at 10:09
  • Thanks for the link! I have been able to make https://github.com/doo/SQLite3-WinRT work in a Windows 10 universal app finally, as the WinJS is not defined error came from another mistake I made in the default.html file. I have a problem now when deploying to Windows 10 Mobile, and made a new question for it here: http://stackoverflow.com/questions/32032927/windows-10-universal-javascript-app-sqlite-in-windows-mobile-10 – cloudcrypt Aug 18 '15 at 02:22

2 Answers2

5

I tried using https://github.com/doo/SQLite3-WinRT on Windows 10 and found VS2015 Community Edition couldn't even load the project. Every time I tried to load it, VS would hang with "unloading project" showing in the status bar. Killing it via task manager was the only way out.

I found this sample app which implements SQLite in a Universal App. 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

  1. Download and unzip Universal JavaScript SQLite Sample
  2. Open in Visual Studio
  3. Click on the "Shared App" project group
  4. Expand "SQLite.Windows" > "References"
  5. Remove the reference to "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)
  8. Expand "SQLite.WindowsPhone" > "References"
  9. Remove the reference to "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)
  12. Compile
roryok
  • 9,325
  • 17
  • 71
  • 138
  • Thanks a lot for this! I was able to successfully port the Universal 8.1 component in the sample app to a Universal Windows 10 Component that builds and runs on my local device and when deploying to Windows 10 Mobile! However, do you know where I can find some documentation for the SQLite implementation (javascript methods/classes available, etc) somewhere? Thanks! – cloudcrypt Aug 20 '15 at 05:18
  • I don't think there is any. I just stumbled across it. The only additional info I can find is the related blog post (linked in the sample) - http://blogs.windows.com/buildingapps/2014/07/02/writing-a-sqlite-wrapper-component-for-universal-windows-apps/ – roryok Aug 20 '15 at 11:59
  • Yeah, that blog post explained it all to me basically. Thanks! Let me know if you would use my win10 universal component version of this (non-8.1 universal), since I can put it up on GitHub. – cloudcrypt Aug 20 '15 at 15:39
  • @cloudcrypt if you can put your project to github, it would be helpful to others like me. I am struggling with the similar problem. Thanks. – Dr. Atul Tiwari Aug 20 '15 at 17:25
  • @cloudcrypt please do, it would help me too! – roryok Aug 20 '15 at 19:45
  • 2
    Here you are: https://github.com/cloudcrypt/SQLite-Universal-WinJS-Component Hope it works, enjoy! – cloudcrypt Aug 20 '15 at 20:41
  • @cloudcrypt well done! FYI on your readme.md the example code still references SDKsample, you might want to remove that. – roryok Aug 21 '15 at 10:47
0

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 Phone 8.1: SQLite for Windows Phone 8.1
  • Windows 8.1: SQLite for Windows Runtime (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:

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);
        }
    }
}
talkitbr
  • 1,112
  • 9
  • 12