Git Product home page Git Product logo

unitysqlite's Introduction

Gameframe.SQLite ๐Ÿ‘‹

Codacy Badge GitHub release (latest by date including pre-releases) openupm GitHub

twitter

SQLite Package

Quick Package Install

Using UnityPackageManager (for Unity 2019.3 or later)

Open the package manager window (menu: Window > Package Manager)
Select "Add package from git URL...", fill in the pop-up with the following link:
https://github.com/coryleach/UnitySQLite.git#1.0.2

Using UnityPackageManager (for Unity 2019.1 or later)

Find the manifest.json file in the Packages folder of your project and edit it to look like this:

{
  "dependencies": {
    "com.gameframe.sqlite": "https://github.com/coryleach/UnitySQLite.git#1.0.2",
    ...
  },
}

Usage

using Mono.Data.SqliteClient;

// Create database
string path = Application.persistentDataPath + "/" + "My_Test_Database";
string connection = "URI=file:" + path;

// Open connection
IDbConnection dbcon = new SqliteConnection(connection);
dbcon.Open();

// Create table
IDbCommand dbcmd;
dbcmd = dbcon.CreateCommand();
string q_createTable = "CREATE TABLE IF NOT EXISTS my_table (id INTEGER PRIMARY KEY, val INTEGER )";

dbcmd.CommandText = q_createTable;
dbcmd.ExecuteReader();

// Insert values in table
IDbCommand cmnd = dbcon.CreateCommand();
cmnd.CommandText = "INSERT INTO my_table (id, val) VALUES (0, 5)";
cmnd.ExecuteNonQuery();

// Read and print all values in table
IDbCommand cmnd_read = dbcon.CreateCommand();
IDataReader reader;
string query ="SELECT * FROM my_table";
cmnd_read.CommandText = query;
reader = cmnd_read.ExecuteReader();

while (reader.Read())
{
    Debug.Log("id: " + reader[0].ToString());
    Debug.Log("val: " + reader[1].ToString());
}

// Close connection
dbcon.Close();

Author

๐Ÿ‘ค Cory Leach

Show your support

Give a โญ๏ธ if this project helped you!


This README was generated with โค๏ธ by Gameframe.Packages

unitysqlite's People

Contributors

coryleach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

unitysqlite's Issues

DLL file origin?

Where can I find the original source of these files?

Mono.Data.Sqlite.dll
Mono.Data.SqliteClient.dll

ROW_NUMBER() throws error from unity, but works fine in DB Browser

running this sql statement in DB Browser for SQLite, runs without errors.

Running it from within unity throws:

SqliteSyntaxException: near "(": syntax error
Mono.Data.SqliteClient.SqliteCommand.GetNextStatement (System.IntPtr pzStart, System.IntPtr& pzTail, System.IntPtr& pStmt) (at <4250a02233a7405f9f3a59c51fdc2ac7>:0)
Mono.Data.SqliteClient.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior, System.Boolean want_results, System.Int32& rows_affected) (at <4250a02233a7405f9f3a59c51fdc2ac7>:0)
Mono.Data.SqliteClient.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) (at <4250a02233a7405f9f3a59c51fdc2ac7>:0)
Mono.Data.SqliteClient.SqliteCommand.ExecuteDbDataReader (System.Data.CommandBehavior behavior) (at <4250a02233a7405f9f3a59c51fdc2ac7>:0)
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader () (at :0)

SQL:

SELECT 
  RunID, 
  Rank,  
  Timestamp,
  Score
FROM (
  SELECT 
    RunID, 
    ROW_NUMBER() OVER (ORDER BY Score DESC) AS Rank,    
    Timestamp,
	Score,
    CASE 
      WHEN Timestamp = (
        SELECT MAX(Timestamp) 
        FROM Stats         
      ) THEN 1 
      ELSE 0 
    END AS IsLatest
  FROM Stats  
) 
ORDER BY IsLatest DESC, Score DESC
LIMIT 10

C# Code:

 [ContextMenu("RowNumberTest")]
 public void SchemaTest()
 {
     path = Application.persistentDataPath + "/" + "Stats.sqlite";
     connection = "URI=file:" + path;
     // Open connection
     dbcon = new SqliteConnection(connection);
     dbcon.Open();

     // Create table
     IDbCommand dbcmd;
     dbcmd = dbcon.CreateCommand();
     string q_createTable = @"
     CREATE TABLE IF NOT EXISTS Stats (
         RunID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,       
         Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,	           
         Score REAL	            
     )";

     dbcmd.CommandText = q_createTable;
     dbcmd.ExecuteReader();

     string query = $"SELECT \r\n  RunID, \r\n  Rank,  \r\n  Timestamp,\r\n  Score\r\nFROM (\r\n  SELECT \r\n    RunID, \r\n    ROW_NUMBER() OVER (ORDER BY Score DESC) AS Rank,    \r\n    Timestamp,\r\n\tScore,\r\n    CASE \r\n      WHEN Timestamp = (\r\n        SELECT MAX(Timestamp) \r\n        FROM Stats         \r\n      ) THEN 1 \r\n      ELSE 0 \r\n    END AS IsLatest\r\n  FROM Stats  \r\n) \r\nORDER BY IsLatest DESC, Score DESC\r\nLIMIT 10";
     Debug.Log($"Select query: {query}");
     IDbCommand cmnd_read = dbcon.CreateCommand();
     cmnd_read.CommandText = query;
     IDataReader reader = cmnd_read.ExecuteReader();

     List<Dictionary<string, object>> statsList = new List<Dictionary<string, object>>();
     while (reader.Read())
     {
         Dictionary<string, object> statsDict = new Dictionary<string, object>();
         for (var i = 0; i < reader.FieldCount; i++)
         {
             statsDict.Add(reader.GetName(i), reader[i]);
             Debug.Log($"{reader.GetName(i)} {reader[i]}");
         }
         statsList.Add(statsDict);
     }
 }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.