package
0.0.0-20231105232618-59b81d9e7ef3
Repository: https://github.com/scott-x/gutils.git
Documentation: pkg.go.dev

# README

package db

API

  • func GetConnection(config *model.DBConfig) (*sql.DB, error) : get a database connection

model

type DBConfig struct {
	Driver   string
	Username string
	Password string
	Host     string
	Port     string
	Database string
}

example

package main

import (
	"fmt"
	"github.com/scott-x/gutils/db"
	"github.com/scott-x/gutils/model"
)

func main() {
	//config
	config := &model.DBConfig{}
	//get connection, we'd better set it as a global value if it was used constantly.
	con, err := db.GetConnection(config)
	if err != nil {
		return
	}
	//define sql
	sql := "select username from user"

	//prepare
	stmt, err := con.Prepare(sql)
	if err != nil {
		panic(err)
	}
	//query
	row, err := stmt.Query()
	if err != nil {
		panic(err)
	}
	for row.Next() {
		var username string
		row.Scan(&username)
		// handle result
		fmt.Println(username)
	}
}

# Functions

No description provided by the author