mysql - dodawanie i pobieranie uzytkownikow dziala

This commit is contained in:
pawlaczyk 2019-01-06 02:23:35 +01:00
parent 63c0fc8d54
commit d0849206c1
2 changed files with 31 additions and 23 deletions

Binary file not shown.

View File

@ -16,29 +16,36 @@ func connectMysql() (*sql.DB, error) {
return db, nil
}
// func addUser(login string, password string, userDescription string, points int) {
// //do rejestracji uzytkownika
// // Insert do bazy Mysql Nowego użytkownika
func addUser(_login string, _password string, _userDescription string, _points int) {
//do rejestracji uzytkownika
// Insert do bazy Mysql Nowego użytkownika
// db, err = connectMysql()
// if err != nil {
// panic(err.Error())
// }
// queryInsert := fmt.Sprintf(`INSERT INTO golang_user (
// login,
// password,
// userDescription,
// points
// )VALUES (
// %s,
// %s,
// %s,
// %d
// )`, login, password, userDescription, 0)
db, err := connectMysql()
// insert, err := db.Query(queryInsert)
if err != nil {
panic(err.Error())
}
// }
queryInsert := fmt.Sprintf(`INSERT INTO users (
login,
password,
userDescription,
points
)VALUES (
"%s",
"%s",
"%s",
"%d"
)`, _login, _password, _userDescription, _points)
fmt.Printf(queryInsert)
insert, err := db.Query(queryInsert)
if err != nil {
panic(err.Error())
}
fmt.Println(insert)
}
func getAllUsers() []User {
//pobiera wszystkich uzytkownikow - do wysiwetlenie na front
@ -60,16 +67,14 @@ func getAllUsers() []User {
for result.Next() {
var user User
err = result.Scan(&user.userId, user.login, user.userDescription, user.points)
err = result.Scan(&user.userId, &user.login, &user.password, &user.userDescription, &user.points)
if err != nil {
panic(err.Error())
}
fmt.Println(user.userId, user.login)
allUsers = append(allUsers, user)
}
fmt.Println(allUsers)
return allUsers
}
@ -81,6 +86,9 @@ func main() {
// // db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/golang_user")
// fmt.Println(reflect.TypeOf(db))
// fmt.Println(reflect.TypeOf(err))
addUser("login10", "password", "userDescription", 123)
allUsers := getAllUsers()
fmt.Println(allUsers)
}