Description

When i build with gorm and sqlite, Then start the binary,I got the errors:

[error] failed to initialize database, got error Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
[LOG-debug] 2023/07/19 20:59:36 Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
[root@VM-0-13-centos ]# Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stu

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: ckganesan

As of my last knowledge update in September 2021, Go applications that use the go-sqlite3 package and require the cgo tool for building (which is the case for go-sqlite3) may need to set the CGO_ENABLED=1 environment variable during the build process. The cgo tool allows Go code to call C code, and some database drivers like go-sqlite3 utilize it to interface with SQLite, which is written in C.

Additionally, you may need to have a C compiler (like GCC) installed and available in your system's PATH so that cgo can properly compile the C code for SQLite. The C compiler is required to build the C code bindings and create the necessary shared object files.

To summarize, when using go-sqlite3, you might need to follow these steps during the build process:

  1. Set the CGO_ENABLED=1 environment variable to enable cgo support: $ export CGO_ENABLED=1
  2. Ensure you have a C compiler (e.g., GCC) installed and available in your system's PATH.
  3. Run the Go build command for your application.

Keep in mind that tooling and dependencies might change over time, so it's always a good idea to refer to the official documentation of the go-sqlite3 package or any other relevant resources for the most up-to-date instructions on building and using the package. Also, ensure that you have installed the correct version of go-sqlite3 that supports your Go version.

https://pkg.go.dev/github.com/mattn/go-sqlite3#readme-installation

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: saeidee

As @ckganesan mentioned this is not related to gorm but it is related to go-sqlite3 package.

Comment From: Destroyer-XR

download TDM-GCC and Follow him, mine on windows must be loaded gcc

จากการอัปเดตความรู้ครั้งล่าสุดของผมในเดือนกันยายน 2021 แอปพลิเคชัน Go ที่ใช้แพ็กเกจ go-sqlite3 และต้องใช้เครื่องมือ cgo ในการสร้าง (ซึ่งเป็นกรณีของ go-sqlite3) อาจจำเป็นต้องตั้งค่าตัวแปรสภาพแวดล้อม CGO_ENABLED=1 ในระหว่างกระบวนการสร้าง เครื่องมือ cgo ช่วยให้โค้ด Go สามารถเรียกใช้โค้ด C ได้ และไดรเวอร์ฐานข้อมูลบางตัว เช่น go-sqlite3 ก็ใช้เครื่องมือนี้เพื่อเชื่อมต่อกับ SQLite ซึ่งเขียนด้วยภาษา C

นอกจากนี้ คุณอาจต้องติดตั้งคอมไพเลอร์ C (เช่น GCC) และพร้อมใช้งานใน PATH ของระบบ เพื่อให้ cgo สามารถคอมไพเลอร์โค้ด C สำหรับ SQLite ได้อย่างถูกต้อง จำเป็นต้องใช้คอมไพเลอร์ C เพื่อสร้างการผูกโค้ด C และสร้างไฟล์ออบเจ็กต์ที่ใช้ร่วมกันที่จำเป็น

สรุป เมื่อใช้ go-sqlite3 คุณอาจต้องปฏิบัติตามขั้นตอนเหล่านี้ในระหว่างกระบวนการสร้าง:

  1. ตั้งค่าตัวแปรสภาพแวดล้อม CGO_ENABLED=1 เพื่อเปิดใช้งานการรองรับ cgo: $ export CGO_ENABLED=1
  2. ตรวจสอบให้แน่ใจว่าคุณได้ติดตั้งคอมไพเลอร์ C (เช่น GCC) และพร้อมใช้งานใน PATH ของระบบของคุณ
  3. เรียกใช้คำสั่ง Go build สำหรับแอปพลิเคชันของคุณ

โปรดทราบว่าเครื่องมือและ dependency อาจเปลี่ยนแปลงไปตามกาลเวลา ดังนั้นจึงควรอ้างอิงเอกสารประกอบอย่างเป็นทางการของแพ็กเกจ go-sqlite3 หรือแหล่งข้อมูลอื่นๆ ที่เกี่ยวข้อง เพื่อดูคำแนะนำล่าสุดเกี่ยวกับการสร้างและใช้งานแพ็กเกจ โปรดตรวจสอบให้แน่ใจว่าคุณได้ติดตั้ง go-sqlite3 เวอร์ชันที่ถูกต้องที่รองรับ Go ของคุณ

https://pkg.go.dev/github.com/mattn/go-sqlite3#readme-installation