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
+9.1k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+33.6k Golang : How to check if slice or array is empty?
+16.8k Golang : Read integer from file into array
+17.8k Golang : Iterate linked list example
+16k Golang : Read large file with bufio.Scanner cause token too long error
+5.8k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+13.4k Golang : Get constant name from value
+6.8k Android Studio : Hello World example
+22.1k Golang : Match strings by wildcard patterns with filepath.Match() function
+9.7k Golang : Populate slice with sequential integers example
+16.4k Golang : Send email and SMTP configuration example
+11.6k Android Studio : Create custom icons for your application example