forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.6 KiB
110 lines
2.6 KiB
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var Creator string = "zhangxw" //作者
|
|
var Edition string = "1.0.0" //版本号
|
|
|
|
//结构体
|
|
type Job struct {
|
|
db *sql.DB
|
|
ch chan int
|
|
total int
|
|
n int
|
|
}
|
|
|
|
/**
|
|
需求:
|
|
读取SQLite相关表,匹配查询对应MySQL是否存在相同表名和字段(数量、名称和顺序一致)
|
|
*/
|
|
func main() {
|
|
tables := [...]string{"A01", "A01SEARCHTEMP", "A02", "A05", "A06", "A08", "A11", "A14", "A15", "A36", "A57", "B01", "CODE_VALUE", "CODE_TYPE"}
|
|
for _, table := range tables {
|
|
fmt.Println(table)
|
|
}
|
|
|
|
db, err := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8")
|
|
checkErr(err)
|
|
QueryMySQLInfoByTableToInsertSQL(db, "SELECT * FROM a01")
|
|
|
|
}
|
|
|
|
func QueryMySQLInfoByTableToInsertSQL(db *sql.DB, sql string) []string {
|
|
rows, err := db.Query(sql)
|
|
checkErr(err)
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
d, _ := rows.ColumnTypes()
|
|
led := len(d)
|
|
for i := 0; i < led; i++ {
|
|
fmt.Println(d[i].DatabaseTypeName())
|
|
fmt.Println(d[i].Name())
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 获取表数据
|
|
func QueryMap(db *sql.DB, sqlStr string, m map[string]string) {
|
|
rows, _ := db.Query(sqlStr)
|
|
defer rows.Close()
|
|
columns, _ := rows.Columns()
|
|
for rows.Next() {
|
|
_ = rows.Scan(&columns[0], &columns[1])
|
|
m[columns[0]] = columns[1]
|
|
}
|
|
}
|
|
|
|
func run() {
|
|
args := [...]string{"run", "sqlite3", "E:/Demo.db", "mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8", "SELECT a1, a2 FROM a ", "insert into a(a1,a2) values (?, ?)"}
|
|
// args := os.Args
|
|
fmt.Println("测试设置读取SQLite数据插入到指定MySQL数据库中。",
|
|
"参数为:", args[1], "=>", args[3])
|
|
|
|
db, err := sql.Open(args[1], args[2])
|
|
checkErr(err)
|
|
|
|
rows, err := db.Query(args[5])
|
|
checkErr(err)
|
|
|
|
dbMySQL, err := sql.Open(args[3], args[4])
|
|
if err != nil {
|
|
fmt.Println("访问数据库出错", err)
|
|
return
|
|
}
|
|
defer dbMySQL.Close()
|
|
dbMySQL.SetConnMaxLifetime(time.Second * 500) // 设置连接超时500秒
|
|
dbMySQL.SetMaxOpenConns(100) // 设置最大连接数
|
|
|
|
fmt.Println("====start=====")
|
|
start := time.Now()
|
|
i := 0
|
|
s, _ := rows.Columns()
|
|
len := len(s)
|
|
for ; i < len; i++ {
|
|
fmt.Print(rows)
|
|
}
|
|
_, err = dbMySQL.Exec(args[6])
|
|
checkErr(err)
|
|
|
|
end := time.Now()
|
|
curr := end.Sub(start)
|
|
fmt.Println("run time:", curr)
|
|
|
|
// if len(args) == 1 {// ------------------------------------------------------------ 判断参数个数类别//
|
|
// }else { // ----------------------------------------------------------------------- 判断参数个数类别
|
|
// }
|
|
}
|
|
|
|
func checkErr(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|