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

我正在开发一个从Web服务获取一些数据的应用程序.该程序是用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

解决方法:

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

我希望这对其他人也很有用,因为我找不到遇到类似问题的人.

DebboR

上一篇:C#使用Xamarin开发移动应用 ---- 系列文章


下一篇:Xamarin.FormsShell基础教程(7)Shell项目关于页面的介绍