我有一個按時間順序按升序時間戳排序的製表符分隔文件。每行有7列。每列是一個參數。Sqlite如何保留INSERT的順序
我有一個sqlite數據庫,我從選項卡文件中插入行作爲批量INSERT命令中的參數。例如:
public void Create(IEnumerable<Game> items)
{
var sbFields = new StringBuilder();
sbFields.Append("ID,");
sbFields.Append("LEAGUE,");
sbFields.Append("VISITORTEAM,");
sbFields.Append("HOMETEAM,");
sbFields.Append("DATETIME,");
sbFields.Append("VISITORSCORE,");
sbFields.Append("HOMESCORE");
int numAppended = 7;
var sbParams = new StringBuilder();
for (int i = 1; i <= numAppended; i++)
{
sbParams.Append("@param");
sbParams.Append(i);
if (i < numAppended)
{
sbParams.Append(", ");
}
}
using (var command = new SQLiteCommand(Db))
{
command.CommandText = "INSERT OR IGNORE INTO GAMES (" + sbFields + ") VALUES(" + sbParams + ")";
command.CommandType = CommandType.Text;
using (var transaction = Db.BeginTransaction())
{
foreach (var game in items)
{
command.Parameters.Add(new SQLiteParameter("@param1", game.Id));
command.Parameters.Add(new SQLiteParameter("@param2", game.League));
command.Parameters.Add(new SQLiteParameter("@param3", game.VisitorTeam));
command.Parameters.Add(new SQLiteParameter("@param4", game.HomeTeam));
command.Parameters.Add(new SQLiteParameter("@param5", game.Date.Ticks));
command.Parameters.Add(new SQLiteParameter("@param6", game.VisitorScore));
command.Parameters.Add(new SQLiteParameter("@param7", game.HomeScore));
command.ExecuteNonQuery();
}
transaction.Commit();
}
}
}
我希望,當我檢索,這將是在相同的順序,我做的插入數據庫的遊戲列表(按時間順序),而不是:其實它是在幾乎是隨機的順序。因此,我必須在從數據庫中讀取遊戲列表後對遊戲列表進行排序。而且這個操作非常緩慢。我不想排序。我想按照與INSERT相同的順序讀取它。
這可能嗎?注意:我做的不是想要添加一個新的參數(列)來表示順序。這似乎是一個人爲的解決方案,我乞求未來的問題。
這裏是我從數據庫中讀取數據:
public IEnumerable<Game> Read()
{
List<Game> enumerable = new List<Game>();
using (var command = new SQLiteCommand(Db))
{
command.CommandText = "SELECT * FROM GAMES";
command.CommandType = CommandType.Text;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Game game = new Game();
game.Id = reader.GetInt32(0);
game.League = reader.GetString(1);
game.VisitorTeam = reader.GetString(2);
game.HomeTeam = reader.GetString(3);
game.Date = new DateTime(reader.GetInt64(4));
//game.Date = DateTime.FromFileTimeUtc(Convert.ToInt64(reader.GetInt64(4)));
game.VisitorScore = Convert.ToInt32(reader.GetInt32(5));
game.HomeScore = Convert.ToInt32(reader.GetInt32(6));
enumerable.Add(game);
}
}
}
return enumerable;
}
下面是表CREATE:
public void CreateTable()
{
string sql =
@"CREATE TABLE IF NOT EXISTS GAMES(
ID INTEGER PRIMARY KEY,
LEAGUE TEXT NOT NULL,
VISITORTEAM TEXT NOT NULL,
HOMETEAM TEXT NOT NULL,
DATETIME INTEGER NOT NULL,
VISITORSCORE INTEGER NOT NULL,
HOMESCORE INTEGER NOT NULL
)";
// if the database table already exists, this will throw an exception.
using (var command = new SQLiteCommand(sql, Db))
{
command.ExecuteNonQuery();
}
}
如何我目前的排序(太慢)
public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey>
(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
bool descending)
{
return descending ? source.OrderByDescending(keySelector)
: source.OrderBy(keySelector);
}
使用:
games = games.OrderByWithDirection(x => x.Date.Ticks, false).ToList();
SELECT * FROM GAMES ORDER BY ID –
該ID是一個散列並且不保證次序 – sapbucket
顯示錶定義。 –