ODBC database driver for GO:GO access to SQL S via ODBC

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.


The go language accesses SQL Server through ODBC, which needs to use the GO-ODBC Library, open Source address:: HTTPS://GITHUB.COM/WEIGJ/GO-ODBC



First, drive installation



Open the Gopath in cmd:






Go to src Directory






In the SRC directory, get the ODBC driver via git:

git clone git: //github.com/weigj/go-odbc.git odbc
Go to the odbc directory:
Run the go install command:



Tip The GCC compiler is not installed to download http://tdm-gcc.tdragon.net/on the official website.



Ii. installation of GCC



or download Mingw-w64-install.exe (170.0 KB), reboot after installation.



is the Mingw-w64-install.exe (https://sourceforge.net/projects/mingw-w64/) installation process:












Configure the GCC environment, open system variables, modify path, add E:\Program files\mingw-w64\x86_64-5.3.0-posix-seh-rt_v4-rev0\mingw64\bin, such as:






Run the Go install command again without prompting for an error message, such as:






Iii. Examples of programs



Idea: Link database--Create a query--display query results

package main import ("database / sql"
     "fmt" _ "odbc / driver") func main () {fmt.Printf ("% s \ n", "Create database link") conn, err: = sql.Open ("odbc", "Driver = {SQL Server}; SERVER =. \\ sql2008; UID = sa; PWD=admin@163.com; DATABASE = DTcmsdb4 ") if err! = Nil {fmt.Println (" Link error ") return} defer conn.Close () fmt.Printf ("% s \ n", "Build query") stmt, err: = conn.Prepare ("select id from dt_article") if err! = nil {fmt.Println ("Query exception:", err) return} defer stmt.Close () row, err: = stmt.Query () if err! = nil {fmt.Println ("Query error:", err)} defer row.Close () fmt.Printf ("% s \ n "," Dataset display ") for row.Next () {var id int
         if err: = row.Scan (& id); err == nil {fmt.Println (id)}} fmt.Printf ("% s \ n", "Complete query") return}


The problem comes with running successfully through the command line:






Running through the VSC, the following error is indicated:






Analysis reason: In the D:\GoWorks directory my first go.go file and Access-sqlserver.go, both have the main function.



WORKAROUND: Modify the main function name in the My first go.go file to Maina.



On secondary commissioning, success:






As a summary, in VSC, all the files in the same directory are compiled.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.