想要学习一下使用Go操作SQLite数据库
package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type LogStatistics struct {
gorm.Model
logStatisticsId string
topVisitArticleList string
topCoastTimeRequestList string
createTime int64
updateTime int64
}
func main() {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// 迁移 schema
err = db.AutoMigrate(&LogStatistics{})
if err != nil {
return
}
// Create
db.Create(&LogStatistics{topVisitArticleList: "00", topCoastTimeRequestList: "xx"})
// Read
var logStatistics LogStatistics
// 根据整型主键查找
db.First(&logStatistics, 1)
// 查找 code 字段值为 D42 的记录
db.First(&logStatistics, "topVisitArticleList = ?", "00")
//// Update - 将 logStatistics 的 price 更新为 200
//db.Model(&logStatistics).Update("Price", 200)
//// Update - 更新多个字段
//db.Model(&logStatistics).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段
//db.Model(&logStatistics).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})
//
//// Delete - 删除 logStatistics
//db.Delete(&logStatistics, 1)
}
GOROOT=D:\ProgramData\GOPATH\go1.17.7 #gosetup
GOPATH=D:\ProgramData\GOPATH #gosetup
D:\ProgramData\GOPATH\go1.17.7\bin\go.exe build -o C:\Users\HP\AppData\Local\Temp\GoLand\___go_build_admin_data_show.exe admin_data_show #gosetup
# github.com/mattn/go-sqlite3
cc1plus.exe: warning: command line option '-std=gnu99' is valid for C/ObjC but not for C++
_cgo_export.c: In function 'void _cgo_1ad64cd63b8b_Cfunc__Cmalloc(void*)':
_cgo_export.c:233:53: error: invalid conversion from 'void*' to '_cgo_1ad64cd63b8b_Cfunc__Cmalloc(void*)::<unnamed struct>*' [-fpermissive]
} __attribute__((__packed__, __gcc_struct__)) *a = v;
Compilation finished with exit code 2
根据网上的一些教程:尝试安装https://jmeubank.github.io/tdm-gcc/articles/2021-05/10.3.0-release的GCC程序,没用,依旧是这个报错
Go初学者,无从下手了
正常运行并能够操作数据库
从错误信息来看,是由用C++编译器编译C文件引起的。在C语言里,void *
类型可以隐式转换为其他指针类型,当在C++里却必须显示转换才行。
解决这个问题,你可以指定用C编译器来编译C文件,比如用gcc编译器
go env -w "CC=gcc.exe"
题主,Go支持sqlite的驱动也比较多,但是好多都是不支持database/sql接口的
https://github.com/mattn/go-sqlite3 支持database/sql接口,基于cgo(关于cgo的知识请参看官方文档
https://github.com/feyeleanor/gosqlite3 不支持database/sql接口,基于cgo写的
https://github.com/phf/go-sqlite3 不支持database/sql接口,基于cgo写的
目前支持database/sql的SQLite数据库驱动只有第一个,我目前也是采用它来开发项目的。采用标准接口有利于以后出现更好的驱动的时候做迁移。
我是用这个库:
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "./foo.db")
checkErr(err)
//插入数据
stmt, err := db.Prepare("INSERT INTO userinfo(username, departname, created) values(?,?,?)")
checkErr(err)
res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09")
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
fmt.Println(id)
//更新数据
stmt, err = db.Prepare("update userinfo set username=? where uid=?")
checkErr(err)
res, err = stmt.Exec("astaxieupdate", id)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
//查询数据
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(&uid, &username, &department, &created)
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}
//删除数据
stmt, err = db.Prepare("delete from userinfo where uid=?")
checkErr(err)
res, err = stmt.Exec(id)
checkErr(err)
affect, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}