mercredi 17 février 2016

I'm not able to start a SqliteTransaction inside a thread

Well, I already created a functional insert on sqlite script. The problem is when i try to do it on a thread. I have no error message and the code is not passing the line "using (_dbTransaction = dbConnection.BeginTransaction())". Here follows the code (OBS: I'm using Unity 5.3.2p1):

private void CheckExistingDataThread(string p_tableName, List<Dictionary<string, string>> p_listData, string[] p_whereKeys)
{
    Debug.Log("CheckExistingDataThread");
    using (_dbConnection = new SqliteConnection(_dbURI))
    {
        Debug.Log("Database Loaded");
        using (_dbCommand = _dbConnection.CreateCommand())
        {
            Debug.Log("Command Created");
            using (_dbTransaction = _dbConnection.BeginTransaction())
            {
                Debug.Log("Transaction Started");
                _dbCommand.Connection = _dbConnection;
                _dbCommand.Transaction = _dbTransaction;
                for (int i = 0; i < p_listData.Count; i ++)
                { 
                    string __sql = string.Empty;
                    __sql = "SELECT COUNT() FROM " + p_tableName + " WHERE ";
                    for (int k = 0; k < p_whereKeys.Length; k ++)
                    {   
                        if (k < p_whereKeys.Length - 1)
                        {
                            __sql += p_whereKeys[k] + " = \"" + p_listData[i][p_whereKeys[k]] + "\" AND ";                  
                        }
                        else
                        {
                            __sql += p_whereKeys[k] + " = \"" + p_listData[i][p_whereKeys[k]] + "\"";
                        }
                    }
                    //Debug.Log(__sql);
                    _dbCommand.CommandText = __sql;
                    try
                    {
                        using (_dbReader = _dbCommand.ExecuteReader())
                        {
                            while(_dbReader.Read())
                            {
                                int __value = _dbReader.GetInt32(0);
                                if (__value > 0)
                                {
                                    UpdateTableData(p_tableName, p_listData[i], p_whereKeys);
                                }
                                else
                                {
                                    InsertTableData(p_tableName, p_listData[i]);
                                }
                            }
                        }   
                    }
                    catch(SqliteException sqle)
                    {
                        Debug.Log("Exception " + sqle);
                    }
                }
            }
            _dbTransaction.Commit();
        }
        _dbConnection.Close();
    }
}

Aucun commentaire:

Enregistrer un commentaire