-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
session variables can not be used? #208
Comments
There are two things wrong with your provided example code:
I'd recommend you to read the database/sql tutorial at http://go-database-sql.org/ |
It may create a new session, it may use the same one. We don't known, it fully depends on the pooling by You can
My recipe for the last one: SET @row := 0;
SELECT @row := @row + 1, * FROM mytable; becomes SELECT @row := @row + 1, t.* FROM (SELECT @row := 0) cnt JOIN mytable t; |
But not all statements can be write into a single statement. db, _ := sql.Open("mysql", connect_string)
trx, _:= db.Begin()
db.Query("set binlog_format=row")
rows, _ := db.Query("select @@binlog_format")
for rows.Next() {
var v string
rows.Scan(&v)
fmt.Println(v)
}
rows.Close()
trx.Commit()
db.Close() It prints "mixed", which is the global value. For comman user, it looks like a bug. to arnehormann, what do you mean by " use the driver without database/sql"? Without importing this package, build faild |
@tbdingqi No, not all can be written into a single statement. But some of the It looks like a bug to a common user: The import: You can use import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
) and speak to the driver directly without |
In your sample code that printed the binlog format, you wrote trx, := db.Begin() You can't access the db variable there; you need to do this instead: trx, err := db.Begin() Note that I am querying with trx, not db. We may need to give clearer examples in the tutorial on how to do |
@xaprb good catch, I missed the Tx misuse. |
This method will not work for variables like Is there a chance to setup this variable for |
how to repeat
db.Query("set @A=1")
db.Query("select @A")
return null
That seems to because when the "select @A" executed, the drive create a new session on mysql server. This leads to session-variables can not be used.
Does the mechanism involved by database/sql or github.com/go-sql-driver/mysql ?
The text was updated successfully, but these errors were encountered: