将对象"converting"到 SQLite 记录的最佳方法是什么?



我使用的是Livescore API,它返回对象列表。

每个对象与大约30个字段匹配(例如:AwayGoalDetails、HomeTeamRedCardDetails、HomeGoals等),字段为string、string[]或int类型。

现在我正在做一些让人觉得很愚蠢的事情。我的表创建查询如下所示:

string sql = "create table BundesligaFixtures (AwayGoalDetails text, AwayGoals int, AwayLineupDefense text, AwayLineupForward text, AwayLineupGoalkeeper text, AwayLineupMidfield text, AwayLineupSubstitutes text, AwaySubDetails text, AwayTeam text, AwayTeamFormation text, AwayTeamRedCardDetails text, AwayTeamYellowCardDetails text, AwayTeam_Id int, Date numeric, FixtureMatch_Id int, HomeGoalDetails text, HomeGoals int, HomeLineupDefense text, HomeLineupForward text, HomeLineupGoalkeeper text, HomeLineupMidfield text, HomeLineupSubstitutes text, HomeSubDetails text, HomeTeam text, HomeTeamFormation text, HomeTeamRedCardDetails text, HomeTeamYellowCardDetails text, HomeTeam_Id int, Location text, Round int, Spectators int, Time text)";

然后,我用foreach循环迭代List(我将字符串[]转换为csv,这样我就可以将它们存储为TEXT),并使用以下怪物:

foreach (var match in matches)
        {
            sql = String.Format("insert into BundesligaFixtures values ('{0}', {1}, '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', {12}, {13}, {14}, '{15}', {16}, '{17}', '{18}', '{19}', '{20}', '{21}', '{22}', '{23}', '{24}', '{25}', '{26}', {27}, '{28}', {29}, {30}, '{31}')", StringModifier.ToString(match.AwayGoalDetails), match.AwayGoals, StringModifier.ToString(match.AwayLineupDefense), StringModifier.ToString(match.AwayLineupForward), match.AwayLineupGoalkeeper, StringModifier.ToString(match.AwayLineupMidfield), StringModifier.ToString(match.AwayLineupSubstitutes), StringModifier.ToString(match.AwaySubDetails), match.AwayTeam, match.AwayTeamFormation, StringModifier.ToString(match.AwayTeamRedCardDetails), StringModifier.ToString(match.AwayTeamYellowCardDetails), match.AwayTeam_Id, match.Date, match.FixtureMatch_Id, StringModifier.ToString(match.HomeGoalDetails), match.HomeGoals, StringModifier.ToString(match.HomeLineupDefense), StringModifier.ToString(match.HomeLineupForward), match.HomeLineupGoalkeeper, StringModifier.ToString(match.HomeLineupMidfield), StringModifier.ToString(match.HomeLineupSubstitutes), StringModifier.ToString(match.HomeSubDetails), match.HomeTeam, match.HomeTeamFormation, StringModifier.ToString(match.HomeTeamRedCardDetails), StringModifier.ToString(match.HomeTeamYellowCardDetails), match.HomeTeam_Id, match.Location, match.Round, match.Spectators, match.Time);
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

有更好的方法吗?

您可以使用ORM库(如实体框架或NHibernate)来简化向数据库写入/从数据库读取数据的过程。例如,使用Sqlite的实体框架。

相关内容

最新更新