我是靠谱客的博主 诚心航空,最近开发中收集的这篇文章主要介绍jmoiron sqlx mysql_mysql(或其他数据库),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

[email protected](127.0.0.1:3306)/dbname")

insert操作:

r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "[email protected]")

package main

import (

"fmt"

_ "github.com/go-sql-driver/mysql"

"github.com/jmoiron/sqlx"

)

type Person struct {

UserId int `db:"user_id"`

Username string `db:"username"`

Sex string `db:"sex"`

Email string `db:"email"`

}

type Place struct {

Country string `db:"country"`

City string `db:"city"`

TelCode int `db:"telcode"`

}

//此处的db是线程安全的,实现了连接池

var Db *sqlx.DB

func init() {

database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

}

func main() {

r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "[email protected]")

if err != nil {

fmt.Println("exec failed, ", err)

return

}

id, err := r.LastInsertId()

if err != nil {

fmt.Println("exec failed, ", err)

return

}

fmt.Println("insert succ:", id)

}

Select 操作:

err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1)

package main

import (

"fmt"

_ "github.com/go-sql-driver/mysql"

"github.com/jmoiron/sqlx"

)

type Person struct {

UserId int `db:"user_id"`

Username string `db:"username"`

Sex string `db:"sex"`

Email string `db:"email"`

}

type Place struct {

Country string `db:"country"`

City string `db:"city"`

TelCode int `db:"telcode"`

}

var Db *sqlx.DB

func init() {

database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

}

func main() {

var person []Person

err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1)

if err != nil {

fmt.Println("exec failed, ", err)

return

}

fmt.Println("select succ:", person)

}

update操作:

_, err := Db.Exec("update person set username=? where user_id=?", "stu0001", 1)

package main

import (

"fmt"

_ "github.com/go-sql-driver/mysql"

"github.com/jmoiron/sqlx"

)

type Person struct {

UserId int `db:"user_id"`

Username string `db:"username"`

Sex string `db:"sex"`

Email string `db:"email"`

}

type Place struct {

Country string `db:"country"`

City string `db:"city"`

TelCode int `db:"telcode"`

}

var Db *sqlx.DB

func init() {

database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

}

func main() {

_, err := Db.Exec("update person set username=? where user_id=?", "stu0001", 1)

if err != nil {

fmt.Println("exec failed, ", err)

return

}

}

Delete 操作:

_, err := Db.Exec("delete from person where user_id=?", 1)

package main

import (

"fmt"

_ "github.com/go-sql-driver/mysql"

"github.com/jmoiron/sqlx"

)

type Person struct {

UserId int `db:"user_id"`

Username string `db:"username"`

Sex string `db:"sex"`

Email string `db:"email"`

}

type Place struct {

Country string `db:"country"`

City string `db:"city"`

TelCode int `db:"telcode"`

}

var Db *sqlx.DB

func init() {

database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")

if err != nil {

fmt.Println("open mysql failed,", err)

return

}

Db = database

}

func main() {

_, err := Db.Exec("delete from person where user_id=?", 1)

if err != nil {

fmt.Println("exec failed, ", err)

return

}

fmt.Println("delete succ")

}

原文:http://blog.51cto.com/5660061/2350179

最后

以上就是诚心航空为你收集整理的jmoiron sqlx mysql_mysql(或其他数据库)的全部内容,希望文章能够帮你解决jmoiron sqlx mysql_mysql(或其他数据库)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部