登录
首页 >  Golang >  Go问答

创建 sqlite 表时 golang 出错

来源:stackoverflow

时间:2024-04-07 22:12:34 463浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《创建 sqlite 表时 golang 出错》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

所以我必须使用 golang 程序创建一个 sqlite 表,所以我这样做了:

package main

import (
    "database/sql"
    "log"

    _ "github.com/mattn/go-sqlite3"
)

func main() {
    database, err := sql.Open("sqlite3", "./ProjetForum.db")
    if err != nil {
        log.Fatal(err)
    }
    defer database.Close()
    statement, err := database.Prepare("CREATE TABLE IF NOT EXISTS Users (UserID integer NOT NULL primary key, Pseudo text NOT NULL , Email text NOT NULL , Rank integer NOT NULL DEFAULT 0, Phone text , Description text , ArrivingDate integer NOT NULL DEFAULT CURRENT_TIMESTAMP , Key text NOT NULL , Verified integer NOT NULL DEFAULT 0 , Banned integer NOT NULL DEFAULT 0 , Deleted integer NOT NULL DEFAULT 0 , Points integer NOT NULL DEFAULT 0 , Staff integer NOT NULL DEFAULT 0 , Image blob )")
    if err != nil {
        log.Fatal(err)
    }
    statement.Exec()
    statement, erro := database.Prepare("CREATE TABLE IF NOT EXISTS Posts (PostID integer NOT NULL primary key , Title text NOT NULL , Description text NOT NULL , PublishDate integer NOT NULL DEFAULT CURRENT_TIMESTAMP, Closed integer NOT NULL DEFAULT 0 , Verified integer NOT NULL DEFAULT 0 , ReviewID integer NOT NULL DEFAULT 0 , Deleted text NOT NULL , ToReview integer NOT NULL DEFAULT 0 , Points integer NOT NULL DEFAULT 0 , Likes integer NOT NULL DEFAULT 0 , Dislikes integer NOT NULL DEFAULT 0 , UserID integer NOT NULL DEFAULT 0, FOREIGN KEY (`UserID`) references Users(`UserID`)) ")
    if erro != nil {
        log.Fatal(erro)
    }
    statement.Exec()
}

第二个不想自行执行,第一个正在正确创建自身,但无法创建 post 表。


正确答案


您应该在执行 statement.Exec() 时检查错误,这会指出实际的错误。

问题在于您执行 CURRRENT_TIMESTAMP 而不是 CURRENT_TIMESTAMP (注意 CURRENTR 的数量),并且不使用 DEFAULT 关键字作为 ReviewIDzqben dczqb。

创建表的语法记录如下:https://www.sqlite.org/lang_createtable.html

本篇关于《创建 sqlite 表时 golang 出错》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>