在Xamarin.Android中执行大量SQlite查询失败

时间:2013-08-05 10:39:18

标签: c# android database sqlite xamarin

我正在开发一个从网络服务获取一些数据的应用。该程序使用Xamarin Studio以C#编写。

该应用程序具有存储在文件中的本地SQLite数据库。我创建了一个单独的Database类来简化数据库的内部工作(例如打开和关闭连接等)。以下是此类的相关摘要:

public static SqliteConnection connect(){
            string documentPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
            try {
                return new SqliteConnection (string.Format ("Data Source={0};Version=3;", documentPath + "Database.db"));
            } catch {
                Console.WriteLine ("New DB created!");
                SqliteConnection.CreateFile (documentPath + "Database.db");
                return new SqliteConnection (string.Format ("Data Source={0};Version=3;", documentPath + "Database.db"));
            }
        }

public static void doQuery(string query){
            //Console.WriteLine ("Performing no-return query: " + query);
            using(var conn = connect()){
                conn.Open ();
                using(var cmd = conn.CreateCommand ()){
                    var trans = conn.BeginTransaction ();
                    cmd.CommandText = query;
                    cmd.CommandType = CommandType.Text;
                    cmd.Transaction = trans;
                    cmd.ExecuteNonQuery ();
                    trans.Commit ();
                }
            }
        }

public static DataTable getQueryResult(string query){
    //Console.WriteLine ("Performing return query: " + query);
    using (var conn = connect()) {
        conn.Open ();
        using (var cmd = conn.CreateCommand ()) {
            cmd.CommandText = query;
            cmd.CommandType = CommandType.Text;
            SqliteDataReader rdr = cmd.ExecuteReader ();
            DataTable dt = new DataTable ();
            dt.Load (rdr);
            return dt;
        }
    }
   }

表演后约1900个查询,我收到此错误:

[mono] Unhandled Exception:
[mono] Mono.Data.Sqlite.SqliteException: Unable to open the database file
[mono] unable to open database file
[mono]   at Mono.Data.Sqlite.SQLite3.Reset (Mono.Data.Sqlite.SqliteStatement stmt) [0x00000] in <filename unknown>:0 
[mono]   at Mono.Data.Sqlite.SQLite3.Step (Mono.Data.Sqlite.SqliteStatement stmt) [0x00000] in <filename unknown>:0 
[mono]   at Mono.Data.Sqlite.SqliteDataReader.NextResult () [0x00000] in <filename unknown>:0 
[mono]   at Mono.Data.Sqlite.SqliteDataReader..ctor (Mono.Data.Sqlite.SqliteCommand cmd, CommandBehavior behave) [0x00000] in <filename unknown>:0 
[mono]   at (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteDataReader:.ctor (Mono.Data.Sqlite.SqliteCommand,System.Data.CommandBehavior)
[mono]   at Mono.Data.Sqlite.SqliteCommand.ExecuteReader (CommandBehavior behavior) [0x00000] in <filename unknown>:0 
[mono]   at Mono.Data.Sqlite.SqliteCommand.ExecuteNonQuery () [0x00000] in <filename unknown>:0 
[mono]   at Mono.Data.Sqlite.SqliteTransaction..ctor (Mono.Data.Sqlite.SqliteConnection connection, Boolean deferred

在“var trans = conn.BeginTransaction();”中抛出此错误doQuery方法中的一行。

我认为这与不处理对象有关,但由于我使用using()作为连接和命令,我不明白为什么......

我希望有人可以帮助我!

DebboR

2 个答案:

答案 0 :(得分:3)

我解决了我的问题!在扩展Application的后台类中,我永久保持连接打开。现在插入一堆元素时我没有遇到任何错误。

我希望这对其他人也有好处,因为我找不到有类似问题的人。

DebboR

答案 1 :(得分:0)

想知道每次打开它时是否尝试过明确地关闭连接。但是,只要没有并发问题,使用锁定的单例模式会更好。我对Xamarin很新,但可能没有连接缓存。