1.mysql配置
CREATE TABLE shop ( article INT(4) UNSIGNED ZEROFILL DEFAULT ‘0000‘ NOT NULL, dealer CHAR(20) DEFAULT ‘‘ NOT NULL, price DOUBLE(16,2) DEFAULT ‘0.00‘ NOT NULL, PRIMARY KEY(article, dealer)); INSERT INTO shop VALUES (1,‘A‘,3.45),(1,‘B‘,3.99),(2,‘A‘,10.99),(3,‘B‘,1.45),(3,‘C‘,1.69), (3,‘D‘,1.25),(4,‘D‘,19.95); 好了,例子数据是这样的: SELECT * FROM shop +---------+--------+-------+ | article | dealer | price | +---------+--------+-------+ | 0001 | A | 3.45 | | 0001 | B | 3.99 | | 0002 | A | 10.99 | | 0003 | B | 1.45 | | 0003 | C | 1.69 | | 0003 | D | 1.25 | | 0004 | D | 19.95 | +---------+--------+-------+
2.golang接口
package main import( "fmt" "github.com/ziutek/mymysql/mysql" _ "github.com/ziutek/mymysql/native" ) func main() { db := mysql.New("tcp", "", "127.0.0.1:3306","root","123456","aaa") err := db.Connect() if err != nil{ fmt.Println(err); //连接失败 } rows, res, err := db.Query("select * from shop") if err == nil{ fmt.Println(rows[0].Str(0)) //字符串显示第一个字段内容 name := res.Map("price") //查询字段名返回序号 fmt.Println(rows[0].Str(name)) } }
时间: 2024-10-08 10:17:52