Skip to content

A golang package to use sqlx and make database management easier

Notifications You must be signed in to change notification settings

lonelymous/godab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic usage

I have a ServerConfig struct that contains the database config you can use your own struct or just pass the config directly

	db, err = godab.Connect(&godab.DatabaseConfig{
		Hostname:  "localhost",
		Port:      3306,
		Driver:    "mysql",
		Name:      "database",
		Username:  "root",
		Password:  "root",
		ParseTime: true,
        })
	if err != nil {
		log.Fatalln("error while setup database", err)
	}

If you want to setup a Database from 0 you can use the OpenAndCreate function

i have a database.sql file in the same directory as my main.go this file contains the sql code to create the database if the database already exists, it will be ignored

	config := &godab.DatabaseConfig{
		...
		DatabaseFilePath: "../database.sql"
	}

	// Setup database
	db, err = godab.OpenAndCreate(config, "../database.sql")
	if err != nil {
		log.Fatalln("error while setup database", err)
	}

After the connection is established, you can use the db variable to execute queries.

    
	// SQL

    rows, err := godab.Query("SELECT * FROM `table`")

    row, err := godab.QueryRow("SELECT * FROM `table` LIMIT 1")
    
	result, err := godab.Exec("USE `database`")

	// SQLX

	rows, err := godab.Queryx("SELECT * FROM `table`")

    row, err := godab.QueryRowx("SELECT * FROM `table` LIMIT 1")
    
	result, err := godab.Exec("USE `database`")

FETCH

	err := godab.FetchRowToAny[Result](row, &result)
	err := godab.FetchRowsToAny[Result](rows, &result)

	err := godab.FetchXRowToStruct[Result](row, &result)
	err := godab.FetchXRowsToStruct[Result](rows, &result)

About

A golang package to use sqlx and make database management easier

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages