加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang如何用包database.sql批量sql语句

发布时间:2020-12-16 19:20:23 所属栏目:大数据 来源:网络整理
导读:如何用Golang的 database.sql包来批量sql语句? 在Java中,我会这样做: // Create a prepared statementString sql = "INSERT INTO my_table VALUES(?)";PreparedStatement pstmt = connection.prepareStatement(sql);// Insert 10 rows of datafor (int i=0
如何用Golang的 database.sql包来批量sql语句?

在Java中,我会这样做:

// Create a prepared statement
String sql = "INSERT INTO my_table VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);

// Insert 10 rows of data
for (int i=0; i<10; i++) {
    pstmt.setString(1,""+i);
    pstmt.addBatch();
}

// Execute the batch
int [] updateCounts = pstmt.executeBatch();

如何在高隆取得同样的成绩?

我不知道Java在SQL级别上的批处理,但是您可以使用事务一次实现批处理多个语句.只需确保您的数据库引擎支持它.

func (db *DB) Begin() (*Tx,error)

Begin starts a transaction. The isolation level is dependent on the driver.

func (tx *Tx) Prepare(query string) (*Stmt,error)

Prepare creates a prepared statement for use within a transaction.

func (tx *Tx) Commit() error

Commit commits the transaction.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读