示例#1
0
func Test_Query(t *testing.T) {
	conn, err := sql.Open("mysql", TestConnParam)
	utest.IsNilNow(t, err)
	defer conn.Close()

	var res *sql.Rows

	res, err = conn.Query("SELECT * FROM test ORDER BY id ASC")
	utest.IsNilNow(t, err)
	defer res.Close()

	i := 0
	for res.Next() {
		var (
			id    int64
			value string
		)

		err := res.Scan(&id, &value)
		utest.IsNilNow(t, err)

		utest.Equal(t, id, int64(i))
		utest.Equal(t, value, strconv.Itoa(i))
		i++
	}

	utest.Equal(t, i, 10)
}
示例#2
0
func Test_Execute(t *testing.T) {
	param := TestConnEnv
	param.DbName = "mysql"
	name, _ := json.Marshal(param)

	conn, err := sql.Open("mysql", string(name))
	utest.IsNilNow(t, err)
	defer conn.Close()

	_, err = conn.Exec("CREATE DATABASE " + TestConnEnv.DbName)
	utest.IsNilNow(t, err)

	_, err = conn.Exec("USE " + TestConnEnv.DbName)
	utest.IsNilNow(t, err)

	_, err = conn.Exec(`CREATE TABLE test (
		id INT PRIMARY KEY,
		value VARCHAR(10)
	)`)
	utest.IsNilNow(t, err)

	for i := 0; i < 10; i++ {
		res, err := conn.Exec("INSERT INTO test VALUES(" + strconv.Itoa(i) + ",'" + strconv.Itoa(i) + "')")
		utest.IsNilNow(t, err)
		num, _ := res.RowsAffected()
		utest.Equal(t, num, 1)
	}
}
示例#3
0
func Test_Prepare(t *testing.T) {
	conn, err := sql.Open("mysql", TestConnParam)
	utest.IsNilNow(t, err)
	defer conn.Close()

	var (
		stmt   *sql.Stmt
		res    sql.Result
		reader *sql.Rows
	)

	stmt, err = conn.Prepare("SELECT * FROM test ORDER BY id ASC")
	utest.IsNilNow(t, err)

	reader, err = stmt.Query()
	utest.IsNilNow(t, err)

	i := 0
	for reader.Next() {
		var (
			id    int64
			value string
		)

		err := reader.Scan(&id, &value)
		utest.IsNilNow(t, err)

		utest.Equal(t, id, int64(i))
		utest.Equal(t, value, strconv.Itoa(i))
		i++
	}

	utest.Equal(t, i, 10)

	stmt.Close()

	stmt, err = conn.Prepare("INSERT INTO test VALUES(?, ?)")
	utest.IsNilNow(t, err)

	res, err = stmt.Exec(10, "10")
	utest.IsNilNow(t, err)
	num, _ := res.RowsAffected()
	utest.Equal(t, num, 1)

	stmt.Close()
}