You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/05.3.md
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -15,16 +15,16 @@ The first driver is the only one that supports the `database/sql` interface stan
15
15
## Samples
16
16
17
17
We create the following SQL:
18
-
18
+
```sql
19
19
CREATETABLE `userinfo` (
20
20
`uid`INTEGERPRIMARY KEY AUTOINCREMENT,
21
21
`username`VARCHAR(64) NULL,
22
22
`departname`VARCHAR(64) NULL,
23
23
`created`DATENULL
24
24
);
25
-
25
+
```
26
26
An example:
27
-
27
+
```Go
28
28
package main
29
29
30
30
import (
@@ -101,11 +101,11 @@ An example:
101
101
panic(err)
102
102
}
103
103
}
104
-
104
+
```
105
105
You may have noticed that the code is almost the same as in the previous section, and that we only changed the name of the registered driver and called `sql.Open` to connect to SQLite in a different way.
106
106
107
107
Note that sometimes you can't use the `for` statement because you don't have more than one row, then you can use the `if` statement
@@ -114,14 +114,14 @@ Note that sometimes you can't use the `for` statement because you don't have mor
114
114
fmt.Println(department)
115
115
fmt.Println(created)
116
116
}
117
-
117
+
```
118
118
Also you have to do a `rows.Next()`, without using that you can't fetch data in the `Scan` function.
119
119
120
120
Transactions
121
121
===============
122
122
123
123
The above example shows how you fetch data from the database, but when you want to write a web application then it will not only be necessary to fetch data from the db but it will also be required to write data into it. For that purpose, you should use transactions because for various reasons, such as having multiple go routines which access the database, the database might get locked. This is undesirable in your web application and the use of transactions is effective in ensuring your database activities either pass or fail completely depending on circumstances. It is clear that using transactions can prevent a lot of things from going wrong with the web app.
124
-
124
+
```Go
125
125
trashSQL, err:= database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
126
126
if err != nil {
127
127
fmt.Println(err)
@@ -137,7 +137,7 @@ The above example shows how you fetch data from the database, but when you want
137
137
} else {
138
138
tx.Commit()
139
139
}
140
-
140
+
```
141
141
As it is clear from the above block of code, you first prepare a statement, after which you execute it, depending on the output of that execution then you either roll it back or commit it.
`beedb.New()` actually has two arguments. The first is the database object, and the second is for indicating which database engine you're using. If you're using MySQL/SQLite, you can just skip the second argument.
51
51
52
52
Otherwise, this argument must be supplied. For instance, in the case of SQLServer:
53
-
53
+
```Go
54
54
orm = beedb.New(db, "mssql")
55
-
55
+
```
56
56
PostgreSQL:
57
-
57
+
```Go
58
58
orm = beedb.New(db, "pg")
59
-
59
+
```
60
60
beedb supports debugging. Use the following code to enable it:
61
-
61
+
```Go
62
62
beedb.OnDebug=true
63
-
63
+
```
64
64
Next, we have a struct for the `Userinfo` database table that we used in previous sections.
65
-
65
+
```Go
66
66
typeUserinfostruct {
67
67
Uidint`PK`// if the primary key is not id, you need to add tag `PK` for your customized primary key.
68
68
Usernamestring
69
69
Departnamestring
70
70
Created time.Time
71
71
}
72
-
72
+
```
73
73
Be aware that beedb auto-converts camelcase names to lower snake case. For example, if we have `UserInfo` as the struct name, beedb will convert it to `user_info` in the database. The same rule applies to struct field names.
74
74
75
75
76
76
## Insert data
77
77
78
78
The following example shows you how to use beedb to save a struct, instead of using raw SQL commands. We use the beedb Save method to apply the change.
79
-
79
+
```Go
80
80
varsaveoneUserinfo
81
81
saveone.Username = "Test Add User"
82
82
saveone.Departname = "Test Add Departname"
83
83
saveone.Created = time.Now()
84
84
orm.Save(&saveone)
85
-
85
+
```
86
86
You can check `saveone.Uid` after the record is inserted; its value is a self-incremented ID, which the Save method takes care of for you.
87
87
88
88
beedb provides another way of inserting data; this is via Go's map type.
89
-
89
+
```Go
90
90
add:=make(map[string]interface{})
91
91
add["username"] = "astaxie"
92
92
add["departname"] = "cloud develop"
93
93
add["created"] = "2012-12-02"
94
94
orm.SetTable("userinfo").Insert(add)
95
-
95
+
```
96
96
Insert multiple data:
97
-
97
+
```Go
98
98
addslice:=make([]map[string]interface{}, 10)
99
99
add:=make(map[string]interface{})
100
100
add2:=make(map[string]interface{})
@@ -106,26 +106,26 @@ Insert multiple data:
106
106
add2["created"] = "2012-12-02"
107
107
addslice = append(addslice, add, add2)
108
108
orm.SetTable("userinfo").InsertBatch(addslice)
109
-
109
+
```
110
110
The method shown above is similar to a chained query, which you should be familiar with if you've ever used jquery. It returns the original ORM object after calls, then continues doing other jobs.
111
111
112
112
The method `SetTable` tells the ORM we want to insert our data into the `userinfo` table.
113
113
114
114
## Update data
115
115
116
116
Let's continue working with the above example to see how to update data. Now that we have the primary key of saveone(Uid), beedb executes an update operation instead of inserting a new record.
117
-
117
+
```Go
118
118
saveone.Username = "Update Username"
119
119
saveone.Departname = "Update Departname"
120
120
saveone.Created = time.Now()
121
121
orm.Save(&saveone) // update
122
-
122
+
```
123
123
Like before, you can also use map for updating data:
Let's see how to use the driver that I forked to operate on a database:
101
-
101
+
```Go
102
102
package main
103
103
104
104
import (
@@ -129,7 +129,7 @@ Let's see how to use the driver that I forked to operate on a database:
129
129
}
130
130
client.Del("l")
131
131
}
132
-
132
+
```
133
133
We can see that it is quite easy to operate redis in Go, and it has high performance. It's client commands are almost the same as redis' built-in commands.
0 commit comments