How to connect sqlite db to unity

2019-08-31 04:13发布

I have a preloaded db file, which is present in c drive in my system. If I connect that db file to unity project and try to get data from tables it didn't get any data and I am not getting any exceptions also. If I open this db file (which is present in c drive) with sqlite admin software it shows data in tables. Please suggest any idea, what I am missing. Here is the code I am using for connecting database to my unity project.

   Public void OpenDatabase()
   {
     var dbfileName = "Testuser.db"

     var _filePath = File.GetFullPath(dbfileName);

     var _connectionString = "URI=file:" + _filePath;

     dbcon = new SqliteConnection(_connectionString);

     dbcon.Open();
   }

When I am trying to create and read db file in the following manner working well.

Working code :

     Public void OpenDatabase()
     {

       var _filePath = Application.PersistanceDataPath + "/" + "owndbfile.db";

       if (!File.Exists(_filePath))
        {                                              
            WWW loadDB = new WWW("jar:file://" + Application.PersistanceDataPath + "!/assets/" + "owndbfile.db");

            while (!loadDB.isDone) { }

            // then save to Application.persistentDataPath

            File.WriteAllBytes(_filePath, loadDB.bytes);
        }

       var _connectionString = "URI=file:" + _filePath;

       dbcon = new SqliteConnection(_connectionString);

       dbcon.Open();
   }

Please suggest any idea for reading data from tables, if db file is present in one of the drive in a system instead of a present in "application persistance" path.

0条回答
登录 后发表回答