go get -u -v github.com/go-sql-driver/mysql
func Open(driverName, dataSourceName string) (*DB, error)
CREATE DATABASE goTest;
use goTest;
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) DEFAULT '',
`age` INT(11) DEFAULT '0',
PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
INSERT INTO `goTest`.`user` (`name`, `age`) VALUES ('IvesShe', '18');
INSERT INTO `goTest`.`user` (`name`, `age`) VALUES ('Jack', '30');
INSERT INTO `goTest`.`user` (`name`, `age`) VALUES ('ChiChi', '18');
INSERT INTO `goTest`.`user` (`name`, `age`) VALUES ('Alex', '25');
select * from user;
select id,name,age from user where id=1
select id,name,age from user where id=3
select name,age from user where id=4;
- 可以預先讓數據庫編譯,增加效能
- 防止sql注入
- 全部成功提交,才會更新數據庫
- 只要其中出現失敗,即會回滾
程式放置 ./sqlx/ 資料夾下
安裝
go get -u -v github.com/jmoiron/sqlx
輸入以下字符串都可以引發SQL注入的問題
"xxx' or 1=1#"
"xxx' union select * from user #"
"xxx' and (select count(*) from user) <10 #"