directory of this document
- 1.Go Connect SQLite
- 1_1.sqlite Recommended Drivers
- 1_2.sqlite Connection Sample Code
- 2.Go connecting MySQL
- 2_1.mysql Recommended Drivers
- 2_2.mysql Connection Sample Code
- 3.Go Connecting Oracle
- 3_1.oracle recommended drivers and preparation matters
- 3_2.oracle Connection Sample Code
Description: TheGo Language connection database is not as convenient as Java, this article describes the connection of three typical database drive and connection methods: Small, SQLite, Medium, MySQL, Large, Oracle.
-1.go Connect SQLite1.Go Connect SQLiteBack to Top1_1.sqlite Recommended Drivers
Https://github.com/mattn/go-sqlite3
Back to Top1_2.sqlite Connection Sample code
The sample code is as follows:
PackageMainImport ( "Database/sql" "FMT" _ "Github.com/mattn/go-sqlite3" "Log" "OS") type Users struct {UserIdintUname String}func Main () {OS. Remove ("./foo.db") DB, err:= SQL. Open ("Sqlite3", "./foo.db") ifErr! =Nil {log. Fatal (ERR)} defer db. Close () SQL:=' CREATE table Users (UserId integer, uname text); ' Db. EXEC (SQL) SQL= ' INSERT into users ' (Userid,uname) VALUES (1, ' Mike '); ' Db. EXEC (SQL) SQL= ' INSERT into users ' (Userid,uname) VALUES (2, ' John '); ' Db. EXEC (SQL) rows, err:= db. Query ("SELECT * from Users") ifErr! =Nil {log. Fatal (ERR)} defer rows. Close () var users []users= Make ([]users, 0) forrows. Next () {var u Users rows. Scan (&u.userid, &u.uname) Users=Append (Users, u)} fmt. Println (Users)}
The result of the execution is:
[{1 Mike} {2 John}] also generates FOO.DB in the current directory
-2.go connecting MySQL2.Go connecting MySQLBack to Top2_1.mysql Recommended Drivers
Https://github.com/Go-SQL-Driver/MySQL
Back to Top2_2.mysql Connection Sample code
The sample code is as follows:
PackageMainImport ( "Database/sql" "FMT" _ "Github.com/go-sql-driver/mysql") type Users struct {UserIdintUname String}func Main () {//db, err: = SQL. Open ("MySQL", "User:[email protected]/dbname")DB, err: = SQL. Open ("MySQL", "Root:[email protected]/test") ifErr! =Nil {fmt. Println ("Failed to connect to database")} defer db. Close () var users []users= Make ([]users, 0) Sqlstr:= "SELECT * from Users"rows, err:=db. Query (SQLSTR)ifErr! =Nil {fmt. PRINTLN (ERR)}Else { forI: = 0; Rows. Next (); i++{var u Users rows.} Scan (&u.userid, &u.uname) Users=Append (Users, u)} fmt. Println (Users)}}
The result of the execution is:
[{1 Mike} {2 John}]
-3.go Connecting Oracle3.Go connecting OracleBack to Top3_1.oracle recommended Drivers and preparation matters
My database-related configuration is version 11.2.0.1.0Go version is 1.2 system is WIN7 Ultimate 64-bit follow the steps below to eventually connect to the Oracle① first is to install Git on the machine (this is a must As a go developer ② download the latest version of OCI although I used the version of 11.2, but I tried n times to return to the current only the latest 12.1.0.1.0 is the only thing that works is http://www.oracle.com/technetwork/cn/database/ winx64soft-089540.html If this address is not so, can again Baidu is search instant Client Downloads for Microsoft Windows (x64) To download the Instantclient-basic and instantclient-sdk two zip files after download, unpack the two packages, and then place the SDK folder under Instantclient_12_1. form the INSTANTCLIENT_12_1/SDK directory level and then rename the Instantclient_12_1 folder to Instantclient_11_2 and put it in the C drive with the directory ③ download MinGW the latest version (actually I'm not using the latest With this version of X86_64-4.9.0-posix-seh-rt_v3-rev2) ④ to https://github.com/wendal/ Go-oci8 Download Pkg-config.exe and oci8.pc Note Do not put these source git to the computer, but first download Pkg-config.exe and oci8.pc (in the Windows directory) After downloading, do the following to copy Pkg-config.exe to mingw\bin\ under copy oci8.pc to Mingw\lib\pkg-config\ (my pkg-config is new because it was not) note, oci8.pc Need to be modified according to the OCI you downloaded. Here are the changes I made based on the OCI version I downloaded. # Information for PKG-CONFIGPREFIX=C:/INSTANTCLIENT_11_2EXEC_PREFIX=C:/INSTANTCLIENT_11_2LIBDIR=${EXEC_ Prefix}includedir=${prefix}/sdk/include/name:ocidescription:oracle Database Engineversion:11.2Libs:-l${libdir}-locilibs.private:cflags:-i${includedir}⑤ Modify the system environment variables, add path= the original PATH; C:\instantclient_11_2;d:\mingw\bin; (Readers change according to their own catalogue) Pkg_config_path=d:\mingw\lib\pkg-config (readers according to their own directory change) ⑥ download the source code. Https://github.com/wendal/go-oci8 git to local ( This is the GO-OCI library, which is the driver that connects to Oracle. Go get github.com/wendal/go-oci8 then run the test.
Back to Top3_2.oracle Connection Sample code
The sample code is as follows:
PackageMainImport ( "Database/sql" "FMT" _ "Github.com/wendal/go-oci8" "Log") type Users struct {UserIdintUname String}func Main () {log. Println ("Oracle Driver connecting ...") //Username /Password @ Instance name System/[email protected], Sys/[email protected]DB, err: = SQL. Open ("Oci8", "Bookman/[email protected]") ifErr! =Nil {log. Fatal (Err) Panic ("Database connection Failed") } Else{defer db. Close () var users []users= Make ([]users, 0) rows, err:= db. Query ("SELECT * from Users") ifErr! =Nil {log. Fatal (ERR)}Else { forrows. Next () {var u Users rows. Scan (&u.userid, &u.uname) Users=Append (Users, u)} fmt. PRINTLN (users) defer rows. Close () }}}
The execution process is slower than MySQL and SQLite, and the results are as follows
2014/07/08 01:14:05 Oracle Driver connecting .... [{1 Mike} {2 John}]