我正在尝试找到可以与Go一起使用的MySql驱动程序,该驱动程序支持在一个调用中发出多个SQL语句。例如,我可能希望使用以下SQL创建数据库:
DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;
在PHP之类的语言中,您可以将两个SQL语句放在一个字符串中,然后一次性执行,如下所示:
$db = new PDO(...); $db->query("DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;");
我需要这个的原因是因为我有SQL转储(来自mysqldump),我想以编程方式应用于各种数据库。
mysqldump
我在Go中寻找相同的功能,但似乎所有不同的驱动程序都不支持它,坦率地说,这让我感到震惊。
Go-MySQL-Driver https://github.com/go-sql-driver/mysql 这似乎是Go中最常用的驱动程序。
package main import "database/sql" import "log" import _ "github.com/go-sql-driver/mysql" func main() { db, err := sql.Open("mysql", "user:password@(127.0.0.1:3306)/") if err != nil { log.Println(err) } sql := "DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;" _, err = db.Exec(sql) if err != nil { log.Println(err) } db.Close() }
输出:
2015/02/16 18:58:08 Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE SCHEMA IF NOT EXISTS foo' at line 1
MyMySQL https://github.com/ziutek/mymysql 这是另一个流行的驱动程序。
package main import "database/sql" import "log" import _ "github.com/ziutek/mymysql/godrv" func main() { db, err := sql.Open("mymysql", "database/user/password") if err != nil { log.Println(err) } sql := "DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;" _, err = db.Exec(sql) if err != nil { log.Println(err) } sql = "USE DATABASE foo;" _, err = db.Exec(sql) // <-- error if err != nil { log.Println(err) } db.Close() }
2015/02/16 18:58:08 packet sequence error
有谁知道与Go兼容的MySql驱动程序,可以在一个这样的字符串中处理多个语句?
https://github.com/ziutek/mymysql
可以做到。尽管您必须使用其接口与已定义的接口。go官方接口无法处理它,或有多个返回值。
package main import ( "flag" "fmt" "github.com/ziutek/mymysql/autorc" "github.com/ziutek/mymysql/mysql" _ "github.com/ziutek/mymysql/thrsafe" ) type ScanFun func(int, []mysql.Row, mysql.Result) error func RunSQL(hostport, user, pass, db, cmd string, scan ScanFun) error { conn := autorc.New("tcp", "", hostport, user, pass, db) err := conn.Reconnect() if err != nil { return err } res, err := conn.Raw.Start(cmd) if err != nil { return err } rows, err := res.GetRows() if err != nil { return err } RScount := 0 scanErr := error(nil) for { if scanErr == nil { func() { defer func() { if x := recover(); x != nil { scanErr = fmt.Errorf("%v", x) } }() scanErr = scan(RScount, rows, res) }() } if res.MoreResults() { res, err = res.NextResult() if err != nil { return err } rows, err = res.GetRows() if err != nil { return err } } else { break } RScount++ } return scanErr } func main() { host := flag.String("host", "localhost:3306", "define the host where the db is") user := flag.String("user", "root", "define the user to connect as") pass := flag.String("pass", "", "define the pass to use") db := flag.String("db", "information_schema", "what db to default to") sql := flag.String("sql", "select count(*) from columns; select * from columns limit 1;", "Query to run") flag.Parse() scan := func(rcount int, rows []mysql.Row, res mysql.Result) error { if res.StatusOnly() { return nil } for idx, row := range rows { fmt.Print(rcount, "-", idx, ") ") for i, _ := range row { fmt.Print(row.Str(i)) fmt.Print(" ") } fmt.Println("") } return nil } fmt.Println("Host - ", *host) fmt.Println("Db - ", *db) fmt.Println("User - ", *user) if err := RunSQL(*host, *user, *pass, *db, *sql, scan); err != nil { fmt.Println(err) } }