Using Oracle in Go Language: A Complete Guide
Oracle database is one of the industry's leading relational databases and is highly praised for its data security and reliability. Many companies are using Oracle databases to save and manage massive amounts of data. Now, with the rise and application of Go language, more and more people are beginning to explore how to use Oracle in Go language.
Oracle officially provides a driver suitable for the Go language, namely "go-oci8". In this article, we'll cover how to install the driver, initialize the connection, and perform query, delete, and insert operations.
Install Oracle driver
First, we need to install the go-oci8 driver. The driver calls the Oracle database client library through CGO, so you need to check whether the Oracle client library is installed before installation.
Download the installation package: https://oracle.github.io/odpi/doc/installation.html#linux
The installation process is very simple, just unzip and run the installation script. After completing the installation, you can install the go-oci8 driver. Use the following command:
go get -v -u gopkg.in/goracle.v2
Initialize the connection
After the driver is installed and configured, we can connect to the Oracle database in the Go language. You can use the following code to initialize the connection:
package main import ( "database/sql" "fmt" _ "gopkg.in/goracle.v2" ) func main() { // 格式:[账号]/[密码]@[主机名]:[端口号]/[SID] connectionString := "scott/tiger@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))" db, err := sql.Open("goracle", connectionString) if err != nil { fmt.Println("Connection Failed : ", err) return } defer db.Close() fmt.Println("Connected successfully") }
We need to provide the connection string of the Oracle database. The connection string contains the following: username, password, host (or IP address), port, and database SID. If you are not familiar with Oracle connection strings, you can refer to the official Oracle documentation for more information.
After running the above code, if the connection is successful, "Connected successfully" will be output.
Query operation
The following is a sample code for querying the Oracle database:
package main import ( "database/sql" "fmt" _ "gopkg.in/goracle.v2" ) func main() { // Initialize connection connectionString := "scott/tiger@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))" db, err := sql.Open("goracle", connectionString) if err != nil { fmt.Println("Connection Failed : ", err) return } defer db.Close() // Execute SQL query rows, err := db.Query("SELECT EMPNO, ENAME, SAL FROM EMP WHERE DEPTNO = :1", 10) if err != nil { fmt.Println("Error executing SQL query: ", err) return } defer rows.Close() // Process rows for rows.Next() { var empno int var ename string var sal int if err := rows.Scan(&empno, &ename, &sal); err != nil { fmt.Println("Error scanning row: ", err) return } fmt.Println(empno, ename, sal) } }
This code queries all employee information with department number 10 in the employee table. Note that named parameters are used in the query. If you need to use positional parameters, use "?" instead of ":1".
Update operation
The following is a sample code to update the Oracle database:
package main import ( "database/sql" "fmt" _ "gopkg.in/goracle.v2" ) func main() { // Initialize connection connectionString := "scott/tiger@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))" db, err := sql.Open("goracle", connectionString) if err != nil { fmt.Println("Connection Failed : ", err) return } defer db.Close() // Execute SQL update result, err := db.Exec("UPDATE EMP SET SAL = :1 WHERE EMPNO = :2", 10000, 7369) if err != nil { fmt.Println("Error executing SQL update: ", err) return } rowsAffected, err := result.RowsAffected() if err != nil { fmt.Println("Error getting affected rows: ", err) return } fmt.Println("Rows affected:", rowsAffected) }
This code updates the salary of employee number 7369 in the employee table to 10000.
Insert operation
The following is a sample code to insert a record in the Oracle database:
package main import ( "database/sql" "fmt" _ "gopkg.in/goracle.v2" ) func main() { // Initialize connection connectionString := "scott/tiger@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))" db, err := sql.Open("goracle", connectionString) if err != nil { fmt.Println("Connection Failed : ", err) return } defer db.Close() // Execute SQL insert result, err := db.Exec("INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES (:1, :2, :3, :4, :5, :6, :7, :8)", 1000, "Test", "Test", 7369, "01-JAN-00", 10000, 0, 10) if err != nil { fmt.Println("Error executing SQL insert: ", err) return } rowsAffected, err := result.RowsAffected() if err != nil { fmt.Println("Error getting affected rows: ", err) return } fmt.Println("Rows affected:", rowsAffected) }
This code inserts a new record into the employees table.
Summary
This article introduces how to use Oracle database in Go language. We learned how to initialize a connection, perform queries, updates, and insert operations. Additionally, we cover how to install the necessary drivers. The above method is useful if your application needs to interact with Oracle database.
The above is the detailed content of Using Oracle in Go: A Complete Guide. For more information, please follow other related articles on the PHP Chinese website!

在Go语言中使用ElasticStack:完整指南ElasticStack是一个开源工具集,它为搜索、分析和可视化大量数据提供了强大的支持。该工具集由四个主要组件组成:Elasticsearch、Logstash、Kibana和Beats。其中,Elasticsearch是一个分布式的搜索和分析引擎,能够快速地执行搜索、分析和聚合操作。Logstash是

AWS(AmazonWebServices)是一家全球领先的云计算提供商,为企业和个人提供各种云计算服务。随着云计算技术的发展,越来越多的开发者开始使用AWS来进行开发、测试和部署他们的应用程序。Go语言是一门非常流行的编程语言,尤其适合构建高性能和可扩展的云原生应用程序。AWS提供了适用于Go语言的SDK(SoftwareDevelopmentKi

随着Web应用程序的日益普及,安全审计也变得越来越重要。PHP是一种广泛使用的编程语言,也是很多Web应用程序的基础。本文将介绍PHP中的安全审计指南,以帮助开发人员编写更加安全的Web应用程序。输入验证输入验证是Web应用程序中最基本的安全特性之一。虽然PHP提供了许多内置函数来对输入进行过滤和验证,但这些函数并不能完全保证输入的安全性。因此,开发人员需要

近年来,随着云计算技术的发展,许多企业开始转向使用云存储服务来存储和管理自己的数据。其中,AWSS3(AmazonWebServicesSimpleStorageService)是一种备受欢迎的选择。作为AWS的核心服务之一,S3提供了高可用性、高性能、可扩展和安全的存储服务。在这篇文章中,我们将深入探讨如何在Go语言中使用AWSS3。安装AW

随着现代企业的发展,ERP系统的重要性也越来越凸显出来。ERP系统可以帮助企业集成和管理计划、客户关系、供应链、人力资源等方面的数据和业务流程。PHP作为一种流行的编程语言,也可以用于开发ERP系统。在本文中,我们将分享一些PHP中的ERP系统开发指南。确定ERP系统的需求在开始开发ERP系统之前,您需要确定自己的ERP系统所需要的功能。根据你的企业的运营方
![如何使用 wmic 快速列出所有 Windows 进程 [教程]](https://img.php.cn/upload/article/000/887/227/168569000461539.jpg)
当您在处理各种重要项目并且性能是关键字时,必须准确了解后台运行的进程。特别是如果上述一个或多个过程影响您当前的工作,或者您可以只使用额外的果汁。准确地找出您的设备仍在后台运行的内容非常容易。您可以使用wmic工具在几秒钟内获得所需的所有信息。怎么样,你问?我们将在本文中向您展示这一点,因此您可以方便地获得这些信息以供将来参考。如何使用wmic了解后台进程?实际上,您可以在命令提示符窗口中输入许多有用的命令,前提是您以管理员权限打开它,这些命令可以提供宝贵的信息。了解后台进程以及收集系统信息(BI

PHP作为一种广泛使用的服务器端语言,在许多Web应用程序中扮演着重要的角色。音频处理是一个很常见的需求,例如音乐网站和音频产品销售网站等。在本文中,将为读者介绍如何在PHP中操作音频文件。一、了解音频格式在PHP中操作音频文件前,需要先了解音频文件格式。常见的音频格式有MP3、WAV、OGG、FLAC等。不同的格式有不同的音频编码算法和数据格式。例如,MP

AWS(AmazonWebServices)作为云计算业界的领头羊,提供了方便而强大的云计算服务,使得企业可以轻松地构建和管理自己的IT基础设施,并获得更好的可扩展性、灵活性和低成本。而IAM(IdentityandAccessManagement)是AWS中的重要服务之一,负责管理用户(包括人员、应用、服务等)的身份和访问权限,保障AWS资源的安


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
