英文:
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 R
s 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论