第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

golang中用于連接mysql的無(wú)效字符

golang中用于連接mysql的無(wú)效字符

Go
動(dòng)漫人物 2022-06-21 16:14:52
我是使用 golang 的新手并嘗試連接到 mySql這是我的源代碼。當(dāng)我運(yùn)行這個(gè)示例代碼時(shí),我得到了錯(cuò)誤。錯(cuò)誤詳細(xì)信息在源代碼下方package mainimport "database/sql"import "fmt"import _"github.com/go-sql-driver/mysql"type trade_history struct {        id, final_meta_report_id, trading_account_id, lp, lp2, lp3             int        symbol, price, price_type, time, type, status, created_at, updated_at  string        qty, pegged_distance, price_limit                                      double}var db *sql.DBvar err errorfunct getTradingHistory (final_Meta_report_ID int) (err error){        db, err = sql.Open("mysql", username:password@trade.asdewx134.us-east-2.rds.amazonaws.com:3306/trading_dashboard        defer db.Close()        if err != nil {                fmt.Println(err.Error())        }        err = db.Ping()        if err != nil{        fmt.Println(err.Error())        }        var p trade_history        err = db.QueryRow("select id, final_meta_report_id, trading_account_id, symbol, qty, price, price_type, time, lp, lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at, u$        if err !=nil {        fmt.Println(err.Error())        }        fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n pr$        return err}func main(){        getTradingHistory(2074)}我遇到了這樣的錯(cuò)誤# command-line-arguments./myConnectionMySql.go:9:35: syntax error: unexpected type, expecting name./myConnectionMySql.go:15:1: syntax error: non-declaration statement outside function body./myConnectionMySql.go:16:56: invalid character U+0040 '@'./myConnectionMySql.go:26:2: syntax error: non-declaration statement outside function body./myConnectionMySql.go:30: newline in string如何解決這個(gè)問(wèn)題?
查看完整描述

2 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

這樣做,代碼對(duì)它們有注釋?zhuān)梢蚤喿x并適應(yīng)您的需求。


package main


import "database/sql"

import "fmt"

import _ "github.com/go-sql-driver/mysql"


type trade_history struct {

    // best practice in golang is to name your field in upper camel case if you intend to export it

    // or lower camel case if you don't.

    id, final_meta_report_id, trading_account_id, lp, lp2, lp3 int

    // `type` is reserved keyword, therefore for example, i use `ptype`

    symbol, price, price_type, time, ptype, status, created_at, updated_at string

    qty, pegged_distance, price_limit                                      double

}


// you don't need to declare global variable like this, if you want it to be accessible to all your packages

// it's best to use struct.

// var db *sql.DB

// var err error -- this one is shadowed by declaration of err in your getTradingHistory anyway.


func getTradingHistory(reportId int) (err error) {

    // Your dsn is unquoted hence error

    db, err := sql.Open("mysql", "username:password@trade.asdewx134.us-east-2.rds.amazonaws.com:3306/trading_dashboard")

    // your trade data is here


    defer db.Close()

    if err != nil {

        fmt.Println(err.Error())

    }

    err = db.Ping()

    if err != nil {

        fmt.Println(err.Error())

    }

    var th trade_history

    // this one is also unquoted and your SQL statement is not correct

    // here i add `FROM [YOUR TABLE] that you can edit to your needs`

    err = db.QueryRow("select id, final_meta_report_id, trading_account_id, symbol, qty, price, price_type, time, lp, lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at FROM [YOUR TABLE] WHERE id = ?", reportId).Scan( /* Scan your fields here */ ) // This is where you should scan your fields.

    if err != nil {

        fmt.Println(err.Error())

    }

    fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n pr$")

    return err

}


func main() {

    getTradingHistory(2074)

}



查看完整回答
反對(duì) 回復(fù) 2022-06-21
?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

您應(yīng)該修復(fù)錯(cuò)誤,查看代碼注釋如下:


package main


import "database/sql"

import "fmt"

import _ "github.com/go-sql-driver/mysql"


type trade_history struct {

    id, final_meta_report_id, trading_account_id, lp, lp2, lp3 int

    symbol, price, price_type, time, typeField, status, created_at, updated_at string

    qty, pegged_distance, price_limit                                          double

}


var db *sql.DB


var err error



func getTradingHistory(final_Meta_report_ID int) (err error) {

    host := "trade.asdewx134.us-east-2.rds.amazonaws.com"

    port := 3306

    db := "trading_dashboard"


    //use Sprintf if you wanna parse a string with values

    conn := fmt.Sprintf("%s:%s@%s:%s/%s", username, password, host, port, db)

    db, err = sql.Open("mysql", conn)

    defer db.Close()

    if err != nil {

        fmt.Println(err.Error())

    }

    err = db.Ping()

    if err != nil {

        fmt.Println(err.Error())

    }

    var p trade_history


    //if you have multiple lines of texts use ` instead of "

    err = db.QueryRow(`select id, final_meta_report_id, 

    trading_account_id, symbol, qty, price, price_type, time, lp, 

    lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at`).Scan(&p)

    if err != nil {

        fmt.Println(err.Error())

    }



    fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n")


    return err

}


func main() {

    getTradingHistory(2074)

}


查看完整回答
反對(duì) 回復(fù) 2022-06-21
  • 2 回答
  • 0 關(guān)注
  • 299 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)