コード例 #1
0
ファイル: prepared_stmt.go プロジェクト: v-gu/mysqltc
func main() {
	user := "******"
	pass := "******"
	dbname := "test"
	//proto  := "unix"
	//addr   := "/var/run/mysqld/mysqld.sock"
	proto := "tcp"
	addr := "127.0.0.1:3306"

	db := mymy.New(proto, "", addr, user, pass, dbname)
	//db.Debug = true

	fmt.Printf("Connect to %s:%s... ", proto, addr)
	checkError(db.Connect())
	printOK()

	fmt.Print("Drop A table if exists... ")
	_, err := db.Start("drop table A")
	if err == nil {
		printOK()
	} else if e, ok := err.(*mymy.Error); ok {
		// Error from MySQL server
		fmt.Println(e)
	} else {
		checkError(err)
	}

	fmt.Print("Create A table... ")
	checkedResult(db.Query("create table A (txt varchar(40), number int)"))
	printOK()

	fmt.Print("Prepare insert statement... ")
	ins, err := db.Prepare("insert A values (?, ?)")
	checkError(err)
	printOK()

	fmt.Print("Prepare select statement... ")
	sel, err := db.Prepare("select * from A where number > ?")
	checkError(err)
	printOK()

	params := struct {
		txt    *string
		number *int
	}{}

	fmt.Print("Bind insert parameters... ")
	ins.BindParams(&params)
	printOK()

	fmt.Print("Insert into A... ")
	for ii := 0; ii < 1000; ii += 100 {
		if ii%500 == 0 {
			// Assign NULL values to the parameters
			params.txt = nil
			params.number = nil
		} else {
			// Modify parameters
			str := fmt.Sprintf("%d*10= %d", ii/100, ii/10)
			params.txt = &str
			params.number = &ii
		}
		// Execute statement with modified data
		_, err = ins.Run()
		checkError(err)
	}
	printOK()

	fmt.Println("Select from A... ")
	rows, res := checkedResult(sel.Exec(0))
	name := res.Map["name"]
	number := res.Map["number"]
	for ii, row := range rows {
		fmt.Printf(
			"Row: %d\n name:  %-10s {%#v}\n number: %-8d  {%#v}\n", ii,
			"'"+row.Str(name)+"'", row.Data[name],
			row.Int(number), row.Data[number],
		)
	}

	fmt.Print("Remove A... ")
	checkedResult(db.Query("drop table A"))
	printOK()

	fmt.Print("Close connection... ")
	checkError(db.Close())
	printOK()
}
コード例 #2
0
ファイル: simple.go プロジェクト: v-gu/mysqltc
func main() {
	user := "******"
	pass := "******"
	dbname := "test"
	//proto  := "unix"
	//addr   := "/var/run/mysqld/mysqld.sock"
	proto := "tcp"
	addr := "127.0.0.1:3306"

	db := mymy.New(proto, "", addr, user, pass, dbname)
	//db.Debug = true

	fmt.Printf("Connect to %s:%s... ", proto, addr)
	checkError(db.Connect())
	printOK()

	fmt.Print("Drop A table if exists... ")
	_, err := db.Start("drop table A")
	if err == nil {
		printOK()
	} else if e, ok := err.(*mymy.Error); ok {
		// Error from MySQL server
		fmt.Println(e)
	} else {
		checkError(err)
	}

	fmt.Print("Create A table... ")
	checkedResult(db.Query("create table A (txt varchar(40), number int)"))
	printOK()

	fmt.Print("Insert into A... ")
	for ii := 0; ii < 10; ii++ {
		if ii%5 == 0 {
			checkedResult(db.Query("insert A values (null, null)"))
		} else {
			checkedResult(db.Query(
				"insert A values ('%d*10= %d', %d)", ii, ii*10, ii*100,
			))
		}
	}
	printOK()

	fmt.Println("Select from A... ")
	rows, res := checkedResult(db.Query("select * from A"))
	name := res.Map["name"]
	number := res.Map["number"]
	for ii, row := range rows {
		fmt.Printf(
			"Row: %d\n name:  %-10s {%#v}\n number: %-8d  {%#v}\n", ii,
			"'"+row.Str(name)+"'", row.Data[name],
			row.Int(number), row.Data[number],
		)
	}

	fmt.Print("Remove A... ")
	checkedResult(db.Query("drop table A"))
	printOK()

	fmt.Print("Close connection... ")
	checkError(db.Close())
	printOK()
}
コード例 #3
0
ファイル: gaz.go プロジェクト: wingyplus/Gaz
func (database *Database) new() {
	database.Connection.MySQL = mymy.New(proto, laddr, raddr, user, pass, database.Db_name)
}
コード例 #4
0
ファイル: long_data.go プロジェクト: v-gu/mysqltc
func main() {
	user := "******"
	pass := "******"
	dbname := "test"
	//proto  := "unix"
	//addr   := "/var/run/mysqld/mysqld.sock"
	proto := "tcp"
	addr := "127.0.0.1:3306"

	db := mymy.New(proto, "", addr, user, pass, dbname)
	//db.Debug = true

	fmt.Printf("Connect to %s:%s... ", proto, addr)
	checkError(db.Connect())
	printOK()

	fmt.Print("Drop 'web' table if exists... ")
	_, err := db.Start("DROP TABLE web")
	if err == nil {
		printOK()
	} else if e, ok := err.(*mymy.Error); ok {
		// Error from MySQL server
		fmt.Println(e)
	} else {
		checkError(err)
	}

	fmt.Print("Create 'web' table... ")
	_, err = db.Start("CREATE TABLE web (url VARCHAR(80), content LONGBLOB)")
	checkError(err)
	printOK()

	fmt.Print("Prepare insert statement... ")
	ins, err := db.Prepare("INSERT INTO web VALUES (?, ?)")
	checkError(err)
	printOK()

	fmt.Print("Prepare select statement... ")
	sel, err := db.Prepare("SELECT url, OCTET_LENGTH(content) FROM web")
	checkError(err)
	printOK()

	var url string

	fmt.Print("Bind insert parameters... ")
	ins.BindParams(&url, nil)
	printOK()

	fmt.Println()
	for {
		url = ""
		fmt.Print("Please enter an URL (blank line terminates input): ")
		fmt.Scanln(&url)
		if len(url) == 0 {
			break
		}
		if !strings.Contains(url, "://") {
			url = "http://" + url
		}
		http_res, _, err := http.Get(url)
		if err != nil {
			fmt.Println(err)
			continue
		}
		// Retrieve response directly into database. Use 8 kB buffer.
		checkError(ins.SendLongData(1, http_res.Body, 8192))
		_, err = ins.Run()
		checkError(err)
	}
	fmt.Println()

	fmt.Print("Select from 'web' table... ")
	rows, res, err := sel.Exec()
	checkError(err)
	printOK()

	// Print fields names
	fmt.Println()
	for _, field := range res.Fields {
		fmt.Printf("%-38s ", field.Name)
	}
	fmt.Println()
	fmt.Println("------------------------------------------------------------")

	// Print result
	for _, row := range rows {
		for ii, col := range row.Data {
			if col == nil {
				fmt.Print("%-38s ", "NULL")
			} else {
				fmt.Printf("%-38s ", row.Bin(ii))
			}
		}
		fmt.Println()
	}
	fmt.Println()

	fmt.Print("Hit ENTER to exit ")
	fmt.Scanln()

	fmt.Print("Remove 'web' table... ")
	_, err = db.Start("DROP TABLE web")
	checkError(err)
	printOK()

	fmt.Print("Close connection... ")
	checkError(db.Close())
	printOK()
}