gorm.open mysql_golang数据库操作之gorm

gorm快速上手 Quick start

数据库连接

1.Sqlite数据库

package main

import (

"github.com/jinzhu/gorm"

_ "github.com/jinzhu/gorm/dialects/sqlite"

)

func main() {

//打开数据库连接

var db, _ = gorm.Open("sqlite3", "demo.db")

//关闭数据库连接

defer db.Close()

}

2.MySQL数据库

数据库连接字符串格式即DSN (Data Source Name)格式为:

[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]

package main

import (

"github.com/jinzhu/gorm"

_ "github.com/jinzhu/gorm/dialects/mysql"

)

func main() {

//打开数据库连接

db, _ := gorm.Open("mysql", "demo:123456@(localhost)/demo?charset=utf8&parseTime=True&loc=Local")

//关闭数据库连接

defer d

你可能感兴趣的:(gorm.open,mysql)