• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by jXD · Jul 23, 2013 at 10:10 AM · databaseexceptionsqlitelocked

Sqlite databases getting locked by Unity.

Hi guys,

Whenever I try to input values into my sqlite database from unity, it always throws an exception: SqliteException: The database file is locked I still can read from it, but whenever I try to write into the db, Unity will hang for 10 secs and then that error pops up in the console. Here is my code:

 public SqliteDataReader InsertInto(string tableName, string[] values)
     {
         string query = "INSERT INTO " + tableName + " VALUES(" + values[0];
         for (int i = 1; i < values.Length; ++i)
         {
             query += ", " + values[i];
         }
         query += ")";
         Debug.Log("Inserted!");
         return ExecuteQuery(query);
     }

I also understand that t$$anonymous$$s might occur due to the closing of connections being wrong(w$$anonymous$$ch I $$anonymous$$ghly doubt so, but to be sure heres the closing code:

 public void CloseSqlConnection()
     {
         reader.Close();
         reader = null;
         dbCommand.Dispose();
         dbCommand = null;
         dbConnection.Close();
         dbConnection = null;
         GC.Collect();
         Debug.Log("Disconnected from db.");
     }

T$$anonymous$$s problem occurs even when I restart my PC, and launch Unity for the first time, so I doubt its the connection problem. Also, the database is in the proper folder, and it works fine(as I can read from it). It was working fine a few days back, and now it just decided to break.

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Jul 23, 2013 at 10:56 AM 0
Share

Where/how often is your CloseSqlConnection() called?

avatar image Graham-Dunnett ♦♦ · Jul 23, 2013 at 10:59 AM 0
Share

If you use the sqlite command line app, are you able to edit the database directly. I mean, is this uniquely a problem when you connect from script code. Unity itself doesn't know what a sqlite db is. I'd assume it's a permissions thing on the db.

avatar image jXD · Jul 24, 2013 at 11:36 PM 0
Share

I'm using the sqlite manager script from the unify wiki. I found the c# version on the unity forums some time back. As for the closesqlconnection(), I'm using this from an editorwindow, so I call it every onlostfocus() and reconnect every onfocus().

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Jamora · Jul 25, 2013 at 09:04 AM

I made a little test script to see how many times OnFocus is called when the window is opened the first time. Turns out it is called twice. So the reason why your database is locked for writing is because you have an open, possibly leaked connection to it. After the first time On(Lost)Focus behaves as expected.

I t$$anonymous$$nk somet$$anonymous$$ng along these lines would work better:

 [MenuItem("Item/Item")]
 public static void Init(){
     GetWindow<TestScript>();
     //Initialize connection.
 }
 
 void OnFocus(){
     //If (connection == null)
     //Initialize connection.
 }
 
 void OnLostFocus(){
     //Close connection.
 }
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jXD · Jul 27, 2013 at 01:22 AM 0
Share

Thanks, that worked perfectly. I also found that running as admin solves it temporarily, but changing my script to that worked better. Thanks!

avatar image

Answer by jXD · Jul 24, 2013 at 11:40 PM

I'm using the sqlite manager script from the unify wiki. I found the c# version on the unity forums some time back. As for the closesqlconnection(), I'm using t$$anonymous$$s from an editorwindow, so I call it every onlostfocus() and reconnect every onfocus().

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to connect my Application to SQLite Database? 1 Answer

NullReferenceException help 1 Answer

Implementing an offline Inventory that must sync with a server 0 Answers

Do I need Unity Android Pro to find sqlite3.dll 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges