What Is the Equivalent of 'Go' in MySQL

What is the equivalent of 'go' in MySQL?

Semicolon at the end of the statement.

INSERT INTO myTable (ID) values (5);

Is GO Supported in MySQL?

GO is not a SQL keyword, not even a Transact-SQL keyword.

It is a batch separator for Microsoft SQL Server tools.

See GO (Tansact-SQL) on MSDN:

Signals the end of a batch of Transact-SQL statements to the SQL Server utilities.

And:

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

It is not supported by any other databases.

how to set mysql max_execution_time property use go

Execute SET max_execution_time=500 as a SQL statement. This limits the following SQL statements on the same connection to 0.5 seconds.

It is also possible as a SQL comment hint like SELECT /*+ MAX_EXECUTION_TIME(1000) */ field1, field2 FROM tbl ... like
this answer.
If you show details about your slow query in a new question performance improvements may be possible.

golang slice in mysql query with where in clause

You can do something like this:

args := make([]interface{}, len(asID))
for i, id := range asID {
args[i] = id
}
stmt := `SELECT * from table2 where id in (?` + strings.Repeat(",?", len(args)-1) + `)`
anotherRow, err := db.Query(stmt, args...)

Just note you will want to put in a guard if asID can ever have len == 0.

If you have any other arguments to pass in, you'll have to add them to the args slice.

Also to note, you should explicitly name the columns you want so you can guarantee you are scanning in the correct columns to the correct fields.

Datetime error while inserting data in mysql through golang

For SQL use the format "2006-01-02 15:04:05" for DATETIME. But consider using some parameterized database access or ORM for safety.

dt := time.Now().Format("2006-01-02 15:04:05")

How to create a MySQL database (table) in Go and perform CRUD operations

You can use the methods Exec, Query, and QueryRow that are provided by *sql.DB to send your SQL commands to the connected database.

func main() {
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
if err != nil {
panic(err)
} else if err = db.Ping(); err != nil {
panic(err)
}
defer db.Close()

_, err := db.Exec("CREATE TABLE IF NOT EXISTS mytable (id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, some_text TEXT NOT NULL)")
if err != nil {
panic(err)
}

// Create
res, err := db.Exec("INSERT INTO mytable (some_text) VALUES (?)", "hello world")
if err != nil {
panic(err)
}

// get the id of the newly inserted record
id, err := res.LastInsertId()
if err != nil {
panic(err)
}

// Read
var someText string
row := db.QueryRow("SELECT some_text FROM mytable WHERE id = ? LIMIT 1", id)
if err := row.Scan(&someText); err != nil {
panic(err)
}
fmt.Println(someText)

// Update
_, err = db.Exec("UPDATE mytable SET some_text = ? WHERE id = ?", "Hello, 世界", id)
if err != nil {
panic(err)
}

// Delete
_, err = db.Exec("DELETE FROM mytable WHERE id = ?", id)
if err != nil {
panic(err)
}
}

Does a Go Mysql driver exist that supports multiple statements within a single string?

https://github.com/ziutek/mymysql

Can do it. Although you have to use its interface vs the go defined one. The go official interface doesn't handle it, or multiple return values.

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)
}
}


Related Topics



Leave a reply



Submit