# 使用 ORM

主要是方便我們在不同資料庫時有統一的操作模式，以及預防一些安全性問題。

## xorm

{% embed url="<https://github.com/go-xorm/xorm>" %}

以下我們已 SQLite 3 為範例

> 記得要安裝對應的資料庫 driver

```
go get github.com/go-xorm/xorm
go get github.com/mattn/go-sqlite3
```

建立 Table

```go
package main

import (
	"fmt"
	"time"

	"github.com/go-xorm/xorm"
	_ "github.com/mattn/go-sqlite3"
)

// User describes a user
type User struct {
	Id      int64
	Name    string
	Created time.Time `xorm:"created"`
	Updated time.Time `xorm:"updated"`
}

func main() {
	f := "conversion.db"

	orm, err := xorm.NewEngine("sqlite3", f)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	 err = orm.CreateTables(&User{})
	 if err != nil {
	 	fmt.Println(err)
	 	return
	 }
}
```

## 新增

```go
	_, err = orm.Insert(&User{Id: 1, Name: "xlw"})
	if err != nil {
		fmt.Println(err)
		return
	}
```

## 查詢

```go
	users := make([]User, 0)
	err = orm.Find(&users)
	if err != nil {
		fmt.Println(err)
		return
	}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/golang/databasecao-zuo/shi-yong-orm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
