英文:
Using sql database driver
问题
请问一个终端用户(开发或生产)能否推荐一个最好使用“database/sql”包的Sql驱动程序包。我对Postgres、ODBC、MySql(可能还有其他一些,但不是Sqlite)感兴趣,最好能在Windows和/或Linux上使用(最好两者都可以)。对我来说,它可能需要最近更新/维护,并且必须与最新的Go版本配合使用。效率(吞吐量)显然很重要。欢迎提供任何相关评论。
英文:
Could an end-user (development or production) please recommend an Sql driver package that preferably uses the "database/sql" package. I'm interested in Postgres, ODBC, MySql (and possibly something else but for high-volume - ie. not Sqlite) that can preferably be used on Windows and/or Linux (preferably both). To be of interest to me it likely needs to have recently been updated /maintained and must work with latest Go release. Efficiency (throughput) is obviously important. Any relevant comments are welcome.
答案1
得分: 9
GO-SQL-Driver(MySQL):
> https://github.com/Go-SQL-Driver/MySQL
示例表格:
CREATE TABLE `userinfo` (
`uid` INT(10) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(64) NULL DEFAULT NULL,
`departname` VARCHAR(64) NULL DEFAULT NULL,
`created` DATE NULL DEFAULT NULL,
PRIMARY KEY (`uid`)
)
CREATE TABLE `userdetail` (
`uid` INT(10) NOT NULL DEFAULT '0',
`intro` TEXT NULL,
`profile` TEXT NULL,
PRIMARY KEY (`uid`)
)
示例代码:
package main
import (
_ "github.com/Go-SQL-Driver/MySQL"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql", "reckhou:reckhou@/test?charset=utf8")
checkErr(err)
//插入
stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
checkErr(err)
res, err := stmt.Exec("reckhou", "IT", "2010-10-02")
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
fmt.Println(id)
// 更新
stmt, err = db.Prepare("update userinfo set username=? where uid=?")
checkErr(err)
res, err = stmt.Exec("update", id)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
//查询
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(&uid, &username, &department, &created)
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}
//删除
stmt, err = db.Prepare("delete from userinfo where uid=?")
checkErr(err)
res, err = stmt.Exec(id)
checkErr(err)
affect, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
PostgreSQL:
> https://github.com/bmizerany/pq
示例表格:
CREATE TABLE userinfo
(
uid serial NOT NULL,
username character varying(100) NOT NULL,
departname character varying(500) NOT NULL,
Created date,
CONSTRAINT userinfo_pkey PRIMARY KEY (uid)
)
WITH (OIDS=FALSE);
CREATE TABLE userdeatail
(
uid integer,
intro character varying(100),
profile character varying(100)
)
WITH(OIDS=FALSE);
示例代码:
import (
"database/sql"
"fmt"
_ "github.com/bmizerany/pq"
)
func main() {
db, err := sql.Open("postgres", "user=reckhou password=reckhou dbname=test sslmode=disable")
checkErr(err)
//插入
stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES($1,$2,$3) RETURNING uid")
checkErr(err)
res, err := stmt.Exec("reckhou", "IT", "2010-10-01")
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
fmt.Println(id)
// 更新
stmt, err = db.Prepare("update userinfo set username=$1 where uid=$2")
checkErr(err)
res, err = stmt.Exec("update", 1)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
// 查询
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(&uid, &username, &department, &created)
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}
// 删除
stmt, err = db.Prepare("delete from userinfo where uid=$1")
checkErr(err)
res, err = stmt.Exec(1)
checkErr(err)
affect, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
英文:
GO-SQL-Driver(MySQL):
> https://github.com/Go-SQL-Driver/MySQL
Sample table:
CREATE TABLE `userinfo` (
`uid` INT(10) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(64) NULL DEFAULT NULL,
`departname` VARCHAR(64) NULL DEFAULT NULL,
`created` DATE NULL DEFAULT NULL,
PRIMARY KEY (`uid`)
)
CREATE TABLE `userdetail` (
`uid` INT(10) NOT NULL DEFAULT '0',
`intro` TEXT NULL,
`profile` TEXT NULL,
PRIMARY KEY (`uid`)
)
Sample code:
package main
import (
_ "github.com/Go-SQL-Driver/MySQL"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql", "reckhou:reckhou@/test?charset=utf8")
checkErr(err)
//Insert
stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
checkErr(err)
res, err := stmt.Exec("reckhou", "IT", "2010-10-02")
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
fmt.Println(id)
// Update
stmt, err = db.Prepare("update userinfo set username=? where uid=?")
checkErr(err)
res, err = stmt.Exec("update", id)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
//Query
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(&uid, &username, &department, &created)
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}
//Delete
stmt, err = db.Prepare("delete from userinfo where uid=?")
checkErr(err)
res, err = stmt.Exec(id)
checkErr(err)
affect, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
PostgreSQL:
> https://github.com/bmizerany/pq
Sample table:
CREATE TABLE userinfo
(
uid serial NOT NULL,
username character varying(100) NOT NULL,
departname character varying(500) NOT NULL,
Created date,
CONSTRAINT userinfo_pkey PRIMARY KEY (uid)
)
WITH (OIDS=FALSE);
CREATE TABLE userdeatail
(
uid integer,
intro character varying(100),
profile character varying(100)
)
WITH(OIDS=FALSE);
Sample code:
import (
"database/sql"
"fmt"
_ "github.com/bmizerany/pq"
)
func main() {
db, err := sql.Open("postgres", "user=reckhou password=reckhou dbname=test sslmode=disable")
checkErr(err)
//Insert
stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES($1,$2,$3) RETURNING uid")
checkErr(err)
res, err := stmt.Exec("reckhou", "IT", "2010-10-01")
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
fmt.Println(id)
// Update
stmt, err = db.Prepare("update userinfo set username=$1 where uid=$2")
checkErr(err)
res, err = stmt.Exec("update", 1)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
// Query
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(&uid, &username, &department, &created)
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}
// Delete
stmt, err = db.Prepare("delete from userinfo where uid=$1")
checkErr(err)
res, err = stmt.Exec(1)
checkErr(err)
affect, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论