Golang database/sql.DB.Prepare function example
package database/sql
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement.
Golang database/sql.DB.Prepare function usage example
func (db *Config) prebuild() (err error) {
queries := []string{
`CREATE TABLE IF NOT EXISTS rules (
id BIGINT NOT NULL AUTO_INCREMENT,
host VARCHAR(128) NOT NULL DEFAULT '',
json TEXT NOT NULL,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY (host)
)`,
}
for _, query := range queries {
if _, err = db.db.Exec(query); err != nil {
return
}
}
// Prepared statements for storage
if db.stmt.Check, err = db.db.Prepare(`SELECT COUNT(*) FROM rules WHERE updated > ?`); err != nil {
return
}
if db.stmt.Rules, err = db.db.Prepare(`SELECT host, json FROM rules`); err != nil {
return
}
return
}
Reference :
Advertisement
Something interesting
Tutorials
+25.8k Golang : Daemonizing a simple web server process example
+14.2k Golang : syscall.Socket example
+33.7k Golang : All update packages with go get command
+9.1k Golang : Intercept and compare HTTP response code example
+4.7k Fix Google Analytics Redundant Hostnames problem
+6.7k Golang : Check if password length meet the requirement
+8.9k Golang : Gaussian blur on image and camera video feed examples
+8.2k Golang : Get final or effective URL with Request.URL example
+7.2k Golang : Null and nil value
+52.6k Golang : How to get struct field and value by name
+4.9k Nginx and PageSpeed build from source CentOS example