go之golang中用于连接到mysql的无效字符

emanlee 阅读:225 2025-06-02 22:19:02 评论:0

我是使用golang的新手,并尝试连接到mySql
这是我的源代码。当我运行此示例代码时,出现错误。错误详细信息在源代码下方

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, type, status, created_at, updated_at  string 
        qty, pegged_distance, price_limit                                      double 
} 
var db *sql.DB 
var err error 
 
funct 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) 
} 
我有这样的错误
# 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 
如何解决这个问题?

请您参考如下方法:

这样做,代码对它们有注释,您可以阅读并适应您的需求。

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) 
} 


标签:mysql
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号