您的位置:首页 > 数据库 > SQL

golang操作mysql

2013-12-04 10:07 363 查看
001
package main
002
 
003
import (
004
    
"fmt"
005
    
"database/sql"
006
    
_
"mysql"
007
)
008
 
009
type TestMysql
struct

{
010
    
db *sql.DB
011
}
012
 
013
/* 初始化数据库引擎 */
014
func Init() (*TestMysql,error){
015
    
test :=

new
(TestMysql);
016
    
db,err := sql.Open(
"mysql"
,
"test:test@tcp(127.0.0.1:3306)/abwork?charset=utf8"
);
017
    
//第一个参数 : 数据库引擎
018
    
//第二个参数 : 数据库DSN配置。Go中没有统一DSN,都是数据库引擎自己定义的,因此不同引擎可能配置不同
019
    
//本次演示采用http://code.google.com/p/go-mysql-driver
020
    
if

err!=nil {
021
        
fmt.Println(
"database initialize error : "
,err.Error());
022
        
return

nil,err;
023
    
}
024
    
test.db = db;
025
    
return

test,nil;
026
}
027
 
028
/* 测试数据库数据添加 */
029
func (test *TestMysql)Create(){
030
    
if

test.db==nil {
031
        
return
;
032
    
}
033
    
stmt,err := test.db.Prepare(
"insert into test(name,age)values(?,?)"
);
034
    
if

err!=nil {
035
        
fmt.Println(err.Error());
036
        
return
;
037
    
}
038
    
defer stmt.Close();
039
    
if

result,err := stmt.Exec(
"张三"
,20);err==nil {
040
        
if

id,err := result.LastInsertId();err==nil {
041
            
fmt.Println(
"insert id : "
,id);
042
        
}
043
    
}
044
    
if

result,err := stmt.Exec(
"李四"
,30);err==nil {
045
        
if

id,err := result.LastInsertId();err==nil {
046
            
fmt.Println(
"insert id : "
,id);
047
        
}
048
    
}
049
    
if

result,err := stmt.Exec(
"王五"
,25);err==nil {
050
        
if

id,err := result.LastInsertId();err==nil {
051
            
fmt.Println(
"insert id : "
,id);
052
        
}
053
    
}
054
}
055
 
056
/* 测试数据库数据更新 */
057
func (test *TestMysql)Update(){
058
    
if

test.db==nil {
059
        
return
;
060
    
}
061
    
stmt,err := test.db.Prepare(
"update test set name=?,age=? where age=?"
);
062
    
if

err!=nil {
063
        
fmt.Println(err.Error());
064
        
return
;
065
    
}
066
    
defer stmt.Close();
067
    
if

result,err := stmt.Exec(
"周七"
,40,25);err==nil {
068
        
if

c,err := result.RowsAffected();err==nil {
069
            
fmt.Println(
"update count : "
,c);
070
        
}
071
    
}
072
}
073
 
074
/* 测试数据库数据读取 */
075
func (test *TestMysql)Read(){
076
    
if

test.db==nil {
077
        
return
;
078
    
}
079
    
rows,err := test.db.Query(
"select id,name,age from test limit 0,5"
);
080
    
if

err!=nil {
081
        
fmt.Println(err.Error());
082
        
return
;
083
    
}
084
    
defer rows.Close();
085
    
fmt.Println(
""
);
086
    
cols,_ :=  rows.Columns();
087
    
for

i := range cols {
088
        
fmt.Print(cols[i]);
089
        
fmt.Print(
"\t"
);
090
    
}
091
    
fmt.Println(
""
);
092
    
var id

int
;
093
    
var name string;
094
    
var age

int
;
095
    
for

rows.Next(){
096
        
if

err := rows.Scan(&id,&name,&age);err==nil {
097
            
fmt.Print(id);
098
            
fmt.Print(
"\t"
);
099
            
fmt.Print(name);
100
            
fmt.Print(
"\t"
);
101
            
fmt.Print(age);
102
            
fmt.Print(
"\t\r\n"
);
103
        
}
104
    
}
105
}
106
 
107
/* 测试数据库删除 */
108
func (test *TestMysql)Delete(){
109
    
if

test.db==nil {
110
        
return
;
111
    
}
112
    
stmt,err := test.db.Prepare(
"delete from test where age=?"
);
113
    
if

err!=nil {
114
        
fmt.Println(err.Error());
115
        
return
;
116
    
}
117
    
defer stmt.Close();
118
    
if

result,err := stmt.Exec(20);err==nil {
119
        
if

c,err :=  result.RowsAffected();err==nil{
120
            
fmt.Println(
"remove count : "
,c);
121
        
}
122
    
}
123
}
124
 
125
func (test *TestMysql)Close(){
126
    
if

test.db!=nil {
127
        
test.db.Close();
128
    
}
129
}
130
 
131
func main(){
132
    
if

test,err := Init();err==nil {
133
        
test.Create();
134
        
test.Update();
135
        
test.Read();
136
        
test.Delete();
137
        
test.Read();
138
        
test.Close();
139
    
}
140
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  golang mysql