Help us understand the problem. What is going on with this article?

dbr: simple orm for golang

More than 3 years have passed since last update.

memo

package main

import (
    "database/sql"
    "fmt"
    "log"

    _ "github.com/go-sql-driver/mysql"
    "github.com/gocraft/dbr"
)

const (
    mysqlDSN = "interop:interop@tcp(localhost:3306)/interop2016"
)

type DoorMeta struct {
    Id        int64        `db:"id"`
    Name      string       `db:"name"`
    Value     string       `db: "value"`
    CreatedAt dbr.NullTime `db: "created_at"`
}

func main() {
    var result sql.Result
    var err error

    // Open connection
    conn, _ := dbr.Open("mysql", mysqlDSN, nil)
    // Create session
    sess := conn.NewSession(nil)

    // Insert
    result, err = sess.InsertInto("door_meta").
        Columns("name", "value").
        Values("foo2", "bar").
        Exec()

    if err != nil {
        log.Fatal(err)
    } else {
        count, _ := result.RowsAffected()
        fmt.Println(count)
    }

    // Insert with struct
    meta := &DoorMeta{Name: "foofoo", Value: "barbar"}
    sess.InsertInto("door_meta").
        Columns("name", "value").
        Record(meta). // Watch out! this is not Values
        Exec()

    // Select
    var m []DoorMeta
    sess.Select("*").From("door_meta").Load(&m)

    fmt.Println(m)

    // Update
    result, err = sess.Update("door_meta").
        Set("name", "giwa").
        Where("name = ?", "foo").
        Exec()

    if err != nil {
        log.Fatal(err)
    } else {
        count, _ := result.RowsAffected()
        fmt.Println(count)
    }

    fmt.Println(m[0].CreatedAt.Time)
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした