都是先更新大家先看下,然后等有时间了我给大家给这些代码家注释,所有代码大家可以查看go.weiduyun.com我都放在这里了
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) type User struct { id int username, password string } var sqldata map[interface{}]interface{} func main() { var u User db, err := sql.Open("mysql", "root:@/test?charset=utf8") check(err) 插入数据 stmt, err := db.Prepare("INSERT user SET username=?,password=?") check(err) res, err := stmt.Exec("xiaowei", "xiaowei") check(err) id, err := res.LastInsertId() check(err) fmt.Println(id) //查询数据 rows, err := db.Query("SELECT * FROM user") check(err) fmt.Println(rows.Columns()) userinfo := make(map[interface{}]interface{}) for rows.Next() { err := rows.Scan(&u.id, &u.username, &u.password) check(err) userinfo[u.id] = u } fmt.Println(userinfo) } func check(err error) { if err != nil { fmt.Println(err) } }
发表评论 取消回复