Golang | SQLite无法创建数据库 -> 语法错误

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

Golang | SQLite cant create Database -> Syntax Error

问题

我正在尝试使用Golang创建一个SQLite数据库,用于程序的本地安装。但是我的问题是,由于某种原因它似乎无法工作,但我不知道原因。每当我想用SQL查询(CREATE DATABASE [name])创建一个数据库时,它都会说语法无效。而且由于SQLite似乎不存在这个查询,我尝试只创建表,但是然后我会得到一个错误,说我使用了一个未知的数据库。我已经尝试了很多方法,但似乎没有什么起作用。起初我以为可能是因为在创建/打开数据库连接之前执行了查询,所以错误发生了。但是现在我可以确定这不是问题,因为我尝试在执行SQL查询之前睡眠了一段时间,但没有任何改变。当我运行这段代码时,我得到的错误信息如下:

panic: near "DATABASE": syntax error

goroutine 1 [running]:
git.lambya.com/lucwol/coreflare/database.checkError(...)
        /home/lambya/Dev/piflare/coreflare/database/db.go:37
git.lambya.com/lucwol/coreflare/database.deploy()
        /home/lambya/Dev/piflare/coreflare/database/db.go:28 +0xdc
git.lambya.com/lucwol/coreflare/database.Connect({0x832adf, 0x9})
        /home/lambya/Dev/piflare/coreflare/database/db.go:23 +0xbb
main.main()
        /home/lambya/Dev/piflare/coreflare/main.go:102 +0x29
exit status 2

谢谢您的支持 Golang | SQLite无法创建数据库 -> 语法错误

以下是代码:

package database

import (
	"database/sql"
	"os"
	"time"

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

var DB *sql.DB

func Connect(path string) {
	if _, err := os.Stat(path); err != nil {
		file, err := os.Create(path)
		checkError(err)
		file.Close()
	}
	db, err := sql.Open("sqlite3", path)
	checkError(err)
	time.Sleep(time.Millisecond * 2000)
	DB = db
	deploy()
}

func deploy() {
	query, err := DB.Prepare("CREATE DATABASE test")
	checkError(err)
	query.Exec()
	query2, err := DB.Prepare("CREATE TABLE test.user(id int NOT NULL AUTO_INCREMENT, name varchar(50), PRIMARY_KEY(id))")
	checkError(err)
	query2.Exec()
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}
英文:

i am trying to create an SQLite Database with Golang for local Installations of the program. But my problem now is, that for some reason it dosent seem to work, but i have no idea why. It always says that when i want to create an Database with an SQL Query (CREATE DATABASE [name]) it says the syntax is invalid. And since this query dosent seem to exist for SQLite i tried only creating the Table, but then i get an Error that i use an unknown database. I have tried many things now but nothing seems to work. First i thought that maybe the error occurs bc the queries are executed before an Database Connection was created/open. But thats now 100% not the case bc i tried sleeping before the sql queries are executed, what changed nothing. The error i get when i run this code:

panic: near "DATABASE": syntax error

goroutine 1 [running]:
git.lambya.com/lucwol/coreflare/database.checkError(...)
        /home/lambya/Dev/piflare/coreflare/database/db.go:37
git.lambya.com/lucwol/coreflare/database.deploy()
        /home/lambya/Dev/piflare/coreflare/database/db.go:28 +0xdc
git.lambya.com/lucwol/coreflare/database.Connect({0x832adf, 0x9})
        /home/lambya/Dev/piflare/coreflare/database/db.go:23 +0xbb
main.main()
        /home/lambya/Dev/piflare/coreflare/main.go:102 +0x29
exit status 2

Thanks for the support Golang | SQLite无法创建数据库 -> 语法错误

Heres the code.

package database

import (
	"database/sql"
	"os"
	"time"

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

var DB *sql.DB

func Connect(path string) {
	if _, err := os.Stat(path); err != nil {
		file, err := os.Create(path)
		checkError(err)
		file.Close()
	}
	db, err := sql.Open("sqlite3", path)
	checkError(err)
	time.Sleep(time.Millisecond * 2000)
	DB = db
	deploy()
}

func deploy() {
	query, err := DB.Prepare("CREATE DATABASE test")
	checkError(err)
	query.Exec()
	query2, err := DB.Prepare("CREATE TABLE test.user(id int NOT NULL AUTO_INCREMENT, name varchar(50), PRIMARY_KEY(id))")
	checkError(err)
	query2.Exec()
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

答案1

得分: 0

希望这能帮到你

  1. 当你创建文件本身时,使用os.create(path)创建的数据库名称
  2. AUTOINCREMENT关键字是一个单词
  3. 如果你想将user.id设置为主键,它应该是整数类型
package main

import (
	"database/sql"
	_ "github.com/mattn/go-sqlite3" // 导入 go-sqlite3 库
	"os"
	"time"
)

var DB *sql.DB

func Connect(path string) {
	if _, err := os.Stat(path); err != nil {
		file, err := os.Create(path)
		checkError(err)
		file.Close()
	}
	db, err := sql.Open("sqlite3", path)
	checkError(err)
	time.Sleep(time.Millisecond * 2000)
	DB = db
	deploy()
}

func deploy() {
	query2, err := DB.Prepare("CREATE TABLE user(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name varchar(50) )")
	checkError(err)
	query2.Exec()
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	Connect("./sample.db")
}
英文:

Hope this helps

  1. the database name created with os.create(path) when you create the file itself

  2. AUTOINCREMENT key is one word

  3. if you want to make user.id primary key it should be integer

    package main
    
    import (
    	"database/sql"
    	_ "github.com/mattn/go-sqlite3" // Import go-sqlite3 library
    	"os"
    	"time"
    )
    
    var DB *sql.DB
    
    func Connect(path string) {
    	if _, err := os.Stat(path); err != nil {
    		file, err := os.Create(path)
    		checkError(err)
    		file.Close()
    	}
    	db, err := sql.Open("sqlite3", path)
    	checkError(err)
    	time.Sleep(time.Millisecond * 2000)
    	DB = db
    	deploy()
    }
    func deploy() {
    	query2, err := DB.Prepare("CREATE TABLE user(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name varchar(50) )")
    	checkError(err)
    	query2.Exec()
    }
    
    func checkError(err error) {
    	if err != nil {
    		panic(err)
    	}
    }
    
    func main() {
    	Connect("./sample.db")
    }
    

huangapple
  • 本文由 发表于 2022年5月8日 22:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/72161939.html
匿名

发表评论

匿名网友

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

确定