Example #1
0
func defaultConn(t *testing.T) *db.Connection {
	conn, e := mysql.Open("//root@localhost:3306/test")
	if conn == nil || e != nil {
		t.Error("Couldn't connect to [email protected]:3306:test", e)
		return nil
	}
	return &conn
}
Example #2
0
func main() {
	version, e := mysql.Version()
	if e != nil {
		fmt.Printf("error: %s\n", e.String())
	}
	for k, v := range version {
		fmt.Printf("version[%s] == %s\n", k, v)
	}

	info := mysql.ConnectionInfo{
		"host":   "localhost",
		"port":   3306,
		"uname":  "travist",
		"pass":   "******",
		"dbname": "golang"}
	c, e := mysql.Open(info)
	if e != nil {
		fmt.Printf("open error: %s\n", e.String())
		os.Exit(1)
	}
	fmt.Printf("connection: %s\n", c)
	fmt.Printf("About to prepare statement\n")
	s, e := c.Prepare("CREATE TEMPORARY TABLE __hello (i INT)")
	if e != nil {
		fmt.Printf("error: %s\n", e.String())
		os.Exit(1)
	}
	fmt.Printf("statement: %s\n", s)

	fmt.Printf("About to execute query\n")
	cur, e := c.Execute(s)
	if e != nil {
		fmt.Printf("error: %s\n", e.String())
		os.Exit(1)
	}
	fmt.Printf("cursor: %s\n", cur)

	fmt.Printf("Inserting 30 ints\n")
	for i := 0; i < 30; i += 1 {
		s, e = c.Prepare("INSERT INTO __hello (i) values(%d)")
		cur, e = c.Execute(s, i+1)
		if e != nil {
			fmt.Printf("insert error: %s\n", e.String())
			os.Exit(1)
		}
	}

	s, e = c.Prepare("SELECT * FROM __hello ORDER BY i ASC")
	if e != nil {
		fmt.Printf("error: %s\n", e.String())
		os.Exit(1)
	}
	fmt.Printf("statement: %s\n", s)

	fmt.Printf("About to execute query\n")
	cur, e = c.Execute(s)
	if e != nil {
		fmt.Printf("error: %s\n", e.String())
		os.Exit(1)
	}
	fmt.Printf("cursor: %s\n", cur)

	fmt.Printf("About to fech one row\n")
	tuple, e := cur.FetchOne()
	for i := 0; tuple != nil; tuple, e = cur.FetchOne() {
		i++
		fmt.Printf("row[%d]: %s\n", i, tuple[0])
	}
	cur.Close()

	fmt.Printf("=====\n\nAbout to fetch many row\n")
	s, e = c.Prepare("SELECT * FROM __hello ORDER BY i ASC")
	cur, e = c.Execute(s)
	rows, e := cur.FetchMany(10)
	fmt.Printf("%s\n", rows)
	for _, y := range rows {
		fmt.Printf("data: %s\n", y)
	}
	cur.Close()

	fmt.Printf("=====\n\nAbout to fetch all row\n")
	s, e = c.Prepare("SELECT * FROM __hello ORDER BY i ASC")
	cur, e = c.Execute(s)
	rows, e = cur.FetchAll()
	fmt.Printf("%s\n", rows)
	for _, y := range rows {
		fmt.Printf("data: %s\n", y)
	}
	cur.Close()

	e = c.Close()
	if e != nil {
		fmt.Printf("close error: %s\n", e.String())
		os.Exit(1)
	}
}
Example #3
0
func main() {
	flag.Parse()
	if help {
		flag.Usage()
		os.Exit(1)
	}

	conn, err := mysql.Open(fmt.Sprintf("mysql://%s:%s@%s:%d/%s",
		user,
		pass,
		host,
		port,
		dbname))

	if err != nil {
		fmt.Printf("Error connecting to %s:%d: %s\n",
			host, port, err)
		flag.Usage()
		os.Exit(1)
	}
	var stmt db.Statement

	// Create table
	fmt.Printf("Creating temporary table %s.__hello\n", dbname)

	stmt, e := conn.Prepare(
		"CREATE TEMPORARY TABLE __hello (i INT, s VARCHAR(255))")
	if e != nil {
		errorAndQuit(e)
	}

	_, e = conn.Execute(stmt)
	if e != nil {
		errorAndQuit(e)
	}

	// Populate table
	fmt.Println("Inserting 100 random numbers")

	stmt, e = conn.Prepare("INSERT INTO __hello (i, s) VALUE (?, ?)")
	if e != nil {
		errorAndQuit(e)
	}

	for i := 0; i < 100; i += 1 {
		_, e = conn.Execute(stmt, rand.Int(), fmt.Sprintf("id%d", rand.Int()))
		if e != nil {
			errorAndQuit(e)
		}
	}
	stmt.Close()

	// Read from table
	fmt.Println("Reading numbers in numeric order")

	stmt, e = conn.Prepare(
		"SELECT i, s FROM __hello WHERE s LIKE ? ORDER BY i ASC")
	if e != nil {
		errorAndQuit(e)
	}

	rs, cErr := conn.Execute(stmt, "id%")
	if cErr != nil {
		errorAndQuit(cErr)
	}

	for res := range rs.Iter() {
		row := res.Data()

		if v, ok := row[0].(int); ok {
			fmt.Printf("%d %v\n", v, row[1])
		} else {
			fmt.Printf("Error converting %T to int\n", v)
		}
	}
	stmt.Close()
	conn.Close()
}
Example #4
0
func main() {
    version, e := mysql.Version();
    if e != nil {
        fmt.Printf("error: %s\n", e.String());
    }
    for k, v := range version {
        fmt.Printf("version[%s] == %s\n", k, v);
    }

    info := mysql.ConnectionInfo{ 
        "host": "localhost",
        "port": 3306,
        "uname": "yone098",
        "pass": "******",
        "dbname": "golang"
     };
    c, e := mysql.Open(info);
    if e != nil {
        fmt.Printf("open error: %s\n", e.String());
        os.Exit(1);
    }
    fmt.Printf("connection: %s\n", c);
    fmt.Printf("About to prepare statement\n");
    s, e := c.Prepare("CREATE TEMPORARY TABLE __hello (i INT)");
    if e != nil {
        fmt.Printf("error: %s\n", e.String());
        os.Exit(1);
    }
    fmt.Printf("statement: %s\n", s);

    fmt.Printf("About to execute query\n");
    cur, e := c.Execute(s);
    if e != nil {
        fmt.Printf("error: %s\n", e.String());
        os.Exit(1);
    }
    fmt.Printf("corsor: %s\n", cur);

    fmt.Printf("Inserting 30 random ints");
    for i := 0; i < 30; i+=1 {
        s, e = c.Prepare("INSERT INTO __hello (i) values(%d)");
        cur, e = c.Execute(s, rand.Int());
        if e != nil {
            fmt.Printf("insert error: %s\n", e.String());
            os.Exit(1);
        }
    }

    s, e = c.Prepare("SELECT * FROM __hello ORDER BY i ASC");
    if e != nil {
        fmt.Printf("error: %s\n", e.String());
        os.Exit(1);
    }
    fmt.Printf("statement: %s\n", s);

    fmt.Printf("About to execute query\n");
    cur, e = c.Execute(s);
    if e != nil {
        fmt.Printf("error: %s\n", e.String());
        os.Exit(1);
    }
    fmt.Printf("corsor: %s\n", cur);

    fmt.Printf("About to fech one row\n");
    tuple, e := cur.FetchOne();
    for i := 0; tuple != nil; tuple, e = cur.FetchOne() {
        i++;
        fmt.Printf("row[%d]: %s\n", i, tuple[0]);
    }
    cur.Close();

    fmt.Printf("=====\n\nAbout to fetch many row\n");
    s, e = c.Prepare("SELECT * FROM __hello ORDER BY i ASC");
    cur, e = c.Execute(s);
    rows, e := cur.FetchMany(10);
    fmt.Printf("%s\n", rows);
    for _, y := range rows {
        fmt.Printf("data: %s\n", y);
    }
    cur.Close();

    fmt.Printf("=====\n\nAbout to fetch all row\n");
    s, e = c.Prepare("SELECT * FROM __hello ORDER BY i ASC");
    cur, e = c.Execute(s);
    rows, e = cur.FetchAll();
    fmt.Printf("%s\n", rows);
    for _, y := range rows {
        fmt.Printf("data: %s\n", y);
    }
    cur.Close();

    e = c.Close();
    if e != nil {
        fmt.Printf("close error: %s\n", e.String());
        os.Exit(1);
    }
}