func main() { dbh, err := mysql.Connect("tcp", "", "127.0.0.1:3306", "test", "test", "") // With password, user test, with database. if err != nil { fmt.Printf("%s\n", err) os.Exit(1) } dbh.Use("test") dbh.Query("SET NAMES utf8") sth := new(mysql.MySQLStatement) sth, err = dbh.Prepare("SELECT * FROM test WHERE id = ? OR id = ? OR id = ?") if err != nil { fmt.Printf("%s\n", err) os.Exit(1) } res, err := sth.Execute(1, 2, 3) if err != nil { fmt.Printf("%s\n", err) os.Exit(1) } rows := res.FetchAllRowMap() fmt.Printf("Rows: %d\n", len(rows)) fmt.Printf("%#v\n", rows) if err != nil { fmt.Printf("%s\n", err) os.Exit(1) } dbh.Quit() }
func main() { flag.Parse() if *dbhost == "" || *dbname == "" || *dbuser == "" { flag.Usage() os.Exit(1) } var err os.Error var res *mysql.MySQLResult var row map[string]interface{} var key string var value interface{} var stmt *mysql.MySQLStatement // Create new instance db := mysql.New() // Connect to database if err = db.Connect(*dbhost, *dbuser, *dbpass, *dbname); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } // Ensure connection is closed on exit. defer db.Close() // Use UTF8 if _, err = db.Query("SET NAMES utf8"); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } // Initialise statement if stmt, err = db.InitStmt(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } // Prepare statement if err = stmt.Prepare("SELECT * FROM test1 WHERE id > ? AND id < ?"); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } // Bind params stmt.BindParams(1, 5) // Execute statement if res, err = stmt.Execute(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } defer stmt.Close() // Display results for { if row = res.FetchMap(); row == nil { break } for key, value = range row { fmt.Printf("%s:%v\n", key, value) } } }