Go實現對MySQL的增刪改查
依賴
先下載go-sql-driver/mysql
:
go get -u github.com/go-sql-driver/mysql
資料庫
建表:
CREATE TABLE `data` ( `id` bigint(10) NOT NULL AUTO_INCREMENT, `key` varchar(1024) COLLATE utf8mb4_bin DEFAULT NULL, `value` varchar(1024) COLLATE utf8mb4_bin DEFAULT NULL, `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
實現程式碼:
實體物件
func (data Data) String() string { return fmt.Sprintf("id:%d key:%s value:%sn[createTime:%d updateTime:%d]n", data.Id, data.Key, data.Value, data.CreateTime.Unix(), data.UpdateTime.Unix()) } type Data struct { Id int64 Key string Value string CreateTime time.Time //[]uint8 UpdateTime time.Time //[]uint8}
獲得連線
db, err := sql.Open("mysql", "dubby:123456@tcp(127.0.0.1:3306)/go_test?parseTime=true")if err != nil { panic(err.Error()) }
刪除
func Delete(id int64, db *sql.DB) error { stmtOut, err := db.Prepare("DELETE FROM `data` WHERE id = ?") if err != nil { panic(err.Error()) } defer stmtOut.Close() result, err := stmtOut.Exec(id) if err != nil { panic(err.Error()) } if rowNum, err := result.RowsAffected(); err != nil || rowNum != int64(1) { panic("delete error") } return nil}
新增
func Insert(key string, value string, db *sql.DB) *Data { stmtOut, err := db.Prepare("INSERT INTO `data` (`key`, `value`) values (?, ?)") if err != nil { panic(err.Error()) } defer stmtOut.Close() result, err := stmtOut.Exec(key, value) if err != nil { panic(err.Error()) } id, err := result.LastInsertId() if err != nil { panic(err.Error()) } return QueryById(id, db) }
更新
func UpdateById(id int64, key string, value string, db *sql.DB) *Data { stmtOut, err := db.Prepare("UPDATE `data` SET `key`=?, `value`=? WHERE `id`=?") if err != nil { panic(err.Error()) } defer stmtOut.Close() _, err = stmtOut.Exec(key, value, id) if err != nil { panic(err.Error()) } return QueryById(id, db) }
根據ID查詢
func QueryById(idRequest int64, db *sql.DB) *Data { stmtOut, err := db.Prepare("SELECT * FROM `data` WHERE id = ?") if err != nil { panic(err.Error()) } defer stmtOut.Close() rows := stmtOut.QueryRow(idRequest) data := new(Data) err = rows.Scan(&data.Id, &data.Key, &data.Value, &data.CreateTime, &data.UpdateTime) if err != nil { panic(err.Error()) } return data }
根據ID範圍查詢
func QueryByIdRange(minId int64, maxId int64, db *sql.DB) []*Data { stmtOut, err := db.Prepare("SELECT * FROM `data` WHERE id >= ? AND id <= ?") if err != nil { panic(err.Error()) } defer stmtOut.Close() rows, err := stmtOut.Query(minId, maxId) if err != nil { panic(err.Error()) } var result []*Data for rows.Next() { data := new(Data) err = rows.Scan(&data.Id, &data.Key, &data.Value, &data.CreateTime, &data.UpdateTime) if err != nil { panic(err.Error()) } result = append(result, data) } return result }
測試
fmt.Println("========QueryById========") data := QueryById(2, db) fmt.Println(*data) fmt.Println("========QueryByIdRange========") resultList := QueryByIdRange(1, 10, db)for i := 0; i < len(resultList); i++ { fmt.Println(*resultList[i]) } fmt.Println("========Insert========") data = Insert("go_key", "go_value", db) fmt.Println(*data) fmt.Println("========Delete========") err = Delete(data.Id, db) fmt.Println(err) fmt.Println("========UpdateById========") data = QueryById(2, db) prefix := fmt.Sprintf("update-%d-", time.Now().UnixNano()/1000%10000) data = UpdateById(data.Id, prefix+"dubby", prefix+"", db) fmt.Println(*data)
測試結果
========QueryById========id:2 key:update-5469-dubby value:update-5469- [createTime:1539340810 updateTime:1539345474] ========QueryByIdRange========id:2 key:update-5469-dubby value:update-5469- [createTime:1539340810 updateTime:1539345474]id:3 key:dubby value:dubby.cn [createTime:1539340810 updateTime:1539340810]id:4 key:test value:test [createTime:1539343652 updateTime:1539343652]id:5 key:test value:test [createTime:1539343760 updateTime:1539343760]id:6 key:go_key value:go_value [createTime:1539343942 updateTime:1539343942]id:7 key:go_key value:go_value [createTime:1539343956 updateTime:1539343956]id:8 key:go_key value:go_value [createTime:1539343977 updateTime:1539343977]id:9 key:go_key value:go_value [createTime:1539343981 updateTime:1539343981]id:10 key:go_key value:go_value [createTime:1539343982 updateTime:1539343982] ========Insert========id:35 key:go_key value:go_value [createTime:1539355099 updateTime:1539355099] ========Delete======== <nil> ========UpdateById========id:2 key:update-8738-dubby value:update-8738- [createTime:1539340810 updateTime:1539355099]
作者:我是楊正
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4560/viewspace-2815396/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mybatis實現MySQL資料庫的增刪改查MyBatisMySql資料庫
- mysql增刪改查MySql
- Node.js+Express+Mysql 實現增刪改查Node.jsExpressMySql
- Node+Vue實現對資料的增刪改查Vue
- JDBC連線mysql-8.0實現增刪改查JDBCMySql
- koa+mysql實現增刪改查-全棧之路MySql全棧
- js實現表格的增刪改查JS
- mysql基本增刪改查MySql
- Go微服務實踐之增刪改查Go微服務
- golang使用go-sql-driver實現mysql增刪改操作GolangMySql
- JSP實現servlet對資料庫的增刪查改操作JSServlet資料庫
- 使用express+mongoose對mongodb實現增刪改查操作ExpressMongoDB
- 運用layui實現增刪改查UI
- MySQL表的增刪查改(提高篇)MySql
- MySQL的基本語法(增,刪,改,查)MySql
- MySql 表資料的增、刪、改、查MySql
- MySQL表的增刪改查(基礎)MySql
- Java實現簡單的增刪改查操作Java
- jQuery實現購物車的增刪改查jQuery
- Go微服務框架go-kratos實戰03:使用 gorm 實現增刪改查操作Go微服務框架ORM
- MySQL基礎操作(增刪改查)MySql
- mysql資料增刪改查操作MySql
- 單連結串列實現增刪改查
- Node+Express+MySql實現簡單增刪改查和登入ExpressMySql
- MySQL表的增刪改查(進階)下MySql
- 增刪改查
- linux-MySQL基本指令-增刪改查LinuxMySql
- Mybatis-plus實現簡單增刪改查MyBatis
- Entity Framework使用DBContext實現增刪改查示例FrameworkContext
- 封裝模組實現商品增刪改查封裝
- Python Web實戰:Python+Django+MySQL實現基於Web版的增刪改查PythonWebDjangoMySql
- 列表的增刪改查
- 字典的增刪改查
- layui的增刪改查UI
- mybatis的增刪改查MyBatis
- redist的增刪改查Redis
- 寫一個簡單的Linkedlist,實現增刪改查
- 使用Mongoose類庫實現簡單的增刪改查Go