Skip to content

Generic database package for Go (golang)

License

Notifications You must be signed in to change notification settings

fatihtatoglu/db-go

Repository files navigation

db-go

The Database Go Package is a collection of tools for working with databases in Go applications. It provides functionalities for managing database connections, executing commands, and handling rows and tables. With this package, developers can easily interact with databases in their Go projects, making database-related tasks more efficient and straightforward.

Install

go get github.com/fatihtatoglu/db-go

Usage

Creating a Database Connection

config := db.CreateNewDBConfig("mysql", "username", "password", "localhost", 3306, "database_name")
connection, err := db.CreateNewDBConnection(config)
if err != nil {
    // handle error
}

err = connection.Open()
if err != nil {
    // handle error
}

defer connection.Close()

Executing Commands

command, err := db.CreateNewDBCommand(connection)
if err != nil {
    // handle error
}

_, err = command.Execute("CREATE TABLE users (name VARCHAR(255), age INT)")
if err != nil {
    // handle error
}

Executing Commands with Parameters

result, err := db.Execute("INSERT INTO users (name, age) VALUES (?, ?)", "John Doe", 30)
 if err != nil {
  // handle error
 }
 fmt.Println("Rows affected:", result.RowsAffected)

Querying Data

query := "SELECT * FROM users"
result, err := command.Query(query)
if err != nil {
    // handle error
}

Querying Data with Parameters

query := "SELECT * FROM users WHERE age > ?"
result, err := command.Query(query, 25)
if err != nil {
    // handle error
}

Features

  • Support for various database drivers including;
    • MySQL
    • PostgreSQL
    • SQLite
    • SQL Server
  • Easy-to-use API for executing SQL queries, fetching data, and managing database connections
  • Efficient handling of database transactions and error handling

Credits

License

This project is licensed under the MIT License - see the LICENSE file for details.