关于mattn的sqlite3的驱动程序的问题

160 views
Skip to first unread message

平民四月份

unread,
Apr 24, 2012, 10:55:56 PM4/24/12
to golang...@googlegroups.com
本人系统是: win7 64bit + go1
                    RedHat 企业版 5 + go1
因为需要,打算使用sqlite来存储数据,在go-nuts上翻了一圈,找到了mattn的驱动( https://github.com/mattn/go-sqlite3 )

  1.  在windows下 http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/  只提供了32bit的包,所以我手动下载后,将sqlite的c源码与sqlite的go驱动源码放在同一目录,然后执行 go build 然后提示:

C:\Users\lewgun>go build sqlite3lib
# pkg-config --cflags sqlite3
exec: "pkg-config": executable file not found in %PATH%

2. 然后我将 sqlite.go中的:

/*
#include "sqlite3.h"
#include <stdlib.h>
#include <string.h>

static int
_sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
  return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
}

static int
_sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
  return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
}

//#cgo pkg-config: sqlite3                 // 这行注释掉,因为直接编译源码,所以我想可以这样做
*/

3.然后再次执行:
C:\Users\lewgun>go build sqlite3lib 
cc1.exe: sorry, unimplemented: 64-bit mode not compiled in

4.所以windows上的编译失败


然后换用linux

按照提示:
1. [root@localhost ~]# pkg-config --cflags --libs sqlite3
 -lsqlite3  

2. [root@localhost ~]# go get github.com/mattn/go-sqlite3
Cloning into /home/MyProjects/src/github.com/mattn/go-sqlite3...
error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed while accessing https://github.com/mattn/go-sqlite3/info/refs

fatal: HTTP request failed
package github.com/mattn/go-sqlite3: exit status 128



请使用过此package的筒子分享一下,使用经验,thx

--
 Face the sea, for the spring flowers blossoming




Wei guangjing

unread,
Apr 25, 2012, 12:15:37 AM4/25/12
to golang...@googlegroups.com

Windows X64下面需要mingw-w64.


>
> 4.所以windows上的编译失败
>
>
> 然后换用linux
>
> 按照提示:
> 1. [root@localhost ~]# pkg-config --cflags --libs sqlite3
> -lsqlite3
>
> 2. [root@localhost ~]# go get github.com/mattn/go-sqlite3
> # cd .; git clone https://github.com/mattn/go-sqlite3
> /home/MyProjects/src/github.com/mattn/go-sqlite3
> Cloning into /home/MyProjects/src/github.com/mattn/go-sqlite3...
> error: SSL certificate problem, verify that the CA cert is OK. Details:
> error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify
> failed while accessing https://github.com/mattn/go-sqlite3/info/refs
>
> fatal: HTTP request failed
> package github.com/mattn/go-sqlite3: exit status 128
>
>
>
> 请使用过此package的筒子分享一下,使用经验,thx
>
> --
> Face the sea, for the spring flowers blossoming
>
>
>
>

> --
> 官网: http://golang-china.org/
> IRC: irc.freenode.net #golang-china
> @golangchina

平民四月份

unread,
Apr 25, 2012, 12:39:03 AM4/25/12
to golang...@googlegroups.com
韦大求链接

2012/4/25 Wei guangjing <vcc...@gmail.com>

Oling Cat

unread,
Apr 25, 2012, 12:50:11 AM4/25/12
to golang...@googlegroups.com
http://www.mingw.org/
--
Hello! This is Oling Cat!



2012/4/25 平民四月份 <lew...@gmail.com>

平民四月份

unread,
Apr 25, 2012, 12:58:47 AM4/25/12
to golang...@googlegroups.com

minux

unread,
Apr 25, 2012, 1:25:37 AM4/25/12
to golang...@googlegroups.com

平民四月份

unread,
Apr 25, 2012, 2:50:22 AM4/25/12
to golang...@googlegroups.com
thx for every 筒子

2012/4/25 minux <minu...@gmail.com>

--
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina

平民四月份

unread,
Apr 25, 2012, 10:29:22 PM4/25/12
to golang...@googlegroups.com
由于 mattn的pkg,需要pkg-config,各种不会弄,所以根据别人的建议,换用了:
这个,在编译成静态库的时候,出现了一错误:
C:\Users\lewgun\AppData\Local\Temp\go-build398770266\github.com\gwenn\gosqlite\_
obj\meta.cgo2.o: In function `my_table_column_metadata':
E:\MyProjects\Go\the3rd\src\github.com\gwenn\gosqlite/meta.go:27: undefined refe
rence to `sqlite3_table_column_metadata'
collect2: ld returned 1 exit status

然后根据一篇blog,知道需要在编译的时候,设置:
`CFLAGS="-DSQLITE_ENABLE_COLUMN_METADATA`
然后编译通过,生成了相应的.a文件,但是在测试的时候,出现错误:

E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(backup.cgo2.o)(.text): sqlite3_backup_init: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(backup.cgo2.o)(.text): sqlite3_backup_finish: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(backup.cgo2.o)(.text): sqlite3_backup_step: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(backup.cgo2.o)(.text): sqlite3_backup_remaining: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(backup.cgo2.o)(.text): sqlite3_errcode: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(backup.cgo2.o)(.text): sqlite3_backup_pagecount: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(blob.cgo2.o)(.text): sqlite3_blob_open: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(blob.cgo2.o)(.text): sqlite3_blob_write: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(blob.cgo2.o)(.text): sqlite3_blob_read: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(blob.cgo2.o)(.text): sqlite3_blob_reopen: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(blob.cgo2.o)(.text): sqlite3_blob_bytes: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(blob.cgo2.o)(.text): sqlite3_blob_close: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_value_bytes: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_result_error: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_value_blob: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_result_zeroblob: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_value_numeric_type: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_create_function_v2: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_result_error_code: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_user_data: not defined
E:\Mine\SkyDrive\MyProjects\Go\the3rd\pkg\windows_amd64/github.com/gwenn/gosqli
e.a(function.cgo2.o)(.text): sqlite3_aggregate_context: not defined
too many errors



测试代码如下:

package main

import (
"fmt"
)

func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
db, err := sqlite.Open("") // path to db or "" for temp db
check(err)
defer db.Close()
err = db.Exec("CREATE TABLE test(id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL UNIQUE); -- ... and other ddls separated by semi-colon")
check(err)
ins, err := db.Prepare("INSERT INTO test (name) VALUES (?)")
check(err)
defer ins.Finalize()
_, err = ins.Insert("gosqlite driver")
check(err)
s, err := db.Prepare("SELECT name from test WHERE name like ?", "%go%")
check(err)
defer s.Finalize()
var name string
err = s.Select(func(s *sqlite.Stmt) (err error) {
if err = s.Scan(&name); err != nil {
return
}
fmt.Printf("%s\n", name)
return
})

}


