- Home /
SQLite Transaction Update Problem on Android
This is very specific. I've been using SQLite for an Android game, and everything was working well until I start to use transactions. For some reason, when I try to update a table, using a transaction, the application throws an exception: "sql logic error or missing database". But this happens only on an android device, on the PC it works well. I couldn't find an answer for this. I did a workaround by deleting the rows to be updated and then re-inserting with the new values, but I don't feel confortable doing this because it's not the best practice. Just to be sure, I did a simple class to test this problem:
using Mono.Data.SqliteClient;
using System;
using System.Data;
namespace MyGame
{
internal class TestDAO
{
private DataAccess m_dataAccess;
public TestDAO()
{
m_dataAccess = DataAccess.Instance;
}
public void Insert()
{
IDbCommand command = m_dataAccess.Connection.CreateCommand();
command.CommandText = "INSERT INTO test " +
"VALUES(@id, 0);";
IDbTransaction transaction = null;
try
{
m_dataAccess.Connection.Open();
transaction = m_dataAccess.Connection.BeginTransaction();
command.Transaction = transaction;
for ( int i = 0; i < 1000; i++ )
{
command.Parameters.Clear();
command.Parameters.Add(new SqliteParameter("@id", i));
command.ExecuteNonQuery();
}
transaction.Commit();
}
catch ( Exception e )
{
transaction.Rollback();
throw e;
}
finally
{
m_dataAccess.Connection.Close();
}
}
public void Update()
{
IDbCommand command = m_dataAccess.Connection.CreateCommand();
command.CommandText = "UPDATE test " +
"SET number = @number " +
"WHERE id = @id;";
IDbTransaction transaction = null;
try
{
m_dataAccess.Connection.Open();
transaction = m_dataAccess.Connection.BeginTransaction();
command.Transaction = transaction;
for ( int i = 0; i < 1000; i++ )
{
command.Parameters.Clear();
command.Parameters.Add(new SqliteParameter("@id", i));
command.Parameters.Add(new SqliteParameter("@number", i + 1));
command.ExecuteNonQuery();
}
transaction.Commit();
}
catch ( Exception e )
{
transaction.Rollback();
throw e;
}
finally
{
m_dataAccess.Connection.Close();
}
}
}
}
I've created a table "test" with the fields: "id" and "number", both integer, where id is the primary key. So, I run Insert() and then Update() in my game, and I got the same error on Android, but it works fine on PC. I don't know what is the problem, but seems to be something about permissions or compatibility. Does anyone know a good solution for this? (I mean: using UPDATE instead of DELETE / INSERT)
P.S.: DataAccess is just a singleton class where I encapsulate my database connection, the "Connection" property returns an instance of "IDbConnection", used on the entire application. And, for now, I don't use multithreaded database connections.
Your answer
Follow this Question
Related Questions
SqlLite , android and unity 1 Answer
Unity sqlite to android: no such table 0 Answers
Sqlite3.dll not found on specific Android devices 2 Answers