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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| package main
import ( "database/sql" "dm" "fmt" "io/ioutil" "time" ) var db *sql.DB var err error func main() { driverName := "dm" dataSourceName := "dm://SYSDBA:SYSDBA@localhost:5236" if db, err = connect(driverName, dataSourceName); err != nil { fmt.Println(err) return } if err = insertTable(); err != nil { fmt.Println(err) return } if err = updateTable(); err != nil { fmt.Println(err) return } if err = queryTable(); err != nil { fmt.Println(err) return } if err = deleteTable(); err != nil { fmt.Println(err) return } if err = disconnect(); err != nil { fmt.Println(err) return } }
func connect(driverName string, dataSourceName string) (*sql.DB, error) { var db *sql.DB var err error if db, err = sql.Open(driverName, dataSourceName); err != nil { return nil, err } if err = db.Ping(); err != nil { return nil, err } fmt.Printf("connect to \"%s\" succeed.\n", dataSourceName) return db, nil }
func insertTable() error { var inFileName = "D:\\java\\vagrant\\data\\gopath\\src\\demo\\2022\\20221020\\data\\1.png" var sql = `INSERT INTO production.product(name,author,publisher,publishtime, product_subcategoryid,productno,satetystocklevel,originalprice,nowprice,discount, description,photo,type,papertotal,wordtotal,sellstarttime,sellendtime) VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17);` data, err := ioutil.ReadFile(inFileName) if err != nil { return err } t1, _ := time.Parse("2006-Jan-02", "2005-Apr-01") t2, _ := time.Parse("2006-Jan-02", "2006-Mar-20") t3, _ := time.Parse("2006-Jan-02", "1900-Jan-01") _, err = db.Exec(sql, "三国演义", "罗贯中", "中华书局", t1, 4, "9787101046126", 10, 19.0000, 15.2000, 8.0, "《三国演义》是中国第一部长篇章回体小说,中国小说由短篇发展至长篇的原因与说书有关。", data, "25", 943, 93000, t2, t3) if err != nil { return err } fmt.Println("insertTable succeed") return nil }
func updateTable() error { var sql = "UPDATE production.product SET name = :name WHERE productid = 11;" if _, err := db.Exec(sql, "三国演义(上)"); err != nil { return err } fmt.Println("updateTable succeed") return nil }
func queryTable() error { var productid int var name string var author string var description dm.DmClob var photo dm.DmBlob var sql = "SELECT productid,name,author,description,photo FROM production.product WHERE productid=11" rows, err := db.Query(sql) if err != nil { return err } defer rows.Close() fmt.Println("queryTable results:") for rows.Next() { if err = rows.Scan(&productid, &name, &author, &description, &photo); err != nil { return err } blobLen, _ := photo.GetLength() fmt.Printf("%v %v %v %v %v\n", productid, name, author, description, blobLen) } return nil }
func deleteTable() error { var sql = "DELETE FROM production.product WHERE productid = 12;" if _, err := db.Exec(sql); err != nil { return err } fmt.Println("deleteTable succeed") return nil }
func disconnect() error { if err := db.Close(); err != nil { fmt.Printf("db close failed: %s.\n", err) return err } fmt.Println("disconnect succeed") return nil }
|