-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTg-databaseIO.go
46 lines (35 loc) · 894 Bytes
/
Tg-databaseIO.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
/*
import (
"fmt"
"strconv"
_ "github.com/go-sql-driver/mysql"
)
type Picture struct {
pictureId int `json:"pictureid"`
pictureAddress string `json:"pictureaddress"`
}
type Pictures []Picture
func (db *MyDB) getPictures() (pictures Pictures, err error) {
// Declaration
// prepare SQL statement.
var s = fmt.Sprintf(
`SELECT pictureId, pictureAddress FROM votingsystem.pictures`)
rows, err := db.Query(s)
if err != nil {
return pictures, err
}
for rows.Next() {
// Fill the response here.
var picture Picture
err = rows.Scan(&picture.pictureId, &picture.pictureAddress)
// Error handling
if err != nil {
return pictures, err
}
fmt.Println("Id: " + strconv.Itoa(picture.pictureId) + ", Address: " + picture.pictureAddress)
pictures = append(pictures, picture)
}
// If no error, return list of pictures
return pictures, nil
}