SQL逻辑错误:附近有“RETURNING”:语法错误(1)Go和SQLite

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

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

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

发表评论

匿名网友

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

确定