Created
February 5, 2018 08:31
-
-
Save sinmetal/2e65dcbb5de05c34657fe04d730dfbfd to your computer and use it in GitHub Desktop.
Cloud SQL Proxy Library for Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql" | |
"github.com/google/uuid" | |
) | |
const instanceConnectionName = "sinmetal-tokyo:asia-northeast1:sql1" | |
const databaseUser = "root" | |
const databaseName = "db1" | |
const password = "password" | |
func main() { | |
for { | |
insert() | |
} | |
} | |
func insert() { | |
cfg := mysql.Cfg(instanceConnectionName, databaseUser, password) | |
cfg.DBName = databaseName | |
db, err := mysql.DialCfg(cfg) | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer db.Close() | |
stmtIns, err := db.Prepare("INSERT INTO sample (uuid, client_time, db_time) VALUES (?, ?, now())") | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer stmtIns.Close() | |
id := uuid.New().String() | |
result, err := stmtIns.Exec(id, time.Now()) | |
if err != nil { | |
panic(err.Error()) | |
} | |
insertId, err := result.LastInsertId() | |
if err != nil { | |
panic(err.Error()) | |
} | |
affected, err := result.RowsAffected() | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf("{\"LastInsertId\":\"%d\", \"RowsAffected\":%d}\n", insertId, affected) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment