Example #1
0
func main() {
	// Create new instance
	db := mysql.New()
	// Enable logging
	db.Logging = true
	// Connect to database
	db.Connect("localhost", "root", "", "gotesting", "/tmp/mysql.sock")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Repeat query forever
	for {
		res, _ := db.Query("select * from test1")
		// On error reconnect to the server
		if res == nil {
			fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
			done := make(chan int)
			go reconnect(db, done)
			<-done
		}
		// Sleep for 0.5 seconds
		time.Sleep(500000000)
	}
}
Example #2
0
func initDatabase() bool {
	// Create new instance
	g_db = mysql.New()

	// Enable logging
	g_db.Logging = true

	// Fetch database info from conf file
	dbHost, _ := g_config.GetString("database", "host")
	dbUser, _ := g_config.GetString("database", "user")
	dbPass, _ := g_config.GetString("database", "pass")
	dbData, _ := g_config.GetString("database", "db")

	// Enable/Disable intern mysql logging system
	g_db.Logging, _ = g_config.GetBool("database", "show_log")

	// Connect to database
	err := g_db.Connect(dbHost, dbUser, dbPass, dbData)
	if err != nil {
		g_logger.Printf("[Error] Could not connect to database: %v\n\r", err)
		return false
	}

	return true
}
Example #3
0
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{}

	// Create new instance
	db := mysql.New()

	// Enable logging
	db.Logging = true

	// Connect to database
	if err = db.Connect(*dbhost, *dbuser, *dbpass, *dbname); err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}

	defer db.Close()

	// Use UTF8
	if _, err = db.Query("SET NAMES utf8"); err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		return
	}

	// Query database
	if res, err = db.Query("SELECT * FROM test1 LIMIT 5"); err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		return
	}

	for {
		if row = res.FetchMap(); row == nil {
			break
		}

		for key, value = range row {
			fmt.Printf("%s:%v\n", key, value)
		}
	}

}
Example #4
0
func main() {
	// Create new instance
	db := mysql.New()
	// Enable logging
	db.Logging = false
	// Connect to database
	db.Connect("localhost", "root", "", "gotesting", "/tmp/mysql.sock")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Use UTF8
	db.Query("SET NAMES utf8")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Initialise statement
	stmt, _ := db.InitStmt()
	// Prepare statement
	stmt.Prepare("SELECT * FROM test1 WHERE id > ? AND id < ?")
	if stmt.Errno != 0 {
		fmt.Printf("Error #%d %s\n", stmt.Errno, stmt.Error)
		os.Exit(1)
	}
	// Bind params
	stmt.BindParams(1, 5)
	// Execute statement
	res, _ := stmt.Execute()
	if stmt.Errno != 0 {
		fmt.Printf("Error #%d %s\n", stmt.Errno, stmt.Error)
		os.Exit(1)
	}
	// Display results
	var row map[string]interface{}
	for {
		row = res.FetchMap()
		if row == nil {
			break
		}
		for key, value := range row {
			fmt.Printf("%s:%v\n", key, value)
		}
	}
	// Close statement
	stmt.Close()
	// Close connection
	db.Close()
}
Example #5
0
func main() {
	// Create new instance
	db := mysql.New()
	// Enable logging
	db.Logging = false
	// Connect to database
	db.Connect("localhost", "root", "", "gotesting", "/tmp/mysql.sock")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Use UTF8
	db.Query("SET NAMES utf8")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Query database
	res, _ := db.Query("SELECT fname, lname FROM test1 LIMIT 5")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}

	defer db.Close()

	// Display results
	var row map[string]interface{}
	for {
		row = res.FetchMap()
		if row == nil {
			break
		}
		//		for _, value := range row {
		//			fmt.Printf("%v",  value)
		//		}
		fmt.Printf("%s %s\n", row["fname"], row["lname"])

	}
	// Close connection

}
Example #6
0
func main() {
	// Create new instance
	db := mysql.New()
	// Enable logging
	db.Logging = true
	// Connect to database
	db.Connect("localhost", "root", "********", "gotesting")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Use UTF8
	db.Query("SET NAMES utf8")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Query database
	res := db.Query("SELECT * FROM test1 LIMIT 5")
	if db.Errno != 0 {
		fmt.Printf("Error #%d %s\n", db.Errno, db.Error)
		os.Exit(1)
	}
	// Display results
	var row map[string]interface{}
	for {
		row = res.FetchMap()
		if row == nil {
			break
		}
		for key, value := range row {
			fmt.Printf("%s:%v\n", key, value)
		}
	}
	// Close connection
	db.Close()
}
Example #7
0
func main() {
	flag.Parse()

	if *dbhost == "" || *dbname == "" || *dbuser == "" {
		flag.Usage()
		os.Exit(1)
	}

	var err os.Error

	// 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()

	done := make(chan bool)

	// Repeat query forever
	for {
		if _, err = db.Query("SELECT * FROM test1 LIMIT 5"); err != nil {
			fmt.Fprintf(os.Stderr, "%v\n", err)
			go reconnect(db, done)
			<-done
		}

		// Sleep for 0.5 seconds
		time.Sleep(5e8)
	}
}
Example #8
0
func getMysql() (db *mysql.MySQL) {
	db = mysql.New()
	db.Connect("sql-s1", "cobi", MySQLPassword, "enwiki_p")
	return db
}
Example #9
0
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)
		}
	}
}