求指导


2012/4/25 平民四月份 <lew...@gmail.com>

Wei guangjing

unread,
Apr 26, 2012, 12:28:07 AM4/26/12
to golang...@googlegroups.com
最简单的方法是sqlite用动态库。静态库需要把.a里面的sqlite3.o提取出来加到CGO_OFILE打包到gosqlite包里。

平民四月份

unread,
Apr 26, 2012, 12:39:48 AM4/26/12
to golang...@googlegroups.com
就是不用database包,全自己弄?

2012/4/26 Wei guangjing <vcc...@gmail.com>

Wei guangjing

unread,
Apr 26, 2012, 1:13:23 AM4/26/12
to golang...@googlegroups.com
在 2012年4月26日 下午12:39,平民四月份 <lew...@gmail.com> 写道:
> 就是不用database包,全自己弄?

我的意思是编译libsqlite3时选择编译动态库,go可以轻松连接动态库的.a库文件。

如果想使用libsqlite3的静态库,可以参考go以前版本用的Makefile:
include $(GOROOT)/src/Make.inc

TARG = github.com/mattn/go-sqlite3
CGOFILES = sqlite3.go

CGO_OFILES=\
o/sqlite3.o\

include $(GOROOT)/src/Make.pkg

sqlite3.o可以用ar -x 从libsqlite3.a中提取出来。

平民四月份

unread,
Apr 26, 2012, 3:03:09 AM4/26/12
to golang...@googlegroups.com

"go可以轻松连接动态库的.a库文件。 "
求示例或链接 thx

2012/4/26 Wei guangjing <vcc...@gmail.com>
Reply all
Reply to author
Forward
0 new messages