当前位置:网站首页>Go SQL parsing time Time type
Go SQL parsing time Time type
2022-06-26 04:07:00 【Believe in the reason and follow the reason】
Code :
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"time"
)
var db *sqlx.DB // Connect pool objects
/* initialization mysql dsn The format is username:[email protected](ip:port)/database maxOpenConns int // Set the maximum number of connections in the connection pool maxIdleConns int // Set the maximum number of idle */
type person struct {
Name string
Age int32
Create_time time.Time
}
func main() {
// Connect to database
var err error
db, err = sqlx.Connect("mysql", "root:[email protected](127.0.0.1:3306)/person")
if err != nil {
fmt.Println("[fail] sqlx.Connect failed,err:",err)
return
}
db.SetMaxOpenConns(1024) // Set the maximum number of connections in the connection pool
db.SetMaxIdleConns(512) // Set the maximum number of idle
fmt.Println("[success] Init mysql successfully")
if db == nil {
panic("db is nil,please init")
}
sqlStr := `select * from student where name = ?`
var s person
err = db.Get(&s,sqlStr," ask ")
if err != nil {
fmt.Println("[fail] db.Get() failed,err:",err)
return
}
fmt.Println("[success] getOneStudent successfully,p:",s)
}
sql analysis time Type error :
[success] Init mysql successfully
[fail] db.Get() failed,err: sql: Scan error on column index 2, name "create_time": unsupported Scan, storing driver.Value type []uint8 into type *time.T
ime
solve : In connection sql Pass in the parameter parseTime=true
db, err = sqlx.Connect("mysql", "root:[email protected](127.0.0.1:3306)/person?parseTime=true")
Running results :
[success] Init mysql successfully
[success] getOneStudent successfully,p: {
ask 0 0001-01-01 00:00:00 +0000 UTC}
When inserted time Default to error :
// Insert
sqlStr = `insert into student(name,age,create_time) values(?,?,?)`
var p person
_,err = db.Exec(sqlStr, p.Name, p.Age, p.Create_time)
if err != nil {
fmt.Println("[fail] db.Exec() failed,err:",err)
return
}
fmt.Println("[success] db insert successfully")
[fail] db.Exec() failed,err: Error 1292: Incorrect datetime value: '0000-00-00' for column 'create_time' at row 1
You can format the time and insert it
_,err = db.Exec(sqlStr, p.Name, p.Age, p.Create_time.Format("2006-01-02 15:04:05"))
边栏推荐
- The stc-isp burning program for 51 single chip microcomputer always shows that "the target single chip microcomputer is being detected..." the cold start board does not respond
- 2022.6.24-----leetcode. five hundred and fifteen
- 判断两个集合的相同值 ||不同值
- Knowledge of SQL - database design, backup and restore
- Spark - understand parquet
- 外包干了四年,人直接废了。。。
- 1.基础关
- Introduction Guide to the flutterplugin plug-in in the actual combat of flutter
- How much do you make by writing a technical book? To tell you the truth, 2million is easy!
- Spark - 一文搞懂 parquet
猜你喜欢
![[Flink] Flink batch mode map side data aggregation normalizedkeysorter](/img/c7/611c7207730555b510505d4a8efe3d.png)
[Flink] Flink batch mode map side data aggregation normalizedkeysorter

Quanergy欢迎Lori Sundberg出任首席人力资源官

开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP

Oracle technology sharing Oracle 19.14 upgrade 19.15

线程同步之读写锁

How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup

Machine learning notes - trend components of time series

商城风格也可以很多变,DIY 了解一下

YOLOv5改进:更换骨干网(Backbone)

【QT】资源文件导入
随机推荐
Sorting out the examination sites of the 13th Blue Bridge Cup single chip microcomputer objective questions
Oracle技术分享 oracle 19.14升级19.15
How much do you make by writing a technical book? To tell you the truth, 2million is easy!
Sorting out the knowledge points of the renderview renderobject parentdata of the shuttle
[QT] dialog box
【QT】对话框dialog
I/o virtualization technology - UIO framework
Which is the best embedded visual programming software? (introduction, evaluation and selection of visual programming platform) [scratch, mind+, mixly]
But the Internet began to have a new evolution and began to appear in a new state
816. fuzzy coordinates
go语言泛型在IDE中语法报错
力扣79单词搜索
High performance computing center roce overview
How do wechat applets delay? Timing? Execute a piece of code after? (kengji)
Machine learning notes - trend components of time series
Conditional variables for thread synchronization
Daily tests
asp.net网页选择身份进行登录的简单代码,asp连接数据库,使用asp:Panel、asp:DropDownList控件
Analysis of the principle of obxwidget
mysql自帶的性能測試工具mysqlslap執行壓力測試