Created
June 3, 2011 21:32
-
-
Save dtjm/1007214 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/Philio/GoMySQL" | |
"os" | |
) | |
type Thing struct { | |
Text1, Text2 string | |
Num1 int | |
} | |
func main() { | |
// Connect to database | |
db, err := mysql.DialTCP("host", "user", "password", | |
"go_mysql_test") | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err.String()) | |
} | |
db.Query("CREATE DATABASE IF NOT EXISTS go_mysql_test"); | |
err = db.Query(`CREATE TABLE IF NOT EXISTS test_null_column ( | |
text1 VARCHAR(32), | |
num1 TINYINT, | |
text2 VARCHAR(32) | |
)`) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err.String()) | |
} | |
db.Query(`INSERT INTO test_null_column VALUES ("foo", NULL, "bar")`) | |
// Perform query | |
stmt, err := db.Prepare("SELECT * from test_null_column WHERE text1 = ?") | |
// Get error here | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err.String()) | |
} | |
// Get result set | |
stmt.BindParams("foo") | |
stmt.Execute() | |
var thing Thing | |
stmt.BindResult(&thing.Text1, &thing.Num1, &thing.Text2) | |
// Error occurs on this fetch | |
_, err = stmt.Fetch() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, err.String(), "\n") | |
} | |
fmt.Println(thing) | |
// Insert a value into num1 column | |
db.Query(`UPDATE test_null_column SET num1 = 42 WHERE text1 = "foo"`) | |
stmt.Reset() | |
stmt.Execute() | |
// No error here | |
_, err = stmt.Fetch() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, err.String(), "\n") | |
} | |
fmt.Println(thing) | |
db.Query(`DROP TABLE test_null_column`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment