go sqlite接口封装

有两个现成sqlite的封装网址,直接拿来用即可

https://github.com/phf/go-sqlite3
https://github.com/mattn/go-sqlite3

出于对go调用c原理的理解,

对三个典型的接

sqlite3_open sqlite3_exec sqlite3_close 做了一些go封装测试代码

其中接口参数传递比较典型,可以参考一下

代码如下:

package main

// see sqlite wrapper here
// https://github.com/phf/go-sqlite3
// https://github.com/mattn/go-sqlite3

/*
#cgo CFLAGS: -I../sqlite
#cgo LDFLAGS: -L../lib -lsqlite3.37
#include "sqlite3.h"

int sqlite_callback_bridge(void *data, int argc, char **argv, char **azColName);
char * sqlite_ptr_at(char **ptr, int idx);
typedef void * VoidPtr;
*/
import "C"

import "fmt"
import "unsafe"

//export sqlite_callback
func sqlite_callback(data C.VoidPtr, argc C.int, argv **C.char, azColName **C.char) C.int {
	fmt.Println(C.GoString((*C.char)(data)))
	
	for i:=0; i<int(argc); i ++ {
        //以下针对 **C.char数组访问处理的两种方式,任选一种都可以		
		//argv_i_1 := C.sqlite_ptr_at(argv,      C.int(i))
		tmp_v := uintptr(unsafe.Pointer(argv))  + uintptr(i)*unsafe.Sizeof(*argv)
		argv_i := *(**C.char)( unsafe.Pointer(tmp_v) )
		
		name_i_1 := C.sqlite_ptr_at(azColName, C.int(i))
		//tmp_name := uintptr(unsafe.Pointer(azColName))  + uintptr(i)*unsafe.Sizeof(*azColName)		
		//name_i := *(**C.char)(unsafe.Pointer(tmp_name))


		//fmt.Printf("%p-%p-%x-%p %p-%p %d\r\n", argv_i_1, argv_i, tmp_v, unsafe.Pointer(tmp_v), name_i_1, name_i, int(uintptr(i)*unsafe.Sizeof(*argv)))
		fmt.Print(C.GoString(name_i_1), ":", C.GoString(argv_i), "	")
	}
	fmt.Println()

	return 0
}


func main(){
	v := C.GoString(C.sqlite3_libversion())
	vi := C.sqlite3_libversion_number()
	fmt.Printf("%s %d\r\n", v, vi)


	var hdb *C.sqlite3 = nil

	
	dbname := "test.db"
	dbnamebin := []byte(dbname)

	rc := C.sqlite3_open((*C.char)(unsafe.Pointer(&dbnamebin[0])), &hdb);
	fmt.Println("sqlite3 open", rc)

	sql := `CREATE TABLE test_tbl(
         ID INT         KEY     NOT NULL,
         NAME           TEXT    NOT NULL,
         AGE            INT     NOT NULL,
         ADDRESS        CHAR(50),
         SALARY         REAL );`

	sqlbin := []byte(sql)

	var errStr *C.char = nil
	C.sqlite3_exec(hdb,
		(*C.char)(unsafe.Pointer(&sqlbin[0])),
		(C.sqlite3_callback)(unsafe.Pointer(C.sqlite_callback_bridge)),
		unsafe.Pointer(&dbnamebin[0]),
		&errStr)

	fmt.Println("create tbl", C.GoString(errStr))


	sqlInsert := `INSERT INTO test_tbl (ID,NAME,AGE,ADDRESS,SALARY)
         VALUES
         (1, 'Paul', 32, 'California', 20000.00 ),
         (2, 'Allen', 25, 'Texas', 15000.00 ), 
         (3, 'Teddy', 23, 'Norway', 20000.00 ),
         (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );`

	fmt.Println(sqlInsert)
	sqlInsertBin := []byte(sqlInsert)

	rc = C.sqlite3_exec(hdb,
		(*C.char)(unsafe.Pointer(&sqlInsertBin[0])),
		(C.sqlite3_callback)(unsafe.Pointer(C.sqlite_callback_bridge)),
		unsafe.Pointer(&dbnamebin[0]),
		&errStr)
	fmt.Println("rc:", rc, "zErrMsg", C.GoString(errStr))


	sqlSelect := "SELECT * from test_tbl"
	sqlSelectBin := []byte(sqlSelect)

	rc = C.sqlite3_exec(hdb,
		(*C.char)(unsafe.Pointer(&sqlSelectBin[0])),
		(C.sqlite3_callback)(unsafe.Pointer(C.sqlite_callback_bridge)),
		unsafe.Pointer(&dbnamebin[0]),
		&errStr)

	fmt.Println("rc:", rc, "zErrMsg", C.GoString(errStr))

	C.sqlite3_close(hdb)
}

sqlite3_exec的回调函数桥接代码

package main

/*
#include <stdio.h>

int sqlite_callback(void *data, int argc, char **argv, char **azColName);

char * sqlite_ptr_at(char **ptr, int idx)
{
    return ptr[idx];
}
 
// The gateway function
int sqlite_callback_bridge(void *data, int argc, char **argv, char **azColName){
   sqlite_callback(data, argc, argv, azColName);
   return 0;
}
*/
import "C"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值