GO-SQL-Driver(MySQL):
https://github.com/Go-SQL-Driver/MySQL
样表:
CREATE TABLE `userinfo` ( `uid` INT(10) NOT NULL AUTO_INCREMENT, `username` VARCHAr(64) NULL DEFAULT NULL, `departname` VARCHAr(64) NULL DEFAULT NULL, `created` DATE NULL DEFAULT NULL, PRIMARY KEY (`uid`))CREATE TABLE `userdetail` ( `uid` INT(10) NOT NULL DEFAULT '0', `intro` TEXT NULL, `profile` TEXT NULL, PRIMARY KEY (`uid`))
样例代码:
package mainimport ( _ "github.com/Go-SQL-Driver/MySQL" "database/sql" "fmt")func main() { db, err := sql.Open("mysql", "reckhou:reckhou@/test?charset=utf8") checkErr(err) //Insert stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?") checkErr(err) res, err := stmt.Exec("reckhou", "IT", "2010-10-02") checkErr(err) id, err := res.LastInsertId() checkErr(err) fmt.Println(id) // Update stmt, err = db.Prepare("update userinfo set username=? where uid=?") checkErr(err) res, err = stmt.Exec("update", id) checkErr(err) affect, err := res.RowsAffected() checkErr(err) fmt.Println(affect) //Query rows, err := db.Query("SELECT * FROM userinfo") checkErr(err) for rows.Next() { var uid int var username string var department string var created string err = rows.Scan(&uid, &username, &department, &created) checkErr(err) fmt.Println(uid) fmt.Println(username) fmt.Println(department) fmt.Println(created) } //Delete stmt, err = db.Prepare("delete from userinfo where uid=?") checkErr(err) res, err = stmt.Exec(id) checkErr(err) affect, err = res.RowsAffected() checkErr(err) fmt.Println(affect) db.Close()}func checkErr(err error) { if err != nil { panic(err) }}
PostgreSQL:
https://github.com/bmizerany/pq
样表:
CREATE TABLE userinfo( uid serial NOT NULL, username character varying(100) NOT NULL, departname character varying(500) NOT NULL, Created date, ConSTRAINT userinfo_pkey PRIMARY KEY (uid))WITH (OIDS=FALSE);CREATE TABLE userdeatail( uid integer, intro character varying(100), profile character varying(100))WITH(OIDS=FALSE);
样例代码:
import ( "database/sql" "fmt" _ "github.com/bmizerany/pq")func main() { db, err := sql.Open("postgres", "user=reckhou password=reckhou dbname=test sslmode=disable") checkErr(err) //Insert stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES(,,) RETURNING uid") checkErr(err) res, err := stmt.Exec("reckhou", "IT", "2010-10-01") checkErr(err) id, err := res.LastInsertId() checkErr(err) fmt.Println(id) // Update stmt, err = db.Prepare("update userinfo set username= where uid=") checkErr(err) res, err = stmt.Exec("update", 1) checkErr(err) affect, err := res.RowsAffected() checkErr(err) fmt.Println(affect) // Query rows, err := db.Query("SELECT * FROM userinfo") checkErr(err) for rows.Next() { var uid int var username string var department string var created string err = rows.Scan(&uid, &username, &department, &created) checkErr(err) fmt.Println(uid) fmt.Println(username) fmt.Println(department) fmt.Println(created) } // Delete stmt, err = db.Prepare("delete from userinfo where uid=") checkErr(err) res, err = stmt.Exec(1) checkErr(err) affect, err = res.RowsAffected() checkErr(err) fmt.Println(affect) db.Close()}func checkErr(err error) { if err != nil { panic(err) }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)