首先,我是SQL的新手,就像今天是全新的。当前,我需要删除早于14天且百分比为0.0的数据行。如果百分比发生变化,我需要保留该行(无论它有多旧),并保持该行的上下。我正在Visual Studio 2013中研究此程序。
如果这是将数据写入服务器的方式:
public void WriteSystemOnlineHistory()
{
// sql = "create table if not exists SystemOnlineHistory(Online int, Offline int, Percent Real, dt text)";
while (true)
{
int total = GetStoreTotals("Total");
int online = GetStoreTotals("Online");
int offline = GetStoreTotals("Offline");
decimal total_dec = (decimal)total ;
decimal offline_dec = (decimal)offline;
decimal percent = 100 * (offline_dec / total_dec);
string dt = this.DateTimeSQLite(DateTime.Now);
lock (systemUpdateLock)
{
string sql = "insert into SystemOnlineHistory( Online, Offline, Percent, dt) " +
"Values (@Online, @Offline, @Percent, @dt) ";
using (SQLiteConnection c = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = c.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@Online", online);
cmd.Parameters.AddWithValue("@Offline", offline);
cmd.Parameters.AddWithValue("@Percent", percent);
cmd.Parameters.AddWithValue("@dt", dt);
c.Open();
cmd.ExecuteNonQuery();
c.Close();
}
}
Thread.Sleep(new TimeSpan(8, 0, 0));
}
如何删除旧行?这并不是说我也不能写带有0.0的行,而是只能删除具有14天(或更旧)的0.0的旧行。我希望这是有道理的。我已经对此进行了遗憾的尝试,这里是:
public void RemoveSystemOnlineHistory()
{
lock (systemUpdateLock)
{
string sql = "delete from SystemOnlineHistory( Online, Offline, Percent, dt) where percent = @percent" + "Values (@Online, @Offline, @Percent, @dt) ";
using (SQLiteConnection c = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = c.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@percent", 0.0);
c.Open();
cmd.ExecuteNonQuery();
c.Close();
}
}
}