SqliteConnection,同一事务上的多次提交不起作用

问题描述

我想从流中插入,就像在下面的 Microsoft 文档中一样,但具有间歇性的提交。 为什么我需要添加带问号的行才能让它工作?是否有更好的替代方法来编写此代码?不知道为什么我不能在同一个事务上多次提交。

https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/bulk-insert

private static void CreateDatabase(IEnumerable<Ticker> ticks)
{
    var currentDate = DateTime.Now.ToString("yyyyMMddHHmmss");

    using (var connection = new sqliteConnection(@"Data Source=D:\ticks_" + currentDate + ".db"))
    {

        connection.open();


        var createCommandStr = "CREATE TABLE ticker (id INTEGER NOT NULL,exchange_id INTEGER NOT NULL,time INTEGER NOT NULL," +
                    "name STRING NOT NULL,price NUMERIC NOT NULL,size NUMERIC NOT NULL," +
                    "bid_price NUMERIC NOT NULL,bid_size NUMERIC NOT NULL,ask_price NUMERIC NOT NULL," +
                    "ask_size NUMERIC NOT NULL,PRIMARY KEY(id))";

        using (var createCommand = new sqliteCommand(createCommandStr,connection))
        {
            createCommand.ExecuteNonQuery();
        }

        var insertCommandStr = "INSERT INTO ticker (id,exchange_id,time,name,price,size,bid_price,bid_size,ask_price,ask_size) " +
                "VALUES ($id,$exchange_id,$time,$name,$price,$size,$bid_price,$bid_size,$ask_price,$ask_size)";

        ////deferred: true
        //using (var transaction = connection.BeginTransaction(deferred: true))
        //{
        using (var insertCommand = new sqliteCommand(insertCommandStr,connection,connection.BeginTransaction()))
        {
            #region shitRegion
            var id = insertCommand.CreateParameter();
            id.ParameterName = "$id";
            insertCommand.Parameters.Add(id);

            var exchangeId = insertCommand.CreateParameter();
            exchangeId.ParameterName = "$exchange_id";
            insertCommand.Parameters.Add(exchangeId);

            var time = insertCommand.CreateParameter();
            time.ParameterName = "$time";
            insertCommand.Parameters.Add(time);

            var name = insertCommand.CreateParameter();
            name.ParameterName = "$name";
            insertCommand.Parameters.Add(name);

            var price = insertCommand.CreateParameter();
            price.ParameterName = "$price";
            insertCommand.Parameters.Add(price);

            var size = insertCommand.CreateParameter();
            size.ParameterName = "$size";
            insertCommand.Parameters.Add(size);

            var bid_price = insertCommand.CreateParameter();
            bid_price.ParameterName = "$bid_price";
            insertCommand.Parameters.Add(bid_price);

            var bid_size = insertCommand.CreateParameter();
            bid_size.ParameterName = "$bid_size";
            insertCommand.Parameters.Add(bid_size);

            var ask_price = insertCommand.CreateParameter();
            ask_price.ParameterName = "$ask_price";
            insertCommand.Parameters.Add(ask_price);

            var ask_size = insertCommand.CreateParameter();
            ask_size.ParameterName = "$ask_size";
            insertCommand.Parameters.Add(ask_size);

            #endregion

            insertCommand.Prepare();

            long count = 1L;

            foreach (var ticker in ticks)
            {
                id.Value = count;
                exchangeId.Value = ticker.ExchangeId;
                time.Value = ticker.Time;
                name.Value = ticker.Name;
                price.Value = ticker.Price;
                size.Value = ticker.Size;
                bid_price.Value = ticker.BidPrice;
                bid_size.Value = ticker.BidSize;
                ask_price.Value = ticker.AskPrice;
                ask_size.Value = ticker.AskSize;

                insertCommand.ExecuteNonQuery();

                if (count % 150000L == 0L)
                {
                    //transaction.Commit();
                    insertCommand.Transaction.Commit();
                    insertCommand.Connection = connection;//?
                    insertCommand.Transaction = connection.BeginTransaction();//?
                }

                count++;
            }
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...