package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func dbinit(db *sql.DB) { dbinit, err := db.Query("CREATE TABLE IF NOT EXISTS `media` (`id` INT NOT NULL AUTO_INCREMENT, `yt-id` VARCHAR(100), `url` VARCHAR(255), `localpath` TEXT(65535), `size` INT, `sha256sum` VARCHAR(255), `length` INT, PRIMARY KEY (`id`));") if err != nil { fmt.Println("Error while creating table") panic(err.Error()) } defer dbinit.Close() fmt.Println("DB-Init successfull") } func dbentrycount(db *sql.DB) { var count int dbcount, err := db.Query("SELECT COUNT(*) FROM testdb.media;") if err != nil { fmt.Println("Error while counting rows from table") panic(err.Error()) } defer dbcount.Close() for dbcount.Next() { err = dbcount.Scan(&count) if err != nil { panic(err.Error()) } } fmt.Printf("There are %v entries in the database\n", count) } func main() { db, err := sql.Open("mysql", "username:password@tcp(127.0.0.1:3306)/testdb") if err != nil { fmt.Println("Error while connecting with the database") panic(err.Error()) } fmt.Println("Successfully connected to the database") defer db.Close() dbinit(db) dbentrycount(db) }