Skip to content

Instantly share code, notes, and snippets.

@devansh42
Created September 6, 2018 15:40
Show Gist options
  • Save devansh42/b0e9e25cc2a2ffed02f3329e968cf80a to your computer and use it in GitHub Desktop.
Save devansh42/b0e9e25cc2a2ffed02f3329e968cf80a to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
"strings"
_ "github.com/go-sql-driver/mysql"
)
func main() {
var user, password string
fmt.Printf("\nEnter user name (skip for 'root' ) : ")
fmt.Scanf("%s", &user)
if user == "" {
user = "root"
}
fmt.Printf("\nEnter password : ")
fmt.Scanf("%s", &password)
url := fmt.Sprint(user, ":", password, "@/information_schema")
doStuff(url)
}
func doStuff(url string) {
db, _ := sql.Open("mysql", url)
defer db.Close()
stmt, _ := db.Prepare("SELECT COLUMN_NAME FROM COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME=?")
defer stmt.Close()
fmt.Print("Enter Table Name ( e.g. mydb.mytable) : ")
var name string
fmt.Scan(&name)
var names = strings.Split(name, ".")
rows, _ := stmt.Query(names[0], names[1])
defer rows.Close()
var cols, q []string
var col string
var i = 0
for rows.Next() {
rows.Scan(&col)
cols = append(cols, col)
q = append(q, "?")
i = i + 1
}
fmt.Println(fmt.Sprint("INSERT INTO ", names[0], ".", names[1], "(", strings.Join(cols, ","), ")VALUES(", strings.Join(q, ","), ")"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment