Last active
December 23, 2023 13:28
-
-
Save JudahSan/bd8214e0d501f8d4e216fcaf0273c958 to your computer and use it in GitHub Desktop.
Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work ERROR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package db | |
import ( | |
"database/sql" | |
"fmt" | |
_ "github.com/mattn/go-sqlite3" | |
) | |
var DB *sql.DB | |
func InitDB() { | |
var err error | |
DB, err = sql.Open("sqlite3", "api.db") | |
if err != nil { | |
panic("Could not connect to database." + err.Error()) | |
} | |
// Log successful connection | |
fmt.Println("Connected to the database successfully.") | |
DB.SetMaxOpenConns(10) | |
DB.SetMaxIdleConns(5) | |
createTables() | |
} | |
func createTables() { | |
createEventsTable := ` | |
CREATE TABLE IF NOT EXISTS events ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT, | |
name TEXT NOT NULL, | |
description TEXT NOT NULL, | |
location TEXT NOT NULL, | |
dateTime DATETIME NOT NULL, | |
user_Id INTEGER | |
) | |
` | |
_, err := DB.Exec(createEventsTable) | |
if err != nil { | |
panic("Could not create events table.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connected to the database successfully. | |
Error creating events table. Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub | |
panic: Could not create events table. | |
goroutine 1 [running]: | |
github.com/JudahSan/rest-api/db.createTables() | |
/home/zoro/Development/golang-rest-api/db/db.go:42 +0xb8 | |
github.com/JudahSan/rest-api/db.InitDB() | |
/home/zoro/Development/golang-rest-api/db/db.go:24 +0xbc | |
main.main() | |
/home/zoro/Development/golang-rest-api/main.go:12 +0x17 | |
exit status 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This solved the issue | |
["I ran into this using go run . Had to run apt install build-essential on the host and then all was well."](https://github.com/mattn/go-sqlite3/issues/855#issuecomment-1496136603) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment