zoukankan      html  css  js  c++  java
  • go access database demo


    package main import (
    "database/sql" "fmt" _ "github.com/lib/pq" "pricetable" "strconv" "strings" ) const ( FILEPATH = "D:/test/pricetable/" DB_USER = "xxxxtalog" DB_PASSWORD = "xxxx27yeA" DB_NAME = "xxxx7" DB_HOST = "xxxxx.wwwww.com" DB_PORT = "5433" ) func main() { // for callForavx6a() } func callForavx6a() { //Start fmt.Println("# Execute Start !") //get model //ts := pricetable.GetTable_my_mec_ch_avx6a_prices() ts := pricetable.GetTable_my_pmec_ch_avx6a_prices() //check file make sure file is new pricetable.CheckfileExistAndDelete(FILEPATH, ts.TableName+".sql") /* //get db connection dbinfo := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable", DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME) db, err := sql.Open("postgres", dbinfo) */ //get db connection dbinfo := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable", DB_USER_N, DB_PASSWORD_N, DB_HOST_N, DB_PORT_N, DB_NAME_N) db, err := sql.Open("postgres", dbinfo) pricetable.CheckErr(err) defer db.Close() queryAvx6a(ts, db) //Print Execute finished info fmt.Println("# Execute End !") } func queryAvx6a(ts *pricetable.TableStruct, db *sql.DB) { tbname := ts.TableName tbcolums := ts.TableColums fmt.Println("# Querying") //fmt.Println(len(tablecolums)) for i, v := range tbcolums { //test sentence if false { fmt.Println(i, v) } //combin query sql //qstr := "SELECT "model","" + v + "" FROM my_" + strings.TrimLeft(tbname, "my_") qstr := "SELECT "model","" + v + "" FROM " + tbname fmt.Println(qstr) rows, err := db.Query(qstr) pricetable.CheckErr(err) for rows.Next() { var Model_id string var val string err = rows.Scan(&Model_id, &val) pricetable.CheckErr(err) if val == "N/A" { continue } var vv string f, err := strconv.ParseFloat(val, 2) if err == nil { if v == "CE21" { vv = val } else { //do float round vv = strconv.FormatFloat(pricetable.MyRound(f, 0), 'f', 0, 32) } } else { vv = val } //select * from price_constants //filter fields vv = pricetable.FilterString(vv) //("pid", "unit_name", "model", "refrigerant", "price_option", "y2013", "guid") var pixstr string = "insert into " + strings.TrimLeft(tbname, "my_") + " ("pid", "model","price_option", "y2013") VALUES (" contents := pixstr + "(select "max"(pid) from " + strings.TrimLeft(tbname, "my_") + ")" + " ,'" + strings.Trim(Model_id, " ") + "','" + strings.Trim(v, " ") + "','" + vv + "' ); " //test print to console //fmt.Println(contents) //writer to file pricetable.Writertofile(contents, tbname+".sql", FILEPATH) } } } //======================================END==============

    //pricetable

    package pricetable
    
    type TableStruct struct {
        TableName   string
        TableColums []string
    }
    
    func GetTablea() *TableStruct {
        ts := new(TableStruct)
        ts.TableName = "Tablea"
        ts.TableColums = []string{"acolumA", "acolumB"}
        return ts
    }
    
    func GetTableb() *TableStruct {
        ts := new(TableStruct)
        ts.TableName = "Tableb"
        ts.TableColums = []string{"bcolumA", "bcolumB"}
        return ts
    }
    
    func GetTable_my_dbair2() *TableStruct {
        ts := new(TableStruct)
        ts.TableName = "my_dbair2"
        ts.TableColums = []string{
            "Basic 380-415/3ph/50Hz  (AU )",
            "Basic 460V/3ph/60Hz (Non-UL) (AR )",
            "Drain Pan (Stainless Steel)",
            "Discharge Plenum (Upflow only)",
            "Steam Generating Humidifier",
            "Hot Gas Bypass DX System Only",
            "Liquid Line Solenoid Valve -DX Systems Only (50Hz)",
            "Liquid Line Solenoid Valve -DX Systems Only (60Hz)",
            "Panel Insulation (Insulflex)",
            "Panel Insulation (Double Skin)",
            "SKD (Complete built up unit, piping joints to be brazed by othe",
            "Variable Frequency Drive (VFD) for Evaporator Blower Motor",
            "UnderFloor Water Detector (PinHole Probe)",
            "Smoke Detector",
            "EEV (50Hz/60Hz)",
            "BMS",
            "Low Ambient Kit to 7°C/45F (at design ambient 95F)",
            "Low Ambient Kit to 0°C/32F (at design ambient 95F)",
            "Low Ambient Kit to -10°C/14F (at design ambient 95F)",
            "Low Ambient Kit to 7°C/45F (at design ambient 105F)",
            "Low Ambient Kit to 0°C/32F (at design ambient 105F)",
            "Low Ambient Kit to -10°C/14F (at design ambient 105F)",
            "Coil Fin - Hydrophilic",
            "Coil Fin - Copper",
            "Black Color Epoxy Powder Coating (In lieu of Std Beige Color)  ",
        }
    
        return ts
    }
    
    //====================for ACPSB==================
    func Gettable_my_6acpsb_p() *TableStruct {
        ts := new(TableStruct)
        ts.TableName = "my_6ACPSB_P"
        ts.TableColums = []string{
            //"unit_name",
            //"model",
            "basic-R22",
            "basic-R407C",
            "INS0.5",
            "INS1",
            "DWAL",
            "LAC1",
            "SV",
            "HGBP",
            "HWC",
            "DOL1",
            "DOL2",
            "MII",
            "CTLR",
            "ADS-C",
            "ADS-E",
            "HYD-E",
            "HYD-C",
            "SPG",
            "BotR/S",
            "LIGHT",
            "24VAC",
            "SSB",
            "FR",
        }
    
        return ts
    }
    }
    package pricetable
    
    import (
        "fmt"
        "math"
        "os"
        "strings"
    )
    
    //func ContainMap(target string, ranges map[string]string) bool {
    //    var flag bool = false
    //    for k, _ := range ranges {
    //        if k == target {
    //            flag = true
    //        }
    //    }
    //    return flag
    //}
    
    func ContainArray(target string, ranges []string) bool {
        var flag bool = false
        for _, v := range ranges {
            if v == target {
                flag = true
            }
        }
        return flag
    }
    func FilterString(feild string) string {
    
        if strings.Index(feild, "CONSULT xxxxx") != -1 {
            feild = "-88888"
        }
        if strings.Index(feild, "Contact factory") != -1 {
            feild = "-88888"
        }
        if strings.Index(feild, "contact xxxx") != -1 {
            feild = "-88888"
        }
    
        if strings.Index(feild, "Consult factory") != -1 {
            feild = "-88888"
        }
        if strings.Index(feild, "Consult xxx") != -1 {
            feild = "-33333"
        }
        if strings.ToUpper(feild) == "STD" {
            feild = "-66666"
        }
        if strings.ToUpper(feild) == "N/A" {
            feild = "-77777"
        }
    
        return feild
    }
    
    func Exist(filename string) bool {
        _, err := os.Stat(filename)
        return err == nil || os.IsExist(err)
    }
    
    func CheckfileExistAndDelete(path string, filename string) {
    
        if Exist(path + filename) {
    
            errs := os.Remove(path + filename)
            if errs == nil {
                fmt.Println("delete old file :" + path + filename + " success !")
            } else {
                fmt.Printf("Error: %s
    ", errs)
            }
        } else {
            fmt.Println("file:" + path + filename + " not exist,not need to delete !")
        }
    }
    
    func Writertofile(content string, filename string, path string) {
    
        fil, err := os.OpenFile(path+filename, os.O_RDWR|os.O_APPEND, 0420)
        //fmt.Println(path + filename)
        if err != nil {
            //fmt.Printf("Error: %s
    ", err)
            filnew, errnew := os.Create(filename)
            if errnew != nil {
                fmt.Printf("Error: %s
    ", errnew)
                filnew.Close()
                return
            } else {
                fmt.Println(path + filename + " Create success !")
                fil = filnew
            }
    
        }
        defer fil.Close()
        fil.WriteString(content)
    }
    
    func CheckErr(err error) {
        if err != nil {
            panic(err)
        }
    }
    
    func MyRound(f float64, n int) float64 {
        pow10_n := math.Pow10(n)
        return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
    }
    
    //func strings.TrimLeft(" !!! Achtung !!! ", "! ")
    func Substr(str string, start, length int) string {
        rs := []rune(str)
        rl := len(rs)
        end := 0
    
        if start < 0 {
            start = rl - 1 + start
        }
        end = start + length
    
        if start > end {
            start, end = end, start
        }
    
        if start < 0 {
            start = 0
        }
        if start > rl {
            start = rl
        }
        if end < 0 {
            end = 0
        }
        if end > rl {
            end = rl
        }
    
        return string(rs[start:end])
    }
  • 相关阅读:
    bootstrap学习9-路径分页和徽章组件
    bootstrap学习8-输入框和导航栏组件
    bootstrap学习7-图标菜单和按钮组件
    bootstrap学习6-辅助类和响应工具
    bootstrap学习5-栅格系统
    bootstrap学习4-表单和图片
    bootstrap学习3-表格和按钮组件
    bootstrap学习笔记2
    相等操作符
    读《JavaScript dom编程艺术(第2版)》笔记 1-2
  • 原文地址:https://www.cnblogs.com/rojas/p/5416988.html
Copyright © 2011-2022 走看看