public long getCntFriend()
{
long numOfFriend = 0;
// Construct SQL connection
SqliteConnectionStringBuilder connBuilder = new SqliteConnectionStringBuilder();
connBuilder.DataSource = this.dbPath;
connBuilder.Version = 3;
connBuilder.JournalMode = SQLiteJournalModeEnum.Delete;
SqliteConnection connection = new SqliteConnection(connBuilder.ToString());
SqliteCommand cmd = new SqliteCommand(connection);
try
{
connection.Open();
cmd.CommandText = "SELECT COUNT(*) FROM user, follow WHERE target = " + egoID + " AND source = id " +
" AND isProtected = 0 AND followingsCount < " + followingsCountConstraint +
" AND followersCount < " + followerCountConstraint +
" AND tweetsCount >= " + tweetCountConstraint;
numOfFriend = (long)cmd.ExecuteScalar();
cmd.Dispose ();
}
catch (SqliteException sqlError)
{
Console.WriteLine(sqlError.Message);
Environment.Exit (-1);
}
finally
{
connection.Close();
connection.Dispose();
cmd.Dispose();
connection = null;
cmd = null;
}
return numOfFriend;
}
您好。
My C#Project需要重复访问许多SQLite数据库文件。我确保遵循'try','catch'和'finally'过程并正确关闭Sqlite DB连接。
我遇到的一个问题是,在我的C#项目打开/关闭 120次的数据库文件后,SQLite会出现“无法打开数据库文件”的错误。 (请参阅随附的截图文件)
我的环境:Mac,Xamarin Studio,C#,Mono.Sqlite
我该怎么做才能解决问题?
提前致谢。
答案 0 :(得分:0)
好的,我在Xamarin的iOS项目中使用Sqlite.net库。
我创建了一个快速而又脏的sqlite数据库,名为&#39; sqlite-test.db3&#39;用一个名为&#39; users&#39;的表格并添加了3个用户和一些代码进行测试,也许它有所帮助,也许不是,但我在for循环中执行了120次而没有问题。
我在主代码中的测试循环是
int friendCount = 0;
for(int x = 0; x < 120; x++){
friendCount = getCntFriend ();
Console.WriteLine (@"friendCount after opening database " + x + " times: " + friendCount);
}
我的getCntFriend方法版本和用于获取连接的辅助函数
public SQLite.SQLiteConnection GetConnection ()
{
var sqliteFilename = "/Users/my_user_name_on_imac_machine/dev_projects/OpenSqlLiteTest/OpenSqlLiteTest/sqlite-test.db3";
var conn = new SQLite.SQLiteConnection(sqliteFilename);
// Return the database connection
return conn;
}
public int getCntFriend()
{
int numOfFriend = 0;
SQLite.SQLiteConnection sqliteConn = null;
SQLite.SQLiteCommand sqliteCommand = null;
// Construct SQL connection
try{
sqliteConn = GetConnection ();
}catch(Exception ex){
Console.WriteLine ("SqliteConnection exception: " + ex.Message);
}
if(sqliteConn != null){
using(sqliteConn){
sqliteCommand = new SQLite.SQLiteCommand (sqliteConn);
sqliteCommand.CommandText = "SELECT count(*) FROM 'users';";
try{
numOfFriend = sqliteCommand.ExecuteScalar<int>();
}catch(Exception exCmd){
Console.WriteLine ("Exception on Command execution: " + exCmd.Message);
}
}
}
return numOfFriend;
}
我在调试器中的测试输出
2015-12-01 12:44:46.087 OpenSqlLiteTest[11149:474199] friendCount after opening database 0 times: 3
2015-12-01 12:44:46.088 OpenSqlLiteTest[11149:474199] friendCount after opening database 1 times: 3
2015-12-01 12:44:46.088 OpenSqlLiteTest[11149:474199] friendCount after opening database 2 times: 3
2015-12-01 12:44:46.089 OpenSqlLiteTest[11149:474199] friendCount after opening database 3 times: 3
2015-12-01 12:44:46.089 OpenSqlLiteTest[11149:474199] friendCount after opening database 4 times: 3
2015-12-01 12:44:46.089 OpenSqlLiteTest[11149:474199] friendCount after opening database 5 times: 3
2015-12-01 12:44:46.090 OpenSqlLiteTest[11149:474199] friendCount after opening database 6 times: 3
2015-12-01 12:44:46.090 OpenSqlLiteTest[11149:474199] friendCount after opening database 7 times: 3
2015-12-01 12:44:46.090 OpenSqlLiteTest[11149:474199] friendCount after opening database 8 times: 3
2015-12-01 12:44:46.091 OpenSqlLiteTest[11149:474199] friendCount after opening database 9 times: 3
2015-12-01 12:44:46.091 OpenSqlLiteTest[11149:474199] friendCount after opening database 10 times: 3
2015-12-01 12:44:46.091 OpenSqlLiteTest[11149:474199] friendCount after opening database 11 times: 3
2015-12-01 12:44:46.092 OpenSqlLiteTest[11149:474199] friendCount after opening database 12 times: 3
2015-12-01 12:44:46.092 OpenSqlLiteTest[11149:474199] friendCount after opening database 13 times: 3
2015-12-01 12:44:46.092 OpenSqlLiteTest[11149:474199] friendCount after opening database 14 times: 3
2015-12-01 12:44:46.093 OpenSqlLiteTest[11149:474199] friendCount after opening database 15 times: 3
2015-12-01 12:44:46.093 OpenSqlLiteTest[11149:474199] friendCount after opening database 16 times: 3
2015-12-01 12:44:46.093 OpenSqlLiteTest[11149:474199] friendCount after opening database 17 times: 3
2015-12-01 12:44:46.094 OpenSqlLiteTest[11149:474199] friendCount after opening database 18 times: 3
2015-12-01 12:44:46.094 OpenSqlLiteTest[11149:474199] friendCount after opening database 19 times: 3
2015-12-01 12:44:46.095 OpenSqlLiteTest[11149:474199] friendCount after opening database 20 times: 3
2015-12-01 12:44:46.095 OpenSqlLiteTest[11149:474199] friendCount after opening database 21 times: 3
2015-12-01 12:44:46.095 OpenSqlLiteTest[11149:474199] friendCount after opening database 22 times: 3
2015-12-01 12:44:46.096 OpenSqlLiteTest[11149:474199] friendCount after opening database 23 times: 3
2015-12-01 12:44:46.096 OpenSqlLiteTest[11149:474199] friendCount after opening database 24 times: 3
2015-12-01 12:44:46.096 OpenSqlLiteTest[11149:474199] friendCount after opening database 25 times: 3
2015-12-01 12:44:46.096 OpenSqlLiteTest[11149:474199] friendCount after opening database 26 times: 3
2015-12-01 12:44:46.097 OpenSqlLiteTest[11149:474199] friendCount after opening database 27 times: 3
2015-12-01 12:44:46.097 OpenSqlLiteTest[11149:474199] friendCount after opening database 28 times: 3
2015-12-01 12:44:46.097 OpenSqlLiteTest[11149:474199] friendCount after opening database 29 times: 3
2015-12-01 12:44:46.098 OpenSqlLiteTest[11149:474199] friendCount after opening database 30 times: 3
2015-12-01 12:44:46.098 OpenSqlLiteTest[11149:474199] friendCount after opening database 31 times: 3
2015-12-01 12:44:46.098 OpenSqlLiteTest[11149:474199] friendCount after opening database 32 times: 3
2015-12-01 12:44:46.099 OpenSqlLiteTest[11149:474199] friendCount after opening database 33 times: 3
2015-12-01 12:44:46.099 OpenSqlLiteTest[11149:474199] friendCount after opening database 34 times: 3
2015-12-01 12:44:46.100 OpenSqlLiteTest[11149:474199] friendCount after opening database 35 times: 3
2015-12-01 12:44:46.100 OpenSqlLiteTest[11149:474199] friendCount after opening database 36 times: 3
2015-12-01 12:44:46.101 OpenSqlLiteTest[11149:474199] friendCount after opening database 37 times: 3
2015-12-01 12:44:46.101 OpenSqlLiteTest[11149:474199] friendCount after opening database 38 times: 3
2015-12-01 12:44:46.102 OpenSqlLiteTest[11149:474199] friendCount after opening database 39 times: 3
2015-12-01 12:44:46.102 OpenSqlLiteTest[11149:474199] friendCount after opening database 40 times: 3
2015-12-01 12:44:46.102 OpenSqlLiteTest[11149:474199] friendCount after opening database 41 times: 3
2015-12-01 12:44:46.103 OpenSqlLiteTest[11149:474199] friendCount after opening database 42 times: 3
2015-12-01 12:44:46.103 OpenSqlLiteTest[11149:474199] friendCount after opening database 43 times: 3
2015-12-01 12:44:46.103 OpenSqlLiteTest[11149:474199] friendCount after opening database 44 times: 3
2015-12-01 12:44:46.104 OpenSqlLiteTest[11149:474199] friendCount after opening database 45 times: 3
2015-12-01 12:44:46.104 OpenSqlLiteTest[11149:474199] friendCount after opening database 46 times: 3
2015-12-01 12:44:46.104 OpenSqlLiteTest[11149:474199] friendCount after opening database 47 times: 3
2015-12-01 12:44:46.104 OpenSqlLiteTest[11149:474199] friendCount after opening database 48 times: 3
2015-12-01 12:44:46.105 OpenSqlLiteTest[11149:474199] friendCount after opening database 49 times: 3
2015-12-01 12:44:46.105 OpenSqlLiteTest[11149:474199] friendCount after opening database 50 times: 3
2015-12-01 12:44:46.105 OpenSqlLiteTest[11149:474199] friendCount after opening database 51 times: 3
2015-12-01 12:44:46.106 OpenSqlLiteTest[11149:474199] friendCount after opening database 52 times: 3
2015-12-01 12:44:46.106 OpenSqlLiteTest[11149:474199] friendCount after opening database 53 times: 3
2015-12-01 12:44:46.106 OpenSqlLiteTest[11149:474199] friendCount after opening database 54 times: 3
2015-12-01 12:44:46.106 OpenSqlLiteTest[11149:474199] friendCount after opening database 55 times: 3
2015-12-01 12:44:46.107 OpenSqlLiteTest[11149:474199] friendCount after opening database 56 times: 3
2015-12-01 12:44:46.107 OpenSqlLiteTest[11149:474199] friendCount after opening database 57 times: 3
2015-12-01 12:44:46.107 OpenSqlLiteTest[11149:474199] friendCount after opening database 58 times: 3
2015-12-01 12:44:46.108 OpenSqlLiteTest[11149:474199] friendCount after opening database 59 times: 3
2015-12-01 12:44:46.108 OpenSqlLiteTest[11149:474199] friendCount after opening database 60 times: 3
2015-12-01 12:44:46.108 OpenSqlLiteTest[11149:474199] friendCount after opening database 61 times: 3
2015-12-01 12:44:46.108 OpenSqlLiteTest[11149:474199] friendCount after opening database 62 times: 3
2015-12-01 12:44:46.109 OpenSqlLiteTest[11149:474199] friendCount after opening database 63 times: 3
2015-12-01 12:44:46.109 OpenSqlLiteTest[11149:474199] friendCount after opening database 64 times: 3
2015-12-01 12:44:46.109 OpenSqlLiteTest[11149:474199] friendCount after opening database 65 times: 3
2015-12-01 12:44:46.109 OpenSqlLiteTest[11149:474199] friendCount after opening database 66 times: 3
2015-12-01 12:44:46.110 OpenSqlLiteTest[11149:474199] friendCount after opening database 67 times: 3
2015-12-01 12:44:46.110 OpenSqlLiteTest[11149:474199] friendCount after opening database 68 times: 3
2015-12-01 12:44:46.110 OpenSqlLiteTest[11149:474199] friendCount after opening database 69 times: 3
2015-12-01 12:44:46.111 OpenSqlLiteTest[11149:474199] friendCount after opening database 70 times: 3
2015-12-01 12:44:46.111 OpenSqlLiteTest[11149:474199] friendCount after opening database 71 times: 3
2015-12-01 12:44:46.111 OpenSqlLiteTest[11149:474199] friendCount after opening database 72 times: 3
2015-12-01 12:44:46.111 OpenSqlLiteTest[11149:474199] friendCount after opening database 73 times: 3
2015-12-01 12:44:46.112 OpenSqlLiteTest[11149:474199] friendCount after opening database 74 times: 3
2015-12-01 12:44:46.112 OpenSqlLiteTest[11149:474199] friendCount after opening database 75 times: 3
2015-12-01 12:44:46.112 OpenSqlLiteTest[11149:474199] friendCount after opening database 76 times: 3
2015-12-01 12:44:46.113 OpenSqlLiteTest[11149:474199] friendCount after opening database 77 times: 3
2015-12-01 12:44:46.113 OpenSqlLiteTest[11149:474199] friendCount after opening database 78 times: 3
2015-12-01 12:44:46.113 OpenSqlLiteTest[11149:474199] friendCount after opening database 79 times: 3
2015-12-01 12:44:46.113 OpenSqlLiteTest[11149:474199] friendCount after opening database 80 times: 3
2015-12-01 12:44:46.114 OpenSqlLiteTest[11149:474199] friendCount after opening database 81 times: 3
2015-12-01 12:44:46.114 OpenSqlLiteTest[11149:474199] friendCount after opening database 82 times: 3
2015-12-01 12:44:46.114 OpenSqlLiteTest[11149:474199] friendCount after opening database 83 times: 3
2015-12-01 12:44:46.114 OpenSqlLiteTest[11149:474199] friendCount after opening database 84 times: 3
2015-12-01 12:44:46.115 OpenSqlLiteTest[11149:474199] friendCount after opening database 85 times: 3
2015-12-01 12:44:46.115 OpenSqlLiteTest[11149:474199] friendCount after opening database 86 times: 3
2015-12-01 12:44:46.115 OpenSqlLiteTest[11149:474199] friendCount after opening database 87 times: 3
2015-12-01 12:44:46.116 OpenSqlLiteTest[11149:474199] friendCount after opening database 88 times: 3
2015-12-01 12:44:46.116 OpenSqlLiteTest[11149:474199] friendCount after opening database 89 times: 3
2015-12-01 12:44:46.116 OpenSqlLiteTest[11149:474199] friendCount after opening database 90 times: 3
2015-12-01 12:44:46.116 OpenSqlLiteTest[11149:474199] friendCount after opening database 91 times: 3
2015-12-01 12:44:46.117 OpenSqlLiteTest[11149:474199] friendCount after opening database 92 times: 3
2015-12-01 12:44:46.117 OpenSqlLiteTest[11149:474199] friendCount after opening database 93 times: 3
2015-12-01 12:44:46.117 OpenSqlLiteTest[11149:474199] friendCount after opening database 94 times: 3
2015-12-01 12:44:46.118 OpenSqlLiteTest[11149:474199] friendCount after opening database 95 times: 3
2015-12-01 12:44:46.118 OpenSqlLiteTest[11149:474199] friendCount after opening database 96 times: 3
2015-12-01 12:44:46.118 OpenSqlLiteTest[11149:474199] friendCount after opening database 97 times: 3
2015-12-01 12:44:46.118 OpenSqlLiteTest[11149:474199] friendCount after opening database 98 times: 3
2015-12-01 12:44:46.119 OpenSqlLiteTest[11149:474199] friendCount after opening database 99 times: 3
2015-12-01 12:44:46.119 OpenSqlLiteTest[11149:474199] friendCount after opening database 100 times: 3
2015-12-01 12:44:46.119 OpenSqlLiteTest[11149:474199] friendCount after opening database 101 times: 3
2015-12-01 12:44:46.120 OpenSqlLiteTest[11149:474199] friendCount after opening database 102 times: 3
2015-12-01 12:44:46.120 OpenSqlLiteTest[11149:474199] friendCount after opening database 103 times: 3
2015-12-01 12:44:46.120 OpenSqlLiteTest[11149:474199] friendCount after opening database 104 times: 3
2015-12-01 12:44:46.120 OpenSqlLiteTest[11149:474199] friendCount after opening database 105 times: 3
2015-12-01 12:44:46.121 OpenSqlLiteTest[11149:474199] friendCount after opening database 106 times: 3
2015-12-01 12:44:46.121 OpenSqlLiteTest[11149:474199] friendCount after opening database 107 times: 3
2015-12-01 12:44:46.121 OpenSqlLiteTest[11149:474199] friendCount after opening database 108 times: 3
2015-12-01 12:44:46.122 OpenSqlLiteTest[11149:474199] friendCount after opening database 109 times: 3
2015-12-01 12:44:46.122 OpenSqlLiteTest[11149:474199] friendCount after opening database 110 times: 3
2015-12-01 12:44:46.122 OpenSqlLiteTest[11149:474199] friendCount after opening database 111 times: 3
2015-12-01 12:44:46.122 OpenSqlLiteTest[11149:474199] friendCount after opening database 112 times: 3
2015-12-01 12:44:46.123 OpenSqlLiteTest[11149:474199] friendCount after opening database 113 times: 3
2015-12-01 12:44:46.123 OpenSqlLiteTest[11149:474199] friendCount after opening database 114 times: 3
2015-12-01 12:44:46.123 OpenSqlLiteTest[11149:474199] friendCount after opening database 115 times: 3
2015-12-01 12:44:46.124 OpenSqlLiteTest[11149:474199] friendCount after opening database 116 times: 3
2015-12-01 12:44:46.124 OpenSqlLiteTest[11149:474199] friendCount after opening database 117 times: 3
2015-12-01 12:44:46.124 OpenSqlLiteTest[11149:474199] friendCount after opening database 118 times: 3
2015-12-01 12:44:46.124 OpenSqlLiteTest[11149:474199] friendCount after opening database 119 times: 3