英文:
SQL logic error: near "RETURNING": syntax error (1) Go and SQLite
问题
我正在创建一个CRUD应用程序,并使用内存数据库对数据库进行测试。
唯一出现问题的测试和函数是toggleTask函数和测试。
函数:
func (r *Repository) ToggleTask(task Task) (Task, error) {
query := "UPDATE Tasks SET completed = NOT completed WHERE id = (?)"
_, err := r.db.Exec(query, task.ID)
if err != nil {
return task, err
}
query = "SELECT id, txt, completed FROM Tasks WHERE id = (?) RETURNING *"
err = r.db.QueryRow(query, task.ID).Scan(&task.ID, &task.Text, &task.ListID, &task.Completed)
if err != nil {
return task, err
}
return task, nil
}
测试:
const (
ToggleTask = "SELECT id, txt, completed FROM Tasks WHERE id = (?) RETURNING *"
)
func TestToggleTask(t *testing.T) {
repo := mockDbRepo()
list := List{Name: "Test List"}
repo.db.Exec(CreateList, list.Name)
task := Task{Text: "Test Task", ListID: list.ID}
repo.db.Exec(CreateTask, task.Text, task.ListID)
completedTask, err := repo.ToggleTask(task)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(completedTask, task) {
t.Errorf("Expected %v, got %v", task, completedTask)
}
}
测试返回给我:
SQL逻辑错误:在"RETURNING"附近:语法错误(1)
英文:
I'm creating a crud application and I'm testing the database with an in-memory DB.
The only test and functions that are breaking are the toggleTask func and test.
function:
func (r *Repository) ToggleTask(task Task) (Task, error) {
query := "UPDATE Tasks SET completed = NOT completed WHERE id = (?)"
_, err := r.db.Exec(query, task.ID)
if err != nil {
return task, err
}
query = "SELECT id, txt, completed FROM Tasks WHERE id = (?) RETURNING *"
err = r.db.QueryRow(query, task.ID).Scan(&task.ID, &task.Text, &task.ListID, &task.Completed)
if err != nil {
return task, err
}
return task, nil
}
test:
const (
ToggleTask = "SELECT id, txt, completed FROM Tasks WHERE id = (?) RETURNING *"
)
func TestToggleTask(t *testing.T) {
repo := mockDbRepo()
list := List{Name: "Test List"}
repo.db.Exec(CreateList, list.Name)
task := Task{Text: "Test Task", ListID: list.ID}
repo.db.Exec(CreateTask, task.Text, task.ListID)
completedTask,err := repo.ToggleTask(task)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(completedTask, task) {
t.Errorf("Expected %v, got %v", task, completedTask)
}
}
The test returns to me:
> SQL logic error: near "RETURNING": syntax error (1)
答案1
得分: 1
你的SQL查询语句是错误的。根据SQL Lite文档的说明,SELECT
语句不接受RETURNING
子句。SELECT
子句本身就返回数据。
在你的查询中删除RETURNING
子句,应该就可以了。
SQL Lite参考链接:https://www.sqlite.org/lang_select.html
英文:
Your SQL query is incorrect. As stated in the SQL Lite docs, SELECT
does not accept a RETURNING
clause. The SELECT
clause by itself returns data.
Remove your RETURNING
clauses in your queries and you should be good.
SQL Lite Ref: https://www.sqlite.org/lang_select.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论