6 changed files with 88 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||
|
package db |
||||
|
|
||||
|
// 定义数据库中用的模型
|
||||
|
type StFileInfo struct { |
||||
|
Id string `json:"id"` |
||||
|
Fpath string `json:"fpath"` |
||||
|
Fhash string `json:"fhash"` |
||||
|
Fname string `json:"fname"` |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
package db |
||||
|
|
||||
|
import ( |
||||
|
"database/sql" |
||||
|
"fmt" |
||||
|
"log" |
||||
|
|
||||
|
_ "github.com/mattn/go-sqlite3" |
||||
|
) |
||||
|
|
||||
|
var SqliteDb *sql.DB |
||||
|
|
||||
|
// init
|
||||
|
func init() { |
||||
|
fmt.Printf("sqlite db init \n") |
||||
|
SqliteDb = InitSqlite() |
||||
|
} |
||||
|
|
||||
|
// init
|
||||
|
func InitSqlite() *sql.DB { |
||||
|
db, err := sql.Open("sqlite3", "./ups.db") |
||||
|
if err != nil { |
||||
|
panic(err.Error()) |
||||
|
} |
||||
|
defer db.Close() |
||||
|
return db |
||||
|
} |
||||
|
|
||||
|
// query
|
||||
|
func Sqlite_query() { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
// add
|
||||
|
// 返回执行后的id
|
||||
|
func Sqlite_add(f StFileInfo) int64 { |
||||
|
sql := "INSERT INTO f_info(fname,fpath,fhash) VALUES (?,?,?)" |
||||
|
// 预处理
|
||||
|
stmt, err := SqliteDb.Prepare(sql) |
||||
|
if err != nil { |
||||
|
log.Fatalln(err) |
||||
|
} |
||||
|
// bind value
|
||||
|
res, err := stmt.Exec(f.Fname, f.Fpath, f.Fhash) |
||||
|
if err != nil { |
||||
|
log.Fatalln(err) |
||||
|
} |
||||
|
lastid, _ := res.LastInsertId() |
||||
|
return lastid |
||||
|
} |
||||
|
|
||||
|
// func (u User) Add() error {
|
||||
|
// _, err := DBConn.Exec("insert into t_user(email, username, password, address) values(?,?,?,?)",
|
||||
|
// u.Email, u.UserName, u.Password, u.Address)
|
||||
|
// if err != nil {
|
||||
|
// fmt.Println("failed to insert t_user ", err)
|
||||
|
// return err
|
||||
|
// }
|
||||
|
// return err
|
||||
|
// }
|
||||
Binary file not shown.
Loading…
Reference in new issue