GO mit Führungsterminen angepasst

This commit is contained in:
rxf
2025-10-01 15:25:14 +02:00
parent 4214f48e34
commit 45b76cf1b2
8 changed files with 286 additions and 0 deletions

View 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()
}
}