创建SQLite表时出现了Golang错误。

huangapple go评论86阅读模式
英文:

Error with golang while creating sqlite table

问题

所以我必须使用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表。

英文:

So I have to create a sqlite table with a golang program so I did this:

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()
}

And the second one does not want to execute it self the first one is creating itself correctly but the Post table can't be created.

答案1

得分: 0

你在执行statement.Exec()时应该检查错误,因为这会指向实际的错误。

问题出在你使用了CURRRENT_TIMESTAMP而不是CURRENT_TIMESTAMP(注意CURRENT中的R的数量),以及没有使用DEFAULT关键字为ReviewID

创建表的语法在这里有文档记录:https://www.sqlite.org/lang_createtable.html

英文:

You should check for errors when doing statement.Exec() as well as that would've pointed you to the actual error.

The problem lies where you do CURRRENT_TIMESTAMP instead of CURRENT_TIMESTAMP (note number of Rs in CURRENT) and not using the DEFAULT keyword for ReviewID.

The syntax for creating a table is documented here: https://www.sqlite.org/lang_createtable.html

huangapple
  • 本文由 发表于 2021年6月14日 16:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/67966785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定