douxun4860 2016-11-28 07:47
浏览 191

使用GoLang在具有json类型字段的Postgres中创建表

I am using GoLang in the back-end and PostgreSQL as a database. I'm new to PostgreSQL database connections with Go. I'm using Beego as a back-end. I want to create a table with one of a fields of JSON type using Golang database/sql package and lib/pq. That what I do

This is my create table query

createtable:= `CREATE TABLE apply_leave1 (
                leaveid serial PRIMARY KEY NOT NULL ,
                empid varchar(10) NOT NULL ,
                leavedays double precision NOT NULL DEFAULT 0 ,
                mdays double precision NOT NULL DEFAULT 0 ,
                leavetype varchar(20) NOT NULL DEFAULT '' ,
                daytype text NOT NULL '',
                leavefrom timestamp with time zone NOT NULL,
                leaveto timestamp with time zone NOT NULL,
                applieddate timestamp with time zone NOT NULL,
                leavestatus varchar(15) NOT NULL DEFAULT ''  ,
                resultdate timestamp with time zone,
                certificatestatus bool NOT NULL DEFAULT FALSE 
                certificate json NULL)`



    conn := fmt.Sprintf(
        "user=%s password=%s dbname=%s sslmode=disable",
        "postgres",
        "root",
        "employee")
    log.Println("Creating a new connection: %v", conn)

    db, err := sql.Open("postgres", conn)

    stmt, err1 := db.Prepare(createtable)

    defer stmt.Close()
    _, err = stmt.Exec()
    if err != nil {
        fmt.Println(err.Error()
    }

}

This is throwing me the following error

Handler crashed with error runtime error: invalid memory address or nil pointer dereference

But when I'm using query to select something from the table there is no problem with table creation query or other executed sql code. I appreciate any help. Thanks!

  • 写回答

1条回答 默认 最新

  • duanliaolan6178 2016-11-28 10:15
    关注

    I think that your problem is the invalid sql used to create the table, it should be:

    CREATE TABLE apply_leave1 (
        leaveid serial PRIMARY KEY NOT NULL ,
        empid varchar(10) NOT NULL ,
        leavedays double precision NOT NULL DEFAULT 0 ,
        mdays double precision NOT NULL DEFAULT 0 ,
        leavetype varchar(20) NOT NULL DEFAULT '' ,
        daytype text NOT NULL DEFAULT '',
        leavefrom timestamp with time zone NOT NULL,
        leaveto timestamp with time zone NOT NULL,
        applieddate timestamp with time zone NOT NULL,
        leavestatus varchar(15) NOT NULL DEFAULT ''  ,
        resultdate timestamp with time zone,
        certificatestatus bool NOT NULL DEFAULT FALSE 
        certificate json NULL)
    

    You miss the DEFAULT for daytype column. Also, you're not catching correctly the errors, because the func leave() can be executed totally before send a response. You can use a return when you find an error.

    db, err := sql.Open("postgres", conn)
    if err != nil {
        fmt.Println(err.Error())
        e.Data["json"] = err.Error()
        e.ServeJSON()
        return
    }
    

    Also, you're creating a connection and not closing it at the end of the func.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