📜 ⬆️ ⬇️

Working with MS SQL Database with Go Tools for Beginners

At the moment, the Go language is becoming more and more popular every day. On Habré, there are more and more articles on the topic that are interesting to read not only to the hardened specialists-programmers, but also to system administrators.

I work as a system administrator and show an interest in Go, since we often have to write scripts in bash (shell) to automate our actions and increase the time for eating cookies and pouring coffee into our frail body.

I would like to share a little experience about how a non-programmer wrote a small program on Go.

Let's get started
')
In the company in which I work there is a certain program which is used by all. It is based on the MS SQL 2008 server. At a wonderful moment (not very good for me), the chef says that you need to write a program in which the right people will watch certain data from that very database.

To work with databases have packages database / sql and code.google.com/p/odbc . We will use them. Just do not forget that you need to install the ODBC driver, through which we will work with MS SQL. For Windows, this is done via Odbcad32.exe by adding a client DSN. For Linux, it is a little more complicated, but this is not within the scope of this article I think Google will help you.

This is how we get a list of packages that we will use in the first stage.

import ( "database/sql" "fmt" "log" _ "code.google.com/p/odbc" ) 

We try to connect to the database and execute the query in order to get the data we need.

 package main import ( _ "code.google.com/p/odbc" "database/sql" "fmt" "log" ) var ( name_otdel string query string ) func main() { db, err := sql.Open("odbc", "DSN=DBS0") if err != nil { fmt.Println("Error in connect DB") log.Fatal(err) } query = "select t.DepartmentNAME from dbo.oms_Department t where t.rf_LPUID = 1078" rows, err := db.Query(query) if err != nil { log.Fatal(err) } for rows.Next() { if err := rows.Scan(&name_otdel); err != nil { log.Fatal(err) } fmt.Println(name_otdel) } defer rows.Close() } 

As you can see from the program, everything is not so difficult, and even people like me (little understood in programming) can gradually learn to write on Go.

True, there is one nuance. When deploying a database server, MS SQL Server usually does not touch the encoding settings and it is in most places windows1251. This is a bit of an inconvenience, since everything in Go works in UTF8. In this regard, we will use additional packages that are not included in Go for the conversion of windows1251 to UTF8.

Therefore, if you run our program, you will see a beaverd in the console instead of Russian letters. To avoid this, let's use the golang.org/x/text/encoding/charmap, golang.org/x/text/transform packages.

Complete program with transcoding from cp1251 to UTF8
 package main import ( "database/sql" "fmt" "io/ioutil" "log" "strings" "golang.org/x/text/encoding/charmap" "golang.org/x/text/transform" _ "code.google.com/p/odbc" ) var ( name_otdel string name_utf string query string ) func main() { db, err := sql.Open("odbc", "DSN=DBS0") if err != nil { fmt.Println("Error in connect DB") log.Fatal(err) } query = "select t.DepartmentNAME from dbo.oms_Department t where t.rf_LPUID = 1078" rows, err := db.Query(query) if err != nil { log.Fatal(err) } for rows.Next() { if err := rows.Scan(&name_otdel); err != nil { log.Fatal(err) } sr := strings.NewReader(name_otdel) tr := transform.NewReader(sr, charmap.Windows1251.NewDecoder()) buf, err := ioutil.ReadAll(tr) if err != nil { log.Fatal(err) } name_utf = string(buf) fmt.Println(name_utf) } defer rows.Close() } 


Final result:



We see and rejoice that everything is read and the data is pulled out of the database. I want to note that if the query contains two or more columns, for example, last name, first name and middle name, then we do not forget to specify them in rows.Scan in exactly the same order as in the SQL query.

What have we learned after reading this article? Gained basic knowledge of working with databases. Then it will be possible to process them, sort or display them in the browser, etc. etc. The article is not designed for experienced programmers and is written specifically for people who are just beginning to learn the rather interesting language Go.

Source: https://habr.com/ru/post/253365/


All Articles