Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

SQLite Exception

Discussion in 'Scripting' started by Seaworth, Sep 11, 2019.

  1. Seaworth

    Seaworth

    Joined:
    Aug 15, 2019
    Posts:
    19
    Hi folks,

    One of the things that really seems to suck. I am trying to experiment with SQLite in just a separate app for testing purposes, but run into this exception error all the time. My code is:
    Code (CSharp):
    1. using SQLite4Unity3d;
    2. using System.Data;
    3. using UnityEngine;
    4.  
    5. public class SQLiteTest : MonoBehaviour
    6. {
    7.     //  JSONString = File.ReadAllText(Application.dataPath + "/Resources/QM1.json");
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         // string connection = "URI=file:" + Application.persistentDataPath + "/My_Database";
    13.         string connection = "file:" + Application.dataPath + "/My_Database.sqlite3";
    14.         Debug.Log("Älpha: " + connection);
    15.         IDbConnection dbcon = new SQLiteConnection(connection) as System.Data.IDbConnection;
    16.  
    17.         dbcon.Open();
    18.         IDbCommand dbcmd;
    19.         IDataReader reader;
    20.         dbcmd = dbcon.CreateCommand();
    21.         string q_CreateTable = "CREATE TABLE IF NOT EXISTS my_table (id INTEGER PRIMARY KEY, val INTEGER)";
    22.         dbcmd.CommandText = q_CreateTable;
    23.         reader = dbcmd.ExecuteReader();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.     }
    31. }
    32.  
    The resultant error is:

    SQLiteException: Could not open database file: file:C:/Users/user/Lite Riz/Assets/My_Database.sqlite3 (CannotOpen)
    SQLite4Unity3d.SQLiteConnection..ctor (System.String databasePath, SQLite4Unity3d.SQLiteOpenFlags openFlags, System.Boolean storeDateTimeAsTicks) (at Assets/Scripts/SQLite.cs:242)

    Am I tryng with a bad library or how do I fix this. Any help would be much appreciated.

    Thanks
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,420
    What is this file: for? Correct uri prefix for local filesystem is file://
    Also, do you really need this to be URI? Afair, sqlite accepts filesystem paths and connection string should be something like this "Data Source=c:\mydb.db;Version=3;". It also may be path issues, like spaces and so on. Copy your database to C:\mydb.db and try with this simple conn string if it works.
     
  3. Seaworth

    Seaworth

    Joined:
    Aug 15, 2019
    Posts:
    19
    I have tried various conn strings and as you see, the URI line is commented out. The only common result is the exception error. The file is a sqlite DB file containing math questions. The end goal is an Android app and the db file would be in streaming assets.