-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"database/sql"
"log"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/taosdata/driver-go/taosSql"
"time"
)
var (
db1 *sql.DB
db2 *sql.DB
err error
dbName string
tableName string
)
func Init(){
tableName = "test"
dbName = "test"
db1, err = sql.Open("taosSql", "root:taosdata@/tcp(tdengine:0)/")
checkErr(err)
res, err := db1.Exec("create database test; ")
if err != nil {
log.Println(res)
}
checkErr(err)
db2, err = sql.Open("taosSql", "root:taosdata@/tcp(tdengine:0)/test")
checkErr(err)
}
func CreateTable(db *sql.DB, demot string) {
st := time.Now().Nanosecond()
// create table
res, err := db.Exec("create table test.test (ts timestamp, id int, name binary(20), len tinyint, flag bool, notes binary(20), fv float, dv double)")
checkErr(err)
affectd, err := res.RowsAffected()
checkErr(err)
et := time.Now().Nanosecond()
fmt.Printf("create table result:\n %d row(s) affectd (%6.6fs)\n\n", affectd, (float32(et-st))/1E9)
}
func InsertData(db *sql.DB, dbName string) {
st := time.Now().Nanosecond()
// insert data into table
stmt, err := db.Prepare("insert into ? values(?, ?, ?, ?, ?, ?, ?, ?) (?, ?, ?, ?, ?, ?, ?, ?) (?, ?, ?, ?, ?, ?, ?, ?)")
checkErr(err)
res, err := stmt.Exec(dbName, "now", 1000, "'haidian'", 6, true, "'AI world'", 6987.654, 321.987,
"now+1s", 1001, "'changyang'", 7, false, "'DeepMode'", 12356.456, 128634.456,
"now+2s", 1002, "'chuangping'", 8, true, "'database'", 3879.456, 65433478.456)
checkErr(err)
affectd, err := res.RowsAffected()
checkErr(err)
et := time.Now().Nanosecond()
fmt.Printf("insert data result:\n %d row(s) affectd (%6.6fs)\n\n", affectd, (float32(et-st))/1E9)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func runServer() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func runTdengine() {
defer db1.Close()
defer db2.Close()
CreateTable(db1, tableName)
InsertData(db2, tableName)
}
func main() {
//runServer()
Init()
runTdengine()
}