当前位置:网站首页>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"))
边栏推荐
- Knowledge of functions
- Dix critères de base importants pour les essais de débogage de logiciels
- YOLOv5改进:更换骨干网(Backbone)
- MySQL est livré avec l'outil de test de performance MySQL lap pour effectuer des tests de résistance
- Quanergy欢迎Lori Sundberg出任首席人力资源官
- IEDA 突然找不到了compact middle packages
- Webrtc series - 6-connections tailoring for network transmission
- 使用Jsoup提取接口中的图片
- go time包:秒、毫秒、纳秒时间戳输出
- QPS的概念和实现
猜你喜欢

Custom parameter QR code picture combined with background picture to generate new picture node environment

1.基础关

Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules

Conditional variables for thread synchronization

用eclipse连mysql数据库出错然后出现图中的话是咋回事呀

【MySQL】 MySQL 导出数据库

The style of the mall can also change a lot. DIY can learn about it

Sorting out the examination sites of the 13th Blue Bridge Cup single chip microcomputer objective questions

Analysis of camera memory memory leakage (II)
![[MySQL] MySQL export database](/img/e3/1aa31760dc5447b7c3c0d942644116.jpg)
[MySQL] MySQL export database
随机推荐
Ten important basic principles of software debugging and testing
软件调试测试的十大重要基本准则
Part 4: drawing quadrilateral
How do wechat applets delay? Timing? Execute a piece of code after? (kengji)
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
線程同步之讀寫鎖
Analysis of camera memory memory leakage (II)
I/o virtualization technology - vfio
[从零开始学习FPGA编程-45]:视野篇 - 集成电路助力数字化时代高质量发展-2-市场预测
Lua语法讲解
Camera-CreateCaptureSession
VHDL design
mysql自带的性能测试工具mysqlslap执行压力测试
【QT】对话框dialog
MySQL's built-in performance testing tool, mysqlslap, performs stress testing
XML parsing bean tool class
高性能算力中心 — RoCE — Overview
【Flink】Flink 批处理模式Map端数据聚合 NormalizedKeySorter
[Flink] Flink source code analysis - creation of jobgraph in batch mode
WPF value conversion