Golang之Mysql操作
话说当年武大郎对着电脑一顿噼里啪啦,,,对mysql增删改查
增加insert
package main import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql" //初始化一个mysql驱动,必须
) 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() {
//"mysql"指定数据库类型, /test指定数据库 root:123 冒号隔开密码 root账号 123密码
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(?,?,?)", "chaoge", "man", "yc_uuu@163.com")
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)
}
insert
查询select
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=?", )
if err != nil {
fmt.Println("exec failed,", err)
return
}
fmt.Println("select succ:", person) }
select
改update
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=?", "chaoge666", )
if err != nil {
fmt.Println("exec failed,", err)
}
}
update
删delete
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=?", )
if err != nil {
fmt.Println("exec failed,", err)
return
}
fmt.Println("delete succ")
}
delete
Golang之Mysql操作的更多相关文章
- golang 的 mysql 操作
goLang的mysql操作,大致可分为三个步骤: 1.下载mysql驱动:go get github.com/go-sql-driver/mysql 2.建立连接:sql.Open("my ...
- 47 【golang】mysql操作
新做了个demo,可以简单了解下mysql的操作:https://github.com/helww/mylab/tree/master/go/mysql
- 使用golang理解mysql的两阶段提交
使用golang理解mysql的两阶段提交 文章源于一个问题:如果我们现在有两个mysql实例,在我们要尽量简单地完成分布式事务,怎么处理? 场景重现 比如我们现在有两个数据库,mysql3306和m ...
- Mysql操作初级
Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...
- python学习道路(day12note)(mysql操作,python链接mysql,redis)
1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...
- 学习笔记:MySQL操作初步
对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...
- ecshop的Mysql操作类
摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...
- shell执行mysql操作
http://ully.iteye.com/blog/1226494 http://www.jb51.net/article/55207.htm shell执行mysql操作 mysql -hhos ...
- mysql操作类库--摘抄
<!--?php /** +---------------------------------- * MySQL操作类库 +---------------------------------- ...
随机推荐
- 理解prototype
从别人的博客里面盗了2张图,这2张图将对象/实例/prototype/__proto__/constructor之间的关系描绘的很清楚. 1.prototype 为 function的属性,实例化的对 ...
- python带参数装饰器使用
# -*- coding: utf-8 -* """TensorFlow指定使用GPU工具类 author: Jill usage: 方法上加@tf_with_devic ...
- linux双网卡绑定实现冗余与负载均衡
1 编辑/etc/modprobe.conf 在/etc/modprobe.conf里加入如下两行: alias bond0 bonding options bond0 mode=1 miimon ...
- canvas学习笔记、小函数整理
http://bbs.csdn.net/topics/391493648 canvas实例分享 2016-3-16 http://bbs.csdn.net/topics/390582151 html5 ...
- leetcode167
public class Solution { public int[] TwoSum(int[] numbers, int target) { Dictionary<int, int> ...
- c++builder XE6 线程 tthread
thread TThread class TSleepFunc : public TCppInterfacedObject<TProc> { public: TSleepFunc(TFor ...
- select 实现多路复用IO的server_socket
select 对程序进行同时检测,当发生响应时,数据被拷贝到内核区域,内核区通知用户程序来进行读取数据,内核区域并不知道是客户端连接,因此需要进行循环 server_socket 端 import s ...
- python 之编写登陆接口
基础需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 升级需求: 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时, ...
- vue深入了解组件——动态组件&异步组件
一.在动态组件上使用 keep-alive 我们之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件: <component v-bind:is="currentTabComp ...
- vue基础——Class与Style绑定
Class与Style绑定 操作元素的class列表和内联样式是数据绑定的一个常见的需求. 因为它们都是属性,所以我们可以用v-bind来处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼 ...