एक डेटाबेस से जुड़ना //Connecting to a Database

GORM officially supports the databases MySQL, PostgreSQL, GaussDB, SQLite, SQL Server TiDB, and Oracle Database

MySQL

import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func main() {
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
}

ध्यान दें: time.Time को सही तरीके से हैंडल करने के लिए, आपको parseTime को एक पैरामीटर के रूप में शामिल करना होगा। (अधिक पैरामीटर) UTF-8 एन्कोडिंग का पूर्ण समर्थन करने के लिए, आपको charset=utf8 को charset=utf8mb4 में बदलना होगा। विस्तृत व्याख्या के लिए यह लेख देखें

MySQL ड्राइवर कुछ advanced कॉन्फ़िगरेशन प्रदान करता है, जिसका उपयोग initialization के दौरान किया जा सकता है, उदाहरण के लिए:

db, err := gorm.Open(mysql.New(mysql.Config{
DSN: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name //डेटा स्रोत का नाम
DefaultStringSize: 256, // default size for string fields //स्ट्रिंग फ़ील्ड के लिए डिफ़ॉल्ट आकार
DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6 /डेटाटाइम परिशुद्धता अक्षम करें, जो MySQL 5.6 से पहले समर्थित नहीं है
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB //
निर्दिष्ट का नाम बदलने पर बनाएँ, MySQL 5.7, MariaDB से पहले संगति का नाम परिवर्तन नहीं होता है
DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
// // `परिवर्तन` जब स्तंभ का नाम बदलें, नाम बदलें कॉलम MySQL 8, MariaDB से पहले समर्थित नहीं है
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version //वर्तमान में MySQL संस्करण के आधार पर ऑटो कॉन्फ़िगर करें
}), &gorm.Config{})

ड्राइवर को अनुकूलित करें //Customize Driver

GORM MySQL ड्राइवर को DriverName विकल्प के साथ अनुकूलित करने की अनुमति देता है, उदाहरण के लिए:

import (
_ "example.com/my_mysql_driver"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

db, err := gorm.Open(mysql.New(mysql.Config{
DriverName: "my_mysql_driver",
DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name
}), &gorm.Config{})

Existing database connection //मौजूदा डेटाबेस कनेक्शन

GORM किसी मौजूदा डेटाबेस कनेक्शन के साथ *gorm.DB प्रारंभ करने की अनुमति देता है

import (
"database/sql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

sqlDB, err := sql.Open("mysql", "mydb_dsn")
gormDB, err := gorm.Open(mysql.New(mysql.Config{
Conn: sqlDB,
}), &gorm.Config{})

PostgreSQL

import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

हम pgx का उपयोग पोस्टग्रेज के डेटाबेस/एसक्यूएल ड्राइवर के रूप में कर रहे हैं, यह इसे निष्क्रिय करने के लिए डिफ़ॉल्ट रूप से तैयार स्टेटमेंट कैश को सक्षम करता है:

// https://github.com/go-gorm/postgres
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai",
PreferSimpleProtocol: true, // disables implicit prepared statement usage
}), &gorm.Config{})

Customize Driver

GORM उदाहरण के लिए, DriverName विकल्प के साथ PostgreSQL ड्राइवर को अनुकूलित करने की अनुमति देता है:

import (
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"gorm.io/gorm"
)

db, err := gorm.Open(postgres.New(postgres.Config{
DriverName: "cloudsqlpostgres",
DSN: "host=project:region:instance user=postgres dbname=postgres password=password sslmode=disable",
})

Existing database connection

GORM किसी मौजूदा डेटाबेस कनेक्शन के साथ *gorm.DB प्रारंभ करने की अनुमति देता है

import (
"database/sql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

sqlDB, err := sql.Open("pgx", "mydb_dsn")
gormDB, err := gorm.Open(postgres.New(postgres.Config{
Conn: sqlDB,
}), &gorm.Config{})

GaussDB

import (
"gorm.io/driver/gaussdb"
"gorm.io/gorm"
)

dsn := "host=localhost user=gorm password=gorm dbname=gorm port=8000 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(gaussdb.Open(dsn), &gorm.Config{})

We are using gaussdb-go as gaussdb’s database/sql driver, it enables prepared statement cache by default, to disable it:

// https://github.com/go-gorm/gaussdb
db, err := gorm.Open(gaussdb.New(gaussdb.Config{
DSN: "user=gorm password=gorm dbname=gorm port=8000 sslmode=disable TimeZone=Asia/Shanghai",
PreferSimpleProtocol: true, // disables implicit prepared statement usage
}), &gorm.Config{})

Customize Driver

GORM allows to customize the GaussDB driver with the DriverName option, for example:

import (
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/gaussdb"
"gorm.io/gorm"
)

db, err := gorm.Open(gaussdb.New(gaussdb.Config{
DriverName: "cloudsqlgaussdb",
DSN: "host=project:region:instance user=gaussdb dbname=gaussdb password=password sslmode=disable",
})

Existing database connection

GORM allows to initialize *gorm.DB with an existing database connection

import (
"database/sql"
"gorm.io/driver/gaussdb"
"gorm.io/gorm"
)

sqlDB, err := sql.Open("gaussdbgo", "mydb_dsn")
gormDB, err := gorm.Open(gaussdb.New(gaussdb.Config{
Conn: sqlDB,
}), &gorm.Config{})

Oracle Database

The GORM Driver for Oracle provides support for Oracle Database, enabling full compatibility with GORM’s ORM capabilities. It is built on top of the Go Driver for Oracle (Godror) and supports key features such as auto migrations, associations, transactions, and advanced querying.

Prerequisite: Install Instant Client

To use ODPI-C with Godror, you’ll need to install the Oracle Instant Client on your system. Follow the steps on this page to complete the installation.

After that, you can connect to the database using the dataSourceName, which specifies connection parameters (such as username and password) using a logfmt-encoded parameter list.

The way you specify the Instant Client directory differs by platform:

  • macOS and Windows: You can set the libDir parameter in the dataSourceName.
  • Linux: The libraries must be in the system library search path before your Go process starts, preferably configured with “ldconfig”. The libDir parameter does not work on Linux.

Example (macOS/Windows)

dataSourceName := `user="scott" password="tiger" 
connectString="dbhost:1521/orclpdb1"
libDir="/Path/to/your/instantclient_23_26"`

Example (Linux)

dataSourceName := `user="scott" password="tiger" 
connectString="dbhost:1521/orclpdb1"`

Getting Started

import (
"github.com/oracle-samples/gorm-oracle/oracle"
"gorm.io/gorm"
)

dataSourceName := `user="scott" password="tiger"
connectString="dbhost:1521/orclpdb1"`
db, err := gorm.Open(oracle.Open(dataSourceName), &gorm.Config{})

SQLite

import (
"gorm.io/driver/sqlite" // Sqlite driver based on CGO
// "github.com/glebarez/sqlite" // Pure go SQLite driver, checkout https://github.com/glebarez/sqlite for details
"gorm.io/gorm"
)

// github.com/mattn/go-sqlite3
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

Platinum Sponsors

Become a Sponsor!

Platinum Sponsors

Become a Sponsor!