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
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+6.9k Golang : Fibonacci number generator examples
+12.3k Golang : List running EC2 instances and descriptions
+4.6k MariaDB/MySQL : How to get version information
+15.2k Golang : Save(pipe) HTTP response into a file
+30.8k Golang : Download file example
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+19.9k Golang : How to get time from unix nano example
+7.8k Golang : Scan files for certain pattern and rename part of the files