本地开服务器数据导入sqlslie数据库,供软件调用

微信扫一扫,分享到朋友圈

本地开服务器数据导入sqlslie数据库,供软件调用

package main

import (
“bufio”
“database/sql”
“fmt”
“net/http”
“os”
“strconv”
“time”

"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"

)

var db *sql.DB
var ID int

func ReadLineFile(db *sql.DB, fileName string) {
var i, d int
t1 := time.Now()
if file, err := os.Open(fileName); err != nil {
panic(err)
} else {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d = d + 1
fmt.Println(scanner.Text())
_, err := db.Exec(“INSERT INTO USER (NAME,CODE) VALUES (” + scanner.Text() + “, 0);”)
//fmt.Println(err)
if err == nil {
i = i + 1
}
}
}
t2 := time.Now().Sub(t1).String()
fmt.Printf(“总共%d条数据,插入%d条数据,耗时%s\n”, d, i, t2)
}
func UPDATA(db *sql.DB, ID int) {
fmt.Print(db.Exec(“UPDATE user SET CODE=1 WHERE ID=” + strconv.Itoa(ID-1) + “;”))
}
func main() {
// 打开/创建
var err error
db, err = sql.Open(“sqlite3”, “./my.db”)
defer db.Close()
if err != nil {
panic(err)
} else {
fmt.Println(“打开或创建数据库成功”)
}
table := CREATE TABLE IF NOT EXISTS user(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT UNIQUE,CODE int NOT NULL);
db.Exec(table)
ReadLineFile(db, “test.txt”)

db, err = sql.Open("sqlite3", "./my.db")
if err != nil {
panic(err)
} else {
fmt.Println("打开或创建数据库成功")
}
// db.Exec(`INSERT INTO USER (NAME,CODE) VALUES ("1232323", 0);`)插入数据
// db.Exec("UPDATE user SET code =2 WHERE ID = 6;")更新数据
// rep, _ := db.Query("SELECT * FROM user WHERE ID = 6;")查找数据
// if rep.Next() {
// 	var id int
// 	var name string
// 	var code int
// 	rep.Scan(&id, &name, &code)
// 	fmt.Println(id, name, code)
// }
gin.SetMode(gin.DebugMode) //全局设置环境,此为开发环境,线上环境为gin.ReleaseMode
router := gin.Default()    //获得路由实例
//添加中间件
router.Use(Middleware)
//注册接口
router.GET("/simple/server/get", GetHandler)
router.POST("/simple/server/post", PostHandler)
router.PUT("/simple/server/put", PutHandler)
router.DELETE("/simple/server/delete", DeleteHandler)
//监听端口
http.ListenAndServe(":8080", router)

}
func Middleware(c *gin.Context) {
//fmt.Println(“this is a middleware!”)
}

func GetHandler(c *gin.Context) {
_, exist := c.GetQuery(“key”)
var id int
var name string
var code int
if !exist {
for {
rep, _ := db.Query(“SELECT * FROM user WHERE ID = ” + strconv.Itoa(ID) + “;”) //查找数据
defer rep.Close()
if rep.Next() {
rep.Scan(&id, &name, &code)
if code == 0 {
fmt.Print(“取出一条数据: “)
fmt.Println(id, name, code)
ID = ID + 1
break
}
}
ID = ID + 1
}
}
//c.Data(http.StatusOK, “text/plain”, []byte(fmt.Sprintf(“%s”, name)))
c.Data(http.StatusOK, “text/plain”, []byte(name))
go UPDATA(db, ID)
return
}

func PostHandler(c *gin.Context) {
type JsonHolder struct {
Id int json:"id"
Name string json:"name"
}
holder := JsonHolder{Id: 1, Name: “my name”}
//若返回json数据,可以直接使用gin封装好的JSON方法
c.JSON(http.StatusOK, holder)
return
}
func PutHandler(c *gin.Context) {
c.Data(http.StatusOK, “text/plain”, []byte(“put success!\n”))
return
}
func DeleteHandler(c *gin.Context) {
c.Data(http.StatusOK, “text/plain”, []byte(“delete success!\n”))
return
}

微信扫一扫,分享到朋友圈

本地开服务器数据导入sqlslie数据库,供软件调用

MongoDB教程

上一篇

使用Python调整图像大小

下一篇

你也可能喜欢

本地开服务器数据导入sqlslie数据库,供软件调用

长按储存图像,分享给朋友