GO mit Führungsterminen angepasst
This commit is contained in:
77
GO/sternwarte_fdates/main/main.go
Normal file
77
GO/sternwarte_fdates/main/main.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package sendAllOff
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
/*
|
||||
* Tag... - a very simple struct
|
||||
*/
|
||||
type Tag struct {
|
||||
datetime string `json:"datetime"`
|
||||
}
|
||||
|
||||
func sendAllOff() {
|
||||
fmt.Println("Go MySQL Tutorial")
|
||||
|
||||
// Open up our database connection.
|
||||
// I've set up a database on my local machine using phpmyadmin.
|
||||
// The database is called testDb
|
||||
db, err := sql.Open("mysql", "root:SFluorit@tcp(127.0.0.1:3306)/sternwarte")
|
||||
|
||||
// if there is an error opening the connection, handle it
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
db.SetConnMaxLifetime(time.Minute * 3)
|
||||
db.SetMaxOpenConns(10)
|
||||
db.SetMaxIdleConns(10)
|
||||
|
||||
// defer the close till after the sendAllOff function has finished
|
||||
// executing
|
||||
defer db.Close()
|
||||
|
||||
// Execute the query
|
||||
results, err := db.Query("SELECT datetime FROM fdates")
|
||||
if err != nil {
|
||||
panic(err.Error()) // proper error handling instead of panic in your app
|
||||
}
|
||||
|
||||
for results.Next() {
|
||||
var tag Tag
|
||||
// for each row, scan the result into our tag composite object
|
||||
err = results.Scan(&tag.datetime)
|
||||
if err != nil {
|
||||
panic(err.Error()) // proper error handling instead of panic in your app
|
||||
}
|
||||
// datetime in das datum von fdatum umwandeln
|
||||
str := strings.Replace(tag.datetime[0:10], "-", "", -1)
|
||||
fmt.Println(str)
|
||||
// einen int draus machen
|
||||
i, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
// handle error
|
||||
fmt.Println(err)
|
||||
}
|
||||
// Update-query erzeugen
|
||||
query := fmt.Sprintf("UPDATE fdates SET datum='%d' WHERE datetime='%s'", i, tag.datetime)
|
||||
// update ausführen
|
||||
update, err := db.Query(query)
|
||||
|
||||
// if there is an error inserting, handle it
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
// be careful deferring Queries if you are using transactions
|
||||
update.Close()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user