Skip to content

Instantly share code, notes, and snippets.

@harryge00
Created May 27, 2018 14:02
Show Gist options
  • Save harryge00/d88f06194690b602e8b3acda4f0d48da to your computer and use it in GitHub Desktop.
Save harryge00/d88f06194690b602e8b3acda4f0d48da to your computer and use it in GitHub Desktop.
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Note: the example only works with the code within the same release/branch.
package main
import (
"fmt"
"os"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:654321@/net_manager")
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
defer db.Close()
// Execute the query
rows, err := db.Query("SELECT * FROM alloc_ip where user_id = " + os.Args[1])
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// Fetch rows
for rows.Next() {
var id int
var ip string
var user_id int
var occupied bool
var group sql.NullString
// get RawBytes from data
err = rows.Scan(&id, &ip, &user_id, &occupied, &group)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// Now do something with the data.
// Here we just print each column as a string.
fmt.Println(id, ip, user_id, occupied, group)
fmt.Println("-----------------------------------")
}
if err = rows.Err(); err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment