您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Ubuntu怎么使用SQLServer創建Go應用程序”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Ubuntu怎么使用SQLServer創建Go應用程序”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
在 Ubuntu 機器上安裝 SQL Server 2017和安裝運行 GoLang 所需的依賴項。
為了確保 SQL Server 的最佳性能,計算機應至少具有 4 GB 的內存。
(1)注冊 Microsoft Linux 存儲庫并添加其密鑰。
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list | sudo tee /etc/apt/sources.list.d/mssql-server-2017.list
(2)安裝 SQL Server。
sudo apt-get update sudo apt-get install mssql-server
執行結果:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
mssql-server
...
Unpacking mssql-server ...
Setting up mssql-server ...
(3)設置 SQL Server。
sudo /opt/mssql/bin/mssql-conf setup
執行結果:
Microsoft(R) SQL Server(R) Setup
To abort setup at anytime, press Ctrl-C.
The license terms for this product can be downloaded from http://go.microsoft.com/fwlink/?LinkId=746388 and
found in /usr/share/doc/mssql-server/LICENSE.TXT.Do you accept the license terms? If so, please type YES:
Please enter a password for the system administrator (SA) account:
Please confirm the password for the system administrator (SA) account:
如果您的機器上已經安裝了 Go,請跳過此步驟。
(1)下載安裝。
curl -O https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz tar xvf go1.8.linux-amd64.tar.gz sudo chown -R root:root ./go sudo mv go /usr/local
(2)將這兩行添加到 ~/.profile 文件中。
export GOPATH=$HOME/work export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
SQLCMD 是一個命令行工具,能夠連接到 SQL Server 并運行查詢。
(1)下載適用于操作系統版本的軟件包。
sudo su curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - #Download appropriate package for the OS version #Choose only ONE of the following, corresponding to your OS version #Ubuntu 16.04 curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list #Ubuntu 18.04 curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list #Ubuntu 19.10 curl https://packages.microsoft.com/config/ubuntu/19.10/prod.list > /etc/apt/sources.list.d/mssql-release.list exit sudo apt-get update sudo ACCEPT_EULA=Y apt-get install msodbcsql17 # optional: for bcp and sqlcmd sudo ACCEPT_EULA=Y apt-get install mssql-tools echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc source ~/.bashrc # optional: for unixODBC development headers sudo apt-get install unixodbc-dev
(2)安裝 SQLCMD 后,可以使用以下命令連接到 SQL Server:
sqlcmd -S localhost -U sa -P yourpassword 1> # You're connected! Type your T-SQL statements here. Use the keyword 'GO' to execute each batch of statements.
(3)測試數據庫。結果將打印到標準輸出。
sqlcmd -S localhost -U sa -P yourpassword -Q "SELECT @@VERSION"
--------------------------------------------------------
Microsoft SQL Server vNext (CTP2.0) - 14.0.500.272 (X64)
Apr 2 2023 11:44:40
Copyright (c) Microsoft Corporation
on Linux (Ubuntu 16.04)1 rows(s) returned
Executed in 1 ns
至此,已成功在 Ubuntu 機器上安裝 SQL Server 命令行實用程序,已經在 Ubuntu 計算機上成功安裝并設置 GoLang 和 mssql-tools。現在擁有開始使用 SQL Server 編寫 Go 應用程序所需的一切。
安裝 SQL Server 和 GoLang 后,現在可以繼續創建新的 Go 項目。在這里,將探討三個簡單的應用程序。其中一個將連接并打印數據庫服務器的SQL Server版本,另一個將執行基本的插入,更新,刪除和選擇操作,第三個將使用GORM,一種流行的對象關系映射(ORM)框架,用于Go執行相同的操作。
(1)創建新的項目目錄并安裝 Go 依賴項。
cd ~/ #Create Project Directory mkdir SqlServerSample cd SqlServerSample # Get and install the SQL Server driver for Go go get github.com/denisenkom/go-mssqldb go install github.com/denisenkom/go-mssqldb
(2)通過使用 sqlcmd 連接到 SQL Server 并執行以下命令,創建將用于本教程其余部分的數據庫。不要忘記使用自己的用戶名和密碼更新用戶名和密碼。
sqlcmd -S 127.0.0.1 -U sa -P <你的> -Q "CREATE DATABASE SampleDB;"
(3)創建一個連接到 SQL Server 的簡單 Go 應用。
在 SqlServerSample 文件夾中創建一個名為 connect.go 的文件。將以下內容復制并粘貼到文件中。不要忘記使用自己的用戶名和密碼更新用戶名和密碼。
此示例使用 GoLang 上下文方法來確保存在與數據庫服務器的活動連接。
package main import ( _ "github.com/denisenkom/go-mssqldb" "database/sql" "context" "log" "fmt" ) // Replace with your own connection parameters var server = "localhost" var port = 1433 var user = "sa" var password = "xxxxxx" var db *sql.DB func main() { var err error // Create connection string connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", server, user, password, port) // Create connection pool db, err = sql.Open("sqlserver", connString) if err != nil { log.Fatal("Error creating connection pool: " + err.Error()) } log.Printf("Connected!\n") // Close the database connection pool after program executes defer db.Close() SelectVersion() } // Gets and prints SQL Server version func SelectVersion(){ // Use background context ctx := context.Background() // Ping database to see if it's still alive. // Important for handling network issues and long queries. err := db.PingContext(ctx) if err != nil { log.Fatal("Error pinging database: " + err.Error()) } var result string // Run query and scan for result err = db.QueryRowContext(ctx, "SELECT @@version").Scan(&result) if err != nil { log.Fatal("Scan failed:", err.Error()) } fmt.Printf("%s\n", result) }
(4)運行應用程序。
go run connect.go
執行結果:
Connected!
Microsoft SQL Server 2017 (CTP2.1) - 14.0.600.250 (X64)
Apr 2 2017 12:21:23
Copyright (C) 2017 Microsoft Corporation. All rights reserved.
Developer Edition (64-bit) on Linux (Ubuntu 16.04.2 LTS)
(5)在 SqlServerSample 文件夾中創建一個名為 CreateTestData 的文件.sql。將以下 T-SQL 代碼復制并粘貼到其中。這將創建一個架構、表并插入幾行。
CREATE SCHEMA TestSchema; GO CREATE TABLE TestSchema.Employees ( Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, Name NVARCHAR(50), Location NVARCHAR(50) ); GO INSERT INTO TestSchema.Employees (Name, Location) VALUES (N'Jared', N'Australia'), (N'Nikita', N'India'), (N'Tom', N'Germany'); GO SELECT * FROM TestSchema.Employees; GO
(6)使用 sqlcmd 連接到數據庫并運行 SQL 腳本以創建架構、表并插入一些行。
sqlcmd -S 127.0.0.1 -U sa -P <你的> -d SampleDB -i ./CreateTestData.sql
執行結果:
CREATE SCHEMA TestSchema; Executed in 0 ms CREATE TABLE TestSchema.Employees ( Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, Name NVARCHAR(50), Location NVARCHAR(50) ); Executed in 0 ms INSERT INTO TestSchema.Employees (Name, Location) VALUES (N'Jared', N'Australia'), (N'Nikita', N'India'), (N'Tom', N'Germany'); Executed in 0 ms SELECT * FROM TestSchema.Employees; Id Name Location -- ------ --------- 1 Jared Australia 2 Nikita India 3 Tom Germany 3 row(s) returned Executed in 1 ms
(7)在 SqlServerSample 文件夾中創建一個名為 crud.go 的新文件。將以下代碼復制并粘貼到其中。這將插入、更新、刪除和讀取幾行。
package main import ( _ "github.com/denisenkom/go-mssqldb" "database/sql" "context" "log" "fmt" "errors" ) var db *sql.DB var server = "localhost" var port = 1433 var user = "sa" var password = "你的" var database = "SampleDB" func main() { // Build connection string connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;", server, user, password, port, database) var err error // Create connection pool db, err = sql.Open("sqlserver", connString) if err != nil { log.Fatal("Error creating connection pool: ", err.Error()) } ctx := context.Background() err = db.PingContext(ctx) if err != nil { log.Fatal(err.Error()) } fmt.Printf("Connected!\n") // Create employee createID, err := CreateEmployee("Jake", "United States") if err != nil { log.Fatal("Error creating Employee: ", err.Error()) } fmt.Printf("Inserted ID: %d successfully.\n", createID) // Read employees count, err := ReadEmployees() if err != nil { log.Fatal("Error reading Employees: ", err.Error()) } fmt.Printf("Read %d row(s) successfully.\n", count) // Update from database updatedRows, err := UpdateEmployee("Jake", "Poland") if err != nil { log.Fatal("Error updating Employee: ", err.Error()) } fmt.Printf("Updated %d row(s) successfully.\n", updatedRows) // Delete from database deletedRows, err := DeleteEmployee("Jake") if err != nil { log.Fatal("Error deleting Employee: ", err.Error()) } fmt.Printf("Deleted %d row(s) successfully.\n", deletedRows) } // CreateEmployee inserts an employee record func CreateEmployee(name string, location string) (int64, error) { ctx := context.Background() var err error if db == nil { err = errors.New("CreateEmployee: db is null") return -1, err } // Check if database is alive. err = db.PingContext(ctx) if err != nil { return -1, err } tsql := "INSERT INTO TestSchema.Employees (Name, Location) VALUES (@Name, @Location); select convert(bigint, SCOPE_IDENTITY());" stmt, err := db.Prepare(tsql) if err != nil { return -1, err } defer stmt.Close() row := stmt.QueryRowContext( ctx, sql.Named("Name", name), sql.Named("Location", location)) var newID int64 err = row.Scan(&newID) if err != nil { return -1, err } return newID, nil } // ReadEmployees reads all employee records func ReadEmployees() (int, error) { ctx := context.Background() // Check if database is alive. err := db.PingContext(ctx) if err != nil { return -1, err } tsql := fmt.Sprintf("SELECT Id, Name, Location FROM TestSchema.Employees;") // Execute query rows, err := db.QueryContext(ctx, tsql) if err != nil { return -1, err } defer rows.Close() var count int // Iterate through the result set. for rows.Next() { var name, location string var id int // Get values from row. err := rows.Scan(&id, &name, &location) if err != nil { return -1, err } fmt.Printf("ID: %d, Name: %s, Location: %s\n", id, name, location) count++ } return count, nil } // UpdateEmployee updates an employee's information func UpdateEmployee(name string, location string) (int64, error) { ctx := context.Background() // Check if database is alive. err := db.PingContext(ctx) if err != nil { return -1, err } tsql := fmt.Sprintf("UPDATE TestSchema.Employees SET Location = @Location WHERE Name = @Name") // Execute non-query with named parameters result, err := db.ExecContext( ctx, tsql, sql.Named("Location", location), sql.Named("Name", name)) if err != nil { return -1, err } return result.RowsAffected() } // DeleteEmployee deletes an employee from the database func DeleteEmployee(name string) (int64, error) { ctx := context.Background() // Check if database is alive. err := db.PingContext(ctx) if err != nil { return -1, err } tsql := fmt.Sprintf("DELETE FROM TestSchema.Employees WHERE Name = @Name;") // Execute non-query with named parameters result, err := db.ExecContext(ctx, tsql, sql.Named("Name", name)) if err != nil { return -1, err } return result.RowsAffected() }
(8)運行 crud.go 應用以查看結果。
go run crud.go
執行結果:
Connected!
Inserted ID: 4 successfully.
ID: 1, Name: Jared, Location: Australia
ID: 2, Name: Nikita, Location: India
ID: 3, Name: Tom, Location: Germany
ID: 4, Name: Jake, Location: United States
Read 4 row(s) successfully.
Updated 1 row(s) successfully.
Deleted 1 row(s) successfully.
(1)創建應用目錄并初始化 Go 依賴項。
cd ~/ mkdir SqlServerGormSample cd SqlServerGormSample # Get and install the SQL Server driver for Go go get github.com/denisenkom/go-mssqldb go install github.com/denisenkom/go-mssqldb
(2)將以下內容粘貼到名為orm.go的文件中。確保將密碼變量替換為您自己的變量。
package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mssql" "log" ) var server = "localhost" var port = 1433 var user = "sa" var password = "你的" var database = "SampleDB" // Define a User model struct type User struct { gorm.Model FirstName string LastName string } // Define a Task model struct type Task struct { gorm.Model Title string DueDate string IsComplete bool UserID uint } // Read and print all the tasks func ReadAllTasks(db *gorm.DB){ var users []User var tasks []Task db.Find(&users) for _, user := range users{ db.Model(&user).Related(&tasks) fmt.Printf("%s %s's tasks:\n", user.FirstName, user.LastName) for _, task := range tasks { fmt.Printf("Title: %s\nDueDate: %s\nIsComplete:%t\n\n", task.Title, task.DueDate, task.IsComplete) } } } // Update a task based on a user func UpdateSomeonesTask(db *gorm.DB, userId int){ var task Task db.Where("user_id = ?", userId).First(&task).Update("Title", "Buy donuts for Luis") fmt.Printf("Title: %s\nDueDate: %s\nIsComplete:%t\n\n", task.Title, task.DueDate, task.IsComplete) } // Delete all the tasks for a user func DeleteSomeonesTasks(db *gorm.DB, userId int){ db.Where("user_id = ?", userId).Delete(&Task{}) fmt.Printf("Deleted all tasks for user %d", userId) } func main() { connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s", server, user, password, port, database) db, err := gorm.Open("mssql", connectionString) if err != nil { log.Fatal("Failed to create connection pool. Error: " + err.Error()) } gorm.DefaultCallback.Create().Remove("mssql:set_identity_insert") defer db.Close() fmt.Println("Migrating models...") db.AutoMigrate(&User{}) db.AutoMigrate(&Task{}) // Create awesome Users fmt.Println("Creating awesome users...") db.Create(&User{FirstName: "Andrea", LastName: "Lam"}) //UserID: 1 db.Create(&User{FirstName: "Meet", LastName: "Bhagdev"}) //UserID: 2 db.Create(&User{FirstName: "Luis", LastName: "Bosquez"}) //UserID: 3 // Create appropriate Tasks for each user fmt.Println("Creating new appropriate tasks...") db.Create(&Task{ Title: "Do laundry", DueDate: "2017-03-30", IsComplete: false, UserID: 1}) db.Create(&Task{ Title: "Mow the lawn", DueDate: "2017-03-30", IsComplete: false, UserID: 2}) db.Create(&Task{ Title: "Do more laundry", DueDate: "2017-03-30", IsComplete: false, UserID: 3}) db.Create(&Task{ Title: "Watch TV", DueDate: "2017-03-30", IsComplete: false, UserID: 3}) // Read fmt.Println("\nReading all the tasks...") ReadAllTasks(db) // Update - update Task title to something more appropriate fmt.Println("Updating Andrea's task...") UpdateSomeonesTask(db, 1) // Delete - delete Luis's task DeleteSomeonesTasks(db, 3) }
(3)運行 orm.go 應用。
go run orm.go
執行結果:
[info] removing callback `mssql:set_identity_insert` from C:/Projects/golang-experiments/tutorials/orm.go:70
Migrating models...
Creating awesome users...
Creating new appropriate tasks...Reading all the tasks...
Andrea Lam's tasks:
Title: Do laundry
DueDate: 2017-03-30
IsComplete:falseMeet Bhagdev's tasks:
Title: Mow the lawn
DueDate: 2017-03-30
IsComplete:falseLuis Bosquez's tasks:
Title: Do more laundry
DueDate: 2017-03-30
IsComplete:falseTitle: Watch TV
DueDate: 2017-03-30
IsComplete:falseUpdating Andrea's task...
Title: Buy donuts for Luis
DueDate: 2017-03-30
IsComplete:falseDeleted all tasks for user 3
已了解基礎知識,接下來可以了解如何使用 SQL Server 改進應用。通過列存儲索引的簡單示例,以及它們如何提高數據處理速度。與傳統行存儲索引相比,列存儲索引在分析工作負荷上可實現高達 100 倍的性能,并將數據壓縮提高多達 10 倍。
(1)切換到主目錄并為項目創建一個文件夾。
cd ~/mkdir SqlServerColumnstoreSamplecd SqlServerColumnstoreSample
(2)在 SqlServerColumnstoreSample 文件夾中創建一個名為 CreateSampleTable 的新文件.sql文件。將下面的 T-SQL 代碼粘貼到新的 SQL 文件中。保存并關閉文件。
WITH a AS (SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) AS a(a)) SELECT TOP(5000000) ROW_NUMBER() OVER (ORDER BY a.a) AS OrderItemId ,a.a + b.a + c.a + d.a + e.a + f.a + g.a + h.a AS OrderId ,a.a * 10 AS Price ,CONCAT(a.a, N' ', b.a, N' ', c.a, N' ', d.a, N' ', e.a, N' ', f.a, N' ', g.a, N' ', h.a) AS ProductName INTO Table_with_5M_rows FROM a, a AS b, a AS c, a AS d, a AS e, a AS f, a AS g, a AS h;
(3)使用 sqlcmd 連接到數據庫并運行 SQL 腳本以創建包含 5 萬行的表。這可能需要幾分鐘才能運行。
sqlcmd -S 127.0.0.1 -U sa -P <你的> -d SampleDB -i ./CreateSampleTable.sql
(1)在項目文件夾中,初始化 Go 依賴項。
go get github.com/denisenkom/go-mssqldb go install github.com/denisenkom/go-mssqldb
(2)在您的文件夾中創建一個名為 columnstore.go 的文件。
package main import ( _ "github.com/denisenkom/go-mssqldb" "database/sql" "context" "log" "fmt" "time" ) var server = "localhost" var port = 1433 var user = "sa" var password = "你的" var database = "SampleDB" var db *sql.DB // Delete an employee from database func ExecuteAggregateStatement(db *sql.DB) { ctx := context.Background() // Ping database to see if it's still alive. // Important for handling network issues and long queries. err := db.PingContext(ctx) if err != nil { log.Fatal("Error pinging database: " + err.Error()) } var result string // Execute long non-query to aggregate rows err = db.QueryRowContext(ctx, "SELECT SUM(Price) as sum FROM Table_with_5M_rows").Scan(&result) if err != nil { log.Fatal("Error executing query: " + err.Error()) } fmt.Printf("Sum: %s\n", result) } func main() { // Connect to database connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;", server, user, password, port, database) var err error // Create connection pool db, err = sql.Open("sqlserver", connString) if err != nil { log.Fatal("Open connection failed:", err.Error()) } fmt.Printf("Connected!\n") defer db.Close() t1 := time.Now() fmt.Printf("Start time: %s\n", t1) ExecuteAggregateStatement(db) t2 := time.Since(t1) fmt.Printf("The query took: %s\n", t2) }
從終端運行 Go 應用。
go run columnstore.go
執行結果:
Connected!
Start time: 2023-04-02 15:33:50.0340976 -0700 PDT
Sum: 50000000
The query took: 601.7463ms
運行以下命令以在表上創建列存儲索引:
sqlcmd -S localhost -U sa -P <你的> -d SampleDB -Q "CREATE CLUSTERED COLUMNSTORE INDEX Columnstoreindex ON Table_with_5M_rows;"
go run columnstore.go
Connected!
Start time: 2017-06-05 16:35:02.5409285 -0700 PDT
Sum: 50000000
The query took: 86.9826ms
讀到這里,這篇“Ubuntu怎么使用SQLServer創建Go應用程序”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。