如何用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.
Commit commits the transaction.