This is a creation in Article, where the information may have evolved or changed.
Use the package "CODE.GOOGLE.COM/P/ODBC"
Database connection Statements
"driver={SQL Server};SERVER="";UID="";PWD="";DATABASE=" + datanameDb, dberr = sql.Open("odbc", conn)
The DB created is actually a connection pool, and a connection to the database is created each time the database is manipulated. So if you break the network after a while, and then execute the query after the disconnection, in fact, will be automatically re-linked.
Data manipulation
1. Enquiry
Method One: Pre-compilation
stmt, errs := Db.Prepare("select * from tbl where col=?")defer stmt.Close()rows, err := stmt.Query(id)defer rows.Close()for rows.Next() { varvalueint if ers := rows.Scan(&value); ers == nil { returnvalue }}
Method Two: Query queries
rows, errs := Db.Query("select * from tbl where col='"+id+"'")defer rows.Close()for rows.Next() { varvalueint if ers := rows.Scan(&value); ers == nil { returnvalue }}
2. Increase
Method One: Pre-compilation
stmt, errs := Db.Prepare("insert into tbl (col) values(?)")defer stmt.Close()_, err := stmt.Exec(id)
Method Two:
_, err := DB.Exec("insert into tbl (col) values(='"+id+"')")
3. Modification
Method One: Pre-compilation
stmt, errs := Db.Prepare("update tbl col=? ")defer stmt.Close()_, err := stmt.Exec(id)
Method Two:
_, err := DB.Exec("update tbl col='"+id+"')")
4. Delete
Method One: Pre-compilation
stmt, errs := Db.Prepare("delete from tbl where col=? ")defer stmt.Close()_, err := stmt.Exec(id)
Method Two:
_, err := DB.Exec("delete from tbl where col='"+id+"')")
Through the above can be found in fact (add, delete, modify) The steps of the operation is the same, if the bulk operation is recommended to use prepare way, or direct use of exec more convenient, eliminating the time to create stmt.
A deeper understanding can be viewed
Use of Database/sql stmt