SQLite Database

Not much going on this weekend, did not have much free time. I did start setting up a database so I build an unlocking system for levels and themes.

Added the ‘SQLite-net-pcl’ and ‘SQLite.Core.UAP’ NuGet packages to my Universal Windows game. So far just figuring out how and where to save the database.

Just started so this is far from finished code.

using System;
using System.IO;
using System.Collections.Generic;
using SQLite;
 
namespace MemoryLapse
{
    public class Database
    {
        public String Name { getset; }
        public String Path { getset; }
        public String ConnectionString { getset; }
 
        public List<Tables.Level> Levels { getset; }
 
        public Database(String name)
        {
            Name = name;
            Path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
            ConnectionString = System.IO.Path.Combine(Path, Name);
 
            bool exist = File.Exists(ConnectionString);
 
            using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
            {
                #region Create Database
                if (!exist)
                {
                    connection.CreateTable(typeof(Tables.Level));
 
                    for (int t = 0; t < Enum.GetValues(typeof(Theme)).Length; t++)
                    {
                        for (int l = 0; l < Enum.GetValues(typeof(Level)).Length; l++)
                        {
                            Tables.Level level = new Tables.Level();
 
                            level.Theme = t;
                            level.LevelNumber = l;
                            level.Unlocked = 0;
 
                            if (level.Theme == 0 && level.LevelNumber == 0)
                                level.Unlocked = 1;
 
                            connection.Insert(level);
                        }
                    }
                }
                #endregion
 
                Levels = connection.Query<Tables.Level>("select * from Level"new Object[] { });
            }
        }
    }
}
namespace MemoryLapse.Tables
{
    public class Level
    {
        public int Theme { getset; }
        public int LevelNumber { getset; }
        public int Unlocked { getset; }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*