doucu9677 2018-03-26 10:43
浏览 45

从postgresql中选择并插入mysql golang中是否不存在

I have two different database servers, one is a mysql database server and the other is postgresql database server , both have a table called users so I have to insert from the users postgresql table to the users mysql table and check if there is already something inserted (if not exist), how can I do one Database query with postgresql and mysql in the same time

//MYSQL CONNECTION

var err1 error
db, err1 = sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/dbusers?parseTime=true")
if err1 != nil {
    log.Fatal(err1)
}

//POSTGRESQL CONNECTION

psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable", HOST, PORT, USER, PASSWORD, DBNAME)
    db1, err2 = sql.Open("postgres", psqlInfo)
    if err2 != nil {
        log.Fatal(err2)
    }

//Here is what i have tried

func GetUsersApiPostgres(c *gin.Context) {
var p Users
Users, err := p.GetUsers() //GetUsers will return all fields in users table
if err != nil {
    log.Fatalln(err)
}

c.JSON(http.StatusOK, gin.H{
    "Users": Users,
})

}
func AddUsersApiMysql(c *gin.Context) {
firstName := c.Request.FormValue("firstname")
lastName := c.Request.FormValue("lastname")

p := Users{FirstName: firstName, LastName: lastName}

ra, err := p.AddUsers()//AddUsers will insert fields  defined in users table
if err != nil {
    log.Fatalln(err)
}
msg := fmt.Sprintf("insert successful %d", ra)
c.JSON(http.StatusOK, gin.H{
    "msg": msg,
})
}
func (u *Users) GetMysqlUsersFromPostgresql{

}

I don't know how to use both function (Add & Get) in the last function

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB动图的问题
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名