Commit 2c1358f2 authored by 潘栩锋's avatar 潘栩锋 🚴

优化 SQLiteHelper 只有当有数据被删除时,才会数据库压缩

parent 2542c75e
...@@ -257,12 +257,14 @@ namespace SQLite ...@@ -257,12 +257,14 @@ namespace SQLite
sqls.RemoveAll(sql => sql == null); sqls.RemoveAll(sql => sql == null);
if (sqls.Count == 0) if (sqls.Count == 0)
return; return;
int cnt = 0;
if (sqliteHelper.QueryTran(sqls)) { foreach (var sql in sqls) {
cnt += sqliteHelper.ExecuteNonQuery(sql);
}
if (cnt > 0) {
//数据库压缩 //数据库压缩
sqliteHelper.ExecuteNonQuery("VACUUM"); sqliteHelper.ExecuteNonQuery("VACUUM");
} }
} }
/// <summary> /// <summary>
......
...@@ -446,18 +446,21 @@ namespace SQLite ...@@ -446,18 +446,21 @@ namespace SQLite
/// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param> /// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="Exception"></exception> /// <exception cref="Exception"></exception>
public void ExecuteNonQuery(string sql) public int ExecuteNonQuery(string sql)
{ {
int ret = 0;
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{ {
using (SQLiteCommand command = new SQLiteCommand(connection)) using (SQLiteCommand command = new SQLiteCommand(connection))
{ {
connection.Open(); connection.Open();
command.CommandText = sql; command.CommandText = sql;
command.ExecuteNonQuery(); ret = command.ExecuteNonQuery();
connection.Close(); connection.Close();
} }
} }
return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment