package service import ( "database/sql" "fmt" "log" _ "github.com/mattn/go-sqlite3" ) type MacAddress struct { Id string Prefix string VendorName string Private bool BlockType string LastUpdate string } type SQLiteHelper struct { connectionString string db *sql.DB } func NewSQLiteHelper(dbFilePath string) *SQLiteHelper { connectionString := fmt.Sprintf("file:%s?cache=shared&mode=rwc", dbFilePath) db, err := sql.Open("sqlite3", connectionString) if err != nil { log.Fatal(err) } return &SQLiteHelper{ connectionString: connectionString, db: db, } } func (helper *SQLiteHelper) Close() { helper.db.Close() } func (helper *SQLiteHelper) QueryMacAddressByPrefix(prefix string) (*MacAddress, error) { const tableName = "MacAddresses" query := fmt.Sprintf("SELECT * FROM %s WHERE Prefix = ?", tableName) row := helper.db.QueryRow(query, prefix) var macAddress MacAddress err := row.Scan(&macAddress.Id, &macAddress.Prefix, &macAddress.VendorName, &macAddress.Private, &macAddress.BlockType, &macAddress.LastUpdate) if err != nil { if err == sql.ErrNoRows { return nil, nil } return nil, err } return &macAddress, nil } func (helper *SQLiteHelper) querySingleColumn(tableName, columnName string) (string, error) { query := fmt.Sprintf("SELECT %s FROM %s LIMIT 1", columnName, tableName) row := helper.db.QueryRow(query) var result string err := row.Scan(&result) if err != nil { return "", err } return result, nil } func (helper *SQLiteHelper) QueryVersion() (string, error) { return helper.querySingleColumn("Config", "Version") } // func main() { // // Example usage // helper := NewSQLiteHelper("path/to/database.db") // defer helper.Close() // version, err := helper.QueryVersion() // if err != nil { // log.Fatal(err) // } // fmt.Println("Database version:", version) // macAddress, err := helper.QueryMacAddressByPrefix("00:1A:2B") // if err != nil { // log.Fatal(err) // } // fmt.Println("MacAddress:", macAddress) // }