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.5k Golang : Detect sample rate, channels or latency with PortAudio
+12.5k Golang : Forwarding a local port to a remote server example
+8.7k Golang : Find duplicate files with filepath.Walk
+36k Golang : Get file last modified date and time
+4.9k JQuery : Calling a function inside Jquery(document) block
+7.5k Golang : Rot13 and Rot5 algorithms example
+5.3k Javascript : Change page title to get viewer attention
+19.9k Golang : Count JSON objects and convert to slice/array
+12.8k Swift : Convert (cast) Int or int32 value to CGFloat
+6.1k Golang : Debug with Godebug
+13.6k Golang : Query string with space symbol %20 in between