当前位置:网站首页>Mysql database learning
Mysql database learning
2022-07-02 04:30:00 【L 00】
MySQL Database learning
The first is the installation of this package , Anyway, with gin The installation of the frame is a little similar , Because it won't be as troublesome as last time , As a result, it took a long time , But with the last experience , It's not enough to explode .
Understand with code :
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
connStr := "root:[email protected](127.0.0.1:3306)/ginsql"
db, err := sql.Open("mysql", connStr)
if err != nil {
log.Fatal(err.Error())
return
}
// Create database
_, err = db.Exec("create table person(" +
"id int auto_increment primary key," +
"name varchar(15) not null ," +
"age int default 1" +
");")
// success err by nil
if err != nil {
log.Fatal(err.Error())
return
}
// insert data
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "Alex", 15)
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "David", 15)
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "Tom", 17)
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "jack", 18)
if err != nil {
log.Fatal(err.Error())
return
} else {
fmt.Println(" Insert data succeeded ")
}
// Query data
row, err := db.Query("select id, name, age from person")
if err != nil {
log.Fatal(err.Error())
return
}
for {
if row.Next() {
person := new(person)
err := row.Scan(&person.Id, &person.Name, &person.Age)
if err != nil {
log.Fatal(err.Error())
return
}
fmt.Println(person.Id, person.Name, person.Age)
} else {
break
}
}
}
type person struct {
Id int
Name string
Age int
}
In fact, it is the code above the video
understand :
Create database code :
connStr := "root:[email protected](127.0.0.1:3306)/ginsql"
db, err := sql.Open("mysql", connStr)
if err != nil {
log.Fatal(err.Error())
return
}
// Create database
_, err = db.Exec("create table person(" +
"id int auto_increment primary key," +
"name varchar(15) not null ," +
"age int default 1" +
");")
// success err by nil
if err != nil {
log.Fatal(err.Error())
return
}
This part needs to be in MySQL Create relevant databases in the Library , front connStr := "root:[email protected](127.0.0.1:3306)/ginsql" In this part, we should pay attention to @tcp() It's fixed operation , At the same time, pay attention to 3306, I just wrote it wrong as 3360, Wasted time , Back ginsql For the relevant database . meanwhile , If you succeed in establishing , Next time, you can't create a library with the same name , Otherwise, it will report a mistake .
// insert data
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "Alex", 15)
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "David", 15)
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "Tom", 17)
_, err = db.Exec("insert into person(name,age)"+"value(?,?);", "jack", 18)
This part , I repeated it several times , It is found that the results are superimposed .
Query data :
// Query data
row, err := db.Query("select id, name, age from person")
if err != nil {
log.Fatal(err.Error())
return
}
for {
if row.Next() {
person := new(person)
err := row.Scan(&person.Id, &person.Name, &person.Age)
if err != nil {
log.Fatal(err.Error())
return
}
fmt.Println(person.Id, person.Name, person.Age)
} else {
break
}
}
}
Mainly row.Next() Code , It will run from the top of the table , If there is data behind it, it is true , If it is not false , At the end of each execution , Will point to the next line .
result :
边栏推荐
- Websites that it people often visit
- 藍湖的安裝及使用
- MySQL advanced SQL statement 2
- [improvement class] st table to solve the interval maximum value problem [2]
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
- Wechat applet map annotation
- 如何解决在editor模式下 无法删除物体的问题
- Research on the security of ognl and El expressions and memory horse
- 【提高课】ST表解决区间最值问题【2】
猜你喜欢
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
The confusion I encountered when learning stm32
[JS event -- event flow]
How much is the tuition fee of SCM training class? How long is the study time?
Pytorch---使用Pytorch进行鸟类的预测
Typescript practice for SAP ui5
FAQ | FAQ for building applications for large screen devices
Mysql表insert中文变?号的问题解决办法
【c语言】基础篇学习笔记
Common sense of cloud server security settings
随机推荐
Let正版短信测压开源源码
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Shutdown procedure after 60
Pytoch yolov5 runs bug solution from 0:
Target free or target specific: a simple and effective zero sample position detection comparative learning method
Social media search engine optimization and its importance
Actual combat | use composite material 3 in application
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Playing with concurrency: what are the ways of communication between threads?
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
云服务器的安全设置常识
Realizing deep learning framework from zero -- Introduction to neural network
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
Fluent icon demo
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
Play with concurrency: draw a thread state transition diagram
I sorted out some basic questions about opencv AI kit.
cs架构下抓包的几种方法
Why can't you remember when reading? Why can't you remember- My technology learning methodology