🚧 working on get questions endpoint
Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
parent
87a2914573
commit
191404a9ee
3 changed files with 71 additions and 7 deletions
51
backend/db/insert_user_data.go
Normal file
51
backend/db/insert_user_data.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func InsertUserData(age string, education string, healthcarepersonell bool, gender string) (int, error) {
|
||||
// Connection string
|
||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
|
||||
"password=%s dbname=%s sslmode=disable",
|
||||
host, port, user, password, dbname)
|
||||
|
||||
// Connect to the database
|
||||
db, err := sql.Open("postgres", psqlInfo)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening database: %v\n", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Check the connection
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Error connecting to the database: %v\n", err)
|
||||
}
|
||||
|
||||
insertStatement := `
|
||||
INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING respondentID
|
||||
`
|
||||
|
||||
stmt, err := db.Prepare(insertStatement)
|
||||
if err != nil {
|
||||
log.Fatalf("Error preparing statement: %v\n", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
var respondentID int
|
||||
err = stmt.QueryRow(age, education, healthcarepersonell, gender).Scan(&respondentID)
|
||||
if err != nil {
|
||||
log.Fatalf("Error inserting userdata: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Data inserted successfully with respondentID: %d\n", respondentID)
|
||||
|
||||
return respondentID, nil
|
||||
}
|
Reference in a new issue