# Database/SQL (Go) > เชี่ยวชาญการเข้าถึงฐานข้อมูล SQL ใน Go ด้วยแพ็กเกจ database/sql: การเชื่อมต่อ, การ query, transaction, prepared statements และการจัดการ connection pool - 22 คำถามสัมภาษณ์ - Mid-Level - [คำถามสัมภาษณ์: Go](https://sharpskill.dev/th/technologies/go/interview-questions.md) ## 1. ความแตกต่างหลักระหว่าง DB.Query และ DB.Exec คืออะไร? **คำตอบ** DB.Query ใช้สำหรับรันคำสั่ง SELECT ที่คืนค่าแถวข้อมูล ในขณะที่ DB.Exec ใช้สำหรับรันคำสั่ง SQL ที่ไม่คืนค่าผลลัพธ์ (INSERT, UPDATE, DELETE) Query คืนค่าอ็อบเจกต์ Rows ที่ต้องวนซ้ำและปิดด้วย rows.Close() ส่วน Exec คืนค่า Result ที่มีจำนวนแถวที่ได้รับผลกระทบและ ID ล่าสุดที่ถูกแทรก ## 2. เหตุใดการปิดอ็อบเจกต์ Rows หลังจากการ query จึงสำคัญ? **คำตอบ** การปิด Rows ด้วย rows.Close() จะปลดปล่อยทรัพยากรของการเชื่อมต่อ database ทำให้การเชื่อมต่อกลับสู่ pool ได้ หากไม่ปิด การเชื่อมต่อจะยังคงถูกใช้งานอยู่และ pool อาจหมด ส่งผลให้เกิดการบล็อก แนะนำให้ใช้ defer rows.Close() ทันทีหลังจากตรวจสอบ error ของ Query เพื่อรับประกันการปิดแม้จะเกิด error ในภายหลัง ## 3. ควรใช้เมธอดใดเพื่อดึงข้อมูลผลลัพธ์เพียงแถวเดียว? **คำตอบ** DB.QueryRow ออกแบบมาโดยเฉพาะเพื่อดึงข้อมูลเพียงแถวเดียวและคืนค่าอ็อบเจกต์ Row (เอกพจน์) ต่างจาก Query ที่คืนค่า Rows และต้องวนซ้ำด้วย rows.Next() โดย QueryRow ช่วยลดความซับซ้อนของโค้ดด้วยการอนุญาตให้ scan ได้โดยตรง การจัดการ error เกิดขึ้นระหว่าง Scan ซึ่ง sql.ErrNoRows บ่งชี้ว่าไม่พบแถวใด ๆ ## มีอีก 19 คำถาม - จะเปิดการเชื่อมต่อกับฐานข้อมูลด้วย database/sql ได้อย่างไร? - เมธอด rows.Scan() คืนค่าอะไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Go อื่นๆ - [พื้นฐาน Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-basics.md): 25 คำถาม, Junior - [โครงสร้างข้อมูล Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-data-structures.md): 20 คำถาม, Junior - [อินเทอร์เฟซใน Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-interfaces.md): 18 คำถาม, Junior - [การจัดการข้อผิดพลาด](https://sharpskill.dev/th/technologies/go/interview-questions/error-handling.md): 20 คำถาม, Junior - [พื้นฐาน Goroutine](https://sharpskill.dev/th/technologies/go/interview-questions/goroutines-basics.md): 20 คำถาม, Junior - [Channels](https://sharpskill.dev/th/technologies/go/interview-questions/channels.md): 22 คำถาม, Junior - [Go Modules](https://sharpskill.dev/th/technologies/go/interview-questions/go-modules.md): 18 คำถาม, Mid-Level - [เซิร์ฟเวอร์ HTTP](https://sharpskill.dev/th/technologies/go/interview-questions/http-server.md): 24 คำถาม, Mid-Level - [ไคลเอนต์ HTTP](https://sharpskill.dev/th/technologies/go/interview-questions/http-client.md): 18 คำถาม, Mid-Level - [การเข้ารหัส JSON](https://sharpskill.dev/th/technologies/go/interview-questions/json-encoding.md): 20 คำถาม, Mid-Level - [แพ็กเกจ context](https://sharpskill.dev/th/technologies/go/interview-questions/context-package.md): 20 คำถาม, Mid-Level - [Testing](https://sharpskill.dev/th/technologies/go/interview-questions/testing.md): 22 คำถาม, Mid-Level - [รูปแบบ Concurrency](https://sharpskill.dev/th/technologies/go/interview-questions/concurrency-patterns.md): 24 คำถาม, Mid-Level - [ไพรมิทีฟการซิงโครไนซ์](https://sharpskill.dev/th/technologies/go/interview-questions/sync-primitives.md): 22 คำถาม, Mid-Level - [เฟรมเวิร์กเว็บ Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-web-frameworks.md): 20 คำถาม, Mid-Level - [การออกแบบ REST API](https://sharpskill.dev/th/technologies/go/interview-questions/rest-api-design.md): 20 คำถาม, Mid-Level - [gRPC](https://sharpskill.dev/th/technologies/go/interview-questions/grpc.md): 22 คำถาม, Mid-Level - [Reflection](https://sharpskill.dev/th/technologies/go/interview-questions/reflection.md): 20 คำถาม, Senior - [การจัดการหน่วยความจำ](https://sharpskill.dev/th/technologies/go/interview-questions/memory-management.md): 24 คำถาม, Senior - [การเพิ่มประสิทธิภาพการทำงาน](https://sharpskill.dev/th/technologies/go/interview-questions/performance-optimization.md): 22 คำถาม, Senior - [Generics](https://sharpskill.dev/th/technologies/go/interview-questions/generics.md): 20 คำถาม, Senior - [Go Design Patterns](https://sharpskill.dev/th/technologies/go/interview-questions/design-patterns.md): 24 คำถาม, Senior - [Microservices](https://sharpskill.dev/th/technologies/go/interview-questions/microservices.md): 24 คำถาม, Senior - [ความปลอดภัยและการยืนยันตัวตน](https://sharpskill.dev/th/technologies/go/interview-questions/security-authentication.md): 24 คำถาม, Senior - [Docker & Containerization](https://sharpskill.dev/th/technologies/go/interview-questions/docker-containerization.md): 20 คำถาม, Senior - [Kubernetes Basics](https://sharpskill.dev/th/technologies/go/interview-questions/kubernetes-basics.md): 22 คำถาม, Senior - [Go ขั้นสูง](https://sharpskill.dev/th/technologies/go/interview-questions/advanced-go.md): 20 คำถาม, Senior - [การพัฒนา CLI](https://sharpskill.dev/th/technologies/go/interview-questions/cli-development.md): 20 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/go/interview-questions/database-sql