Insert to tableA & tableB one procesig with golang mysql

help insert tableA & B to database mysql one proces golang??

‘’’
package main

import (
“context”
“database/sql”
“fmt”
“log”
“time”

_ "github.com/go-sql-driver/mysql"

)

const (
username = “root”
password = “root”
hostname = “localhost:3306”
dbname = “ecommerce”
)

func dsn(dbName string) string {
return fmt.Sprintf("%s:%s@tcp(%s)/%s", username, password, hostname, dbName)
}

func dbConnection() (*sql.DB, error) {
db, err := sql.Open(“mysql”, dsn(""))
if err != nil {
log.Printf(“Error %s when opening DB\n”, err)
return nil, err
}
//defer db.Close()

ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelfunc()
res, err := db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS "+dbname)
if err != nil {
    log.Printf("Error %s when creating DB\n", err)
    return nil, err
}
no, err := res.RowsAffected()
if err != nil {
    log.Printf("Error %s when fetching rows", err)
    return nil, err
}
log.Printf("rows affected %d\n", no)

db.Close()
db, err = sql.Open("mysql", dsn(dbname))
if err != nil {
    log.Printf("Error %s when opening DB", err)
    return nil, err
}
//defer db.Close()

db.SetMaxOpenConns(20)
db.SetMaxIdleConns(20)
db.SetConnMaxLifetime(time.Minute * 5)

ctx, cancelfunc = context.WithTimeout(context.Background(), 5*time.Second)
defer cancelfunc()
err = db.PingContext(ctx)
if err != nil {
    log.Printf("Errors %s pinging DB", err)
    return nil, err
}
log.Printf("Connected to DB %s successfully\n", dbname)
return db, nil

}

func createProductTable(db sql.DB) error {
query := CREATE TABLE IF NOT EXISTS product(product_id int primary key auto_increment, product_name text, product_price int, created_at datetime default CURRENT_TIMESTAMP, updated_at datetime default CURRENT_TIMESTAMP)
ctx, cancelfunc := context.WithTimeout(context.Background(), 5
time.Second)
defer cancelfunc()
res, err := db.ExecContext(ctx, query)
if err != nil {
log.Printf(“Error %s when creating product table”, err)
return err
}
rows, err := res.RowsAffected()
if err != nil {
log.Printf(“Error %s when getting rows affected”, err)
return err
}
log.Printf(“Rows affected when creating table: %d”, rows)
return nil
}

func main() {
db, err := dbConnection()
if err != nil {
log.Printf(“Error %s when getting db connection”, err)
return
}
defer db.Close()
log.Printf(“Successfully connected to database”)
err = createProductTable(db)
if err != nil {
log.Printf(“Create product table failed with error %s”, err)
return
}
}
‘’’

What exactly is your problem? Do you have any errors running your code, or does the data not appear as you think it should or what exactly is it you have problems with?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.