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)