Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sqlite Exception: The database file is locked

Discussion in 'Scripting' started by SuperTG005, Apr 24, 2018.

  1. SuperTG005

    SuperTG005

    Joined:
    Apr 7, 2018
    Posts:
    1
    I am trying to update my query in my database but it keep saying: Sqlite Exception: The database file is locked.
    Can someone help me fix this?
    The error is on line 31 from my script, I am trying to update my query but this mistake pops upi


    SqliteException: The database file is locked
    database is locked
    Mono.Data.Sqlite.SQLite3.Step (Mono.Data.Sqlite.SqliteStatement stmt)
    Mono.Data.Sqlite.SqliteDataReader.NextResult ()
    (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteDataReader:NextResult ()
    Mono.Data.Sqlite.SqliteDataReader..ctor (Mono.Data.Sqlite.SqliteCommand cmd, CommandBehavior behave)
    (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteDataReader:.ctor (Mono.Data.Sqlite.SqliteCommand,System.Data.CommandBehavior)
    Mono.Data.Sqlite.SqliteCommand.ExecuteReader (CommandBehavior behavior)
    Mono.Data.Sqlite.SqliteCommand.ExecuteScalar ()
    DatabaseManager.InsertScore () (at Assets/Scripts/DatabaseManager.cs:31)
    DatabaseManager.Start () (at Assets/Scripts/DatabaseManager.cs:16)

    This is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Data;
    6. using Mono.Data.Sqlite;
    7.  
    8. public class DatabaseManager : MonoBehaviour {
    9.  
    10.     private string connectionString;
    11.     public GameObject ScoredArea;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         connectionString = "URI=file:" + Application.dataPath + "/ScoreDB.sqlite";
    16.         InsertScore ();
    17.         GetScores ();
    18.     }
    19.  
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.     }
    24.  
    25.     private void InsertScore() {
    26.         using (IDbConnection dbConnection = new SqliteConnection (connectionString)) {
    27.             dbConnection.Open ();
    28.             using (IDbCommand dbCmd = dbConnection.CreateCommand ()) {
    29.                 string sqlQuery = "update scores set score=score+1";
    30.                 dbCmd.CommandText = sqlQuery;
    31.                 dbCmd.ExecuteScalar();
    32.                 dbConnection.Close();
    33.             }
    34.         }
    35.     }
    36.  
    37.     private void GetScores() {
    38.         using (IDbConnection dbConnection = new SqliteConnection (connectionString)) {
    39.             dbConnection.Open ();
    40.             using (IDbCommand dbCmd = dbConnection.CreateCommand ()) {
    41.                 string sqlQuery = "select * from Scores";
    42.                 dbCmd.CommandText = sqlQuery;
    43.                 using (IDataReader reader = dbCmd.ExecuteReader ()) {
    44.                     while (reader.Read ()) {
    45.                         Debug.Log (reader.GetInt16 (1));
    46.                     }
    47.                     reader.Close ();
    48.                     dbConnection.Close();
    49.              
    50.                 }
    51.             }
    52.         }
    53.     }
    54. }
     
  2. guayeah

    guayeah

    Joined:
    Jun 9, 2017
    Posts:
    6
    Try to put your database on a new folder (inside the "Assets" folder) and call it StreamingAssets, then insert this function on your star method:

    Code (CSharp):
    1. public class DataBaseManager : MonoBehaviour
    2. {
    3.     private string rutaDB;
    4.     private string strConexion;
    5.     private string DBFileName = "YOUR DATABASE NAME";
    6. void Start()
    7. {
    8.         CheckPlatform();
    9.         InsertScore ();
    10.         GetScores ();
    11. }
    12. void CheckPlatform()
    13.     {
    14.         //Check what platform is running the app
    15.         if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
    16.         {
    17.             rutaDB = Application.dataPath + "/StreamingAssets/" + DBFileName;
    18.         }
    19.         else if (Application.platform == RuntimePlatform.IPhonePlayer)
    20.         {
    21.             rutaDB = Application.dataPath + "/Raw/" + DBFileName;
    22.         }
    23.         else if (Application.platform == RuntimePlatform.Android)
    24.         {
    25.             rutaDB = Application.persistentDataPath + "/" + DBFileName;
    26.             //Check if your file is stored in persistent data
    27.             if (!File.Exists(rutaDB))
    28.             {
    29.                 WWW loadDB = new WWW("jar;file://" + Application.dataPath + "!/assets/" + DBFileName);
    30.                 while (!loadDB.isDone)
    31.                 { }
    32.                 File.WriteAllBytes(rutaDB, loadDB.bytes);
    33.             }
    34.         }
    35.     }
    36. }
     
  3. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507