【Error content】
Sorry, can not exec into mysql: Error 1064 (42000): 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 ...
【reason】
In most cases, only a single SQL statement can be executed. If your SQL script contains multiple statements, you need to split them and execute them one by one. This usually involves placing the entire SQL script contents by statement separators (usually semicolons
;
) split into separate statements and execute them one by one.
If you need to execute two MySQL statements, you must execute () separately. Here we use the split function to split the statement.
as follows:
package main import ( "database/sql" "fmt" "log" "strings" _ "/go-sql-driver/mysql" ) func main() { // Database connection settings dsn := "username:password@tcp(host:port)/dbname" db, err := ("mysql", dsn) if err != nil { (err) } defer () // Test database connection if err = (); err != nil { (err) } // SQL commands sqlCommands := ` DROP TABLE IF EXISTS combinedData; CREATE TABLE combinedData( ProducerName VARCHAR(255) ); ` // Split commands by semicolon commands := (sqlCommands, ";") for _, cmd := range commands { // Remove whitespace characters at the beginning and end of the string cmd = (cmd) if cmd != "" { // Execute SQL commands _, err = (cmd) if err != nil { ("An error occurred when executing SQL: ", err) } } } ("All SQL command execution is completed") }
This code will split the SQL commands into separate statements according to the semicolon and execute them one by one. Note that this approach works for simple SQL scripts, but may not handle more complex situations such as SQL strings or stored procedures that contain semicolons. For more complex situations, you may need a more granular parse method or execute complex scripts directly in the database management tool.
Summarize
This is the article about the solution to the error in GoLang () in GoLang. For more information about the error in GoLang () please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!