MCPcopy Create free account
hub / github.com/kataras/iris / main

Function main

_examples/database/orm/sqlx/main.go:39–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

37);`
38
39func main() {
40 app := iris.New()
41
42 db, err := sqlx.Connect("sqlite3", "./test.db")
43 if err != nil {
44 app.Logger().Fatalf("db failed to initialized: %v", err)
45 }
46 iris.RegisterOnInterrupt(func() {
47 db.Close()
48 })
49
50 db.MustExec(schema)
51
52 app.Get("/insert", func(ctx iris.Context) {
53 res, err := db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
54 map[string]interface{}{
55 "first": "John",
56 "last": "Doe",
57 "email": "johndoe@example.com",
58 })
59
60 if err != nil {
61 // Note: on production, don't give the error back to the user.
62 // However for the sake of the example we do:
63 ctx.StopWithError(iris.StatusInternalServerError, err)
64 return
65 }
66
67 id, err := res.LastInsertId()
68 if err != nil {
69 ctx.StopWithError(iris.StatusInternalServerError, err)
70 return
71 }
72
73 ctx.Writef("person inserted: id: %d", id)
74 })
75
76 app.Get("/get", func(ctx iris.Context) {
77 // Select all persons.
78 people := []Person{}
79 db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
80 if err != nil {
81 ctx.StopWithError(iris.StatusInternalServerError, err)
82 return
83 }
84
85 if len(people) == 0 {
86 ctx.Writef("no persons found, use /insert first.")
87 return
88 }
89
90 ctx.Writef("persons found: %#v", people)
91 /* Select a single or more with a first name of John from the database:
92 person := Person{FirstName: "John"}
93 rows, err := db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, person)
94 if err != nil { ... }
95 defer rows.Close()
96 for rows.Next() {

Callers

nothing calls this directly

Calls 10

NewMethod · 0.80
FatalfMethod · 0.80
StopWithErrorMethod · 0.80
WritefMethod · 0.80
ConnectMethod · 0.65
LoggerMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
SelectMethod · 0.65
ListenMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…