cgo-mysql-for-windows
- 1.安装环境
- 2.编译 && 运行
- 2.1 dll 编译 def
- 2.2 def && dll 编译 a
- 2.3 编译运行exe
- 3.出错处理:
- 3.1 In function `cgo_3a86e410a0e3_Cfunc_mysql_close':
- 3.2 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
- 4.代码 && github
- 4.1 核心代码
- 4.2 github:
- 5.相关资料
- 6.运行结果:
1.安装环境
复制代码
1
2
3
4mingw && mingw-utils mysql:执行users.sql golang 1.13
2.编译 && 运行
2.1 dll 编译 def
复制代码
1
2pexports libmysql.dll > libmysql.def
2.2 def && dll 编译 a
复制代码
1
2dlltool.exe -D libmysql.dll -d libmysql.def -l libmysql.a -k
2.3 编译运行exe
复制代码
1
2go build -x
3.出错处理:
3.1 In function `cgo_3a86e410a0e3_Cfunc_mysql_close’:
复制代码
1
2/tmp/go-build/cgo-gcc-prolog:87: undefined reference to `mysql_close@4'
解决方案:
复制代码
1
2
3找到libmysql.def文件覆盖 "mysql_close" >>>> "mysql_close@4"
3.2 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
解决方案:
复制代码
1
2
3
4
5
6
7
8WINDOWS: SET CGO_ENABLED=1 SET GOOS=windows SET GOARCH=386 GOLAND-Environment: CGO_ENABLED=1;GOOS=windows;GOARCH=386
4.代码 && github
4.1 核心代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95package main /* // -Wl,--allow-multiple-definition for multiple definition #cgo CFLAGS: -I${SRCDIR}/include #cgo LDFLAGS: -Wl,--allow-multiple-definition -L${SRCDIR}/lib -lmysql #include <Windows.h> #include <winsock.h> // for mysql-socket #include <stdio.h> // for c.puts #include <string.h> // for c.strlen #include "mysql.h" // 自定义方法 char * GoNil = NULL; size_t GetMYSQLROWStrLen(MYSQL_ROW row,int j){ return strlen(row[j]); } MYSQL_FIELD GetMYSQLFIELDItem(MYSQL_FIELD * field,int j){ return field[j]; } */ import "C" import ( "fmt" "log" "unsafe" ) const ( maxSize = 1 << 20 ) func Pause() { var str string fmt.Println("") fmt.Print("请按任意键继续...") fmt.Scanln(&str) fmt.Print("程序退出...") } func main() { C.puts(C.CString(" C MYSQL 使用库函数查询…… ")) // 使用C的函数库 初始化 MYSQL * mysql := C.mysql_init(nil) if mysql == nil { log.Fatal("mysql is nil") return } // 使用库连接 MYSQL * C.mysql_real_connect(mysql, C.CString("127.0.0.1"), C.CString("root"), C.CString("root"), C.CString("dongbao"), C.uint(3306), C.GoNil, C.ulong(0)) // 查询函数 int C.mysql_query(mysql, C.CString("select * from users")) // 查询结果 MYSQL_RES * results := C.mysql_store_result(mysql) // 查询的字段数目 unsigned int // 查询结果 char **MYSQL_ROW if results == nil { log.Fatal("result is nil") return } // 查询结果数目 num_rows := int(C.mysql_num_rows(results)) if num_rows > 0 { field := C.mysql_fetch_field(results) cfields := (*[maxSize]C.MYSQL_FIELD)(unsafe.Pointer(field)) num_fields := int(C.mysql_num_fields(results)) fmt.Println("num_rows:", num_rows, "num_fields:", num_fields) for i := 0; i < num_rows; i++ { var row C.MYSQL_ROW = C.mysql_fetch_row(results) rowPtr := (*[maxSize]*[maxSize]byte)(unsafe.Pointer(row)) for j := 0; j < num_fields; j++ { fieldName_StrLen := C.strlen(cfields[j].name) field_name := C.GoBytes(unsafe.Pointer(C.GetMYSQLFIELDItem(field, C.int(j)).name), C.int(fieldName_StrLen)) fieldValue_StrLen := C.GetMYSQLROWStrLen(row, C.int(j)) field_value := C.GoBytes(unsafe.Pointer(rowPtr[j]), C.int(fieldValue_StrLen)) if string(field_value) != "" { fmt.Printf("[%d]field_name is : %s , field_value is : %s n",j, string(field_name), string(field_value)) } } } } // 释放结果 C.mysql_free_result(results) // 关闭mysql C.mysql_close(mysql) C.mysql_server_end() Pause() }
4.2 github:
cgo_mysql_for_windows
5.相关资料
cgo-mysql-for-mac
6.运行结果:
mysql原始数据
golang查询数据
最后
以上就是甜蜜鸡翅最近收集整理的关于[Go实战]CGO 入门系列-手把手教程3 调用 mysql (c语言类库)为案例的全部内容,更多相关[Go实战]CGO内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复