[英]What does the "SQLite3::SQLException: near ".": syntax error" mean?
I'm writing an ORM (object relational model) and I've been stuck on this error message for a while now.我正在编写一个 ORM(对象关系模型)并且我已经被这个错误消息困住了一段时间。 I did narrow it down to the update and the save method (I have a feeling that the error most likely is in the update method specifically), but I can't figure out where the syntax error is happening.
我确实将其缩小到更新和保存方法(我感觉错误最有可能特别是在更新方法中),但我无法弄清楚语法错误发生在哪里。
Here is the error message:这是错误消息:
SQLite3::SQLException: near ".": syntax error
from /Users/karenlee/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/sqlite3-1.4.1/lib/sqlite3/database.rb:147:in `initialize'
And here is my ORM:这是我的 ORM:
require_relative 'questions_database'
class User
attr_reader :id
attr_accessor :fname, :lname
# displays all users
def self.all
users = QuestionsDatabase.instance.execute("SELECT * FROM users")
users.map { | user_info | User.new(user_info) }
end
# finds a user by their primary id
def self.find_by_id(id)
found_user = QuestionsDatabase.instance.execute(<<-SQL, id: id)
SELECT
users.*
FROM
users
WHERE
users.id = :id
SQL
found_user.nil? ? nil : User.new(found_user.first)
end
# finds a user by their first and last name
def self.find_by_name(fname, lname)
name = {fname: fname, lname: lname}
found_user = QuestionsDatabase.instance.execute(<<-SQL, name)
SELECT
users.*
FROM
users
WHERE
users.fname = :fname AND users.lname = :lname
SQL
found_user.nil? ? nil : User.new(found_user.first)
end
# creates a new user instance
def initialize(options)
@id, @fname, @lname = options.values_at('id', 'fname', 'lname')
end
# saves the user into the database (or updates when needed)
def save
if self.id
update
else
create
end
end
private
# helper method to update the database
def update
values = {id: id, fname: fname, lname: lname}
QuestionsDatabase.instance.execute(<<-SQL, values)
UPDATE
users
SET
users.fname = :fname, users.lname = :lname
WHERE
users.id = :id
SQL
end
# helper method to create a row in the database
def create
name = {fname: fname, lname: lname}
QuestionsDatabase.instance.execute(<<-SQL, name)
INSERT INTO
users (fname, lname)
VALUES
(:fname, :lname)
SQL
self.id = QuestionsDatabase.instance.last_insert_row_id
end
end
Here's the QuestionsDatabase class if you need it:如果需要,这里是 QuestionsDatabase 类:
require 'sqlite3'
require 'singleton'
class QuestionsDatabase < SQLite3::Database
include Singleton
def initialize
super('questions.db')
self.type_translation = true
self.results_as_hash = true
end
end
UPDATE:更新:
I've found where my error was!我找到了我的错误所在! It turned out that I should have just been saying
fname = :fname, lname = :lname
instead of saying users.fname = :fname, users.lname = :lname
.原来我应该说
fname = :fname, lname = :lname
而不是users.fname = :fname, users.lname = :lname
。 But I'm still unsure as to why this is.但我仍然不确定为什么会这样。 It seems like the update statement doesn't like it when I don't use aliases, but some more explanation on this would be great!
当我不使用别名时,更新语句似乎不喜欢它,但是对此进行更多解释会很棒!
It turns out that aliases are only available in the FROM clause.事实证明,别名仅在 FROM 子句中可用。 So if I were to use aliases in the UPDATE statement, it would have to be structured differently;
因此,如果我要在 UPDATE 语句中使用别名,则它的结构必须有所不同; I'd have to use a FROM clause.
我必须使用 FROM 子句。 See this link for more info on restructuring!
有关重组的更多信息,请参阅此链接!
https://sqlstudies.com/2013/09/16/dba-myths-you-cant-use-an-alias-in-an-update-statement/ https://sqlstudies.com/2013/09/16/dba-myths-you-cant-use-an-alias-in-an-update-statement/
It turned out that I should have just been saying fname = :fname, lname = :lname instead of saying users.fname = :fname, users.lname = :lname.
原来我应该说 fname = :fname, lname = :lname 而不是 users.fname = :fname, users.lname = :lname。 But I'm still unsure as to why this is.
但我仍然不确定为什么会这样。
If you look at the UPDATE
syntax diagrams you'll see it just takes plain column names.如果您查看
UPDATE
语法图,您会看到它只采用简单的列名。 There's no need to specify the table name because you can only update a single table at a time;不需要指定表名,因为一次只能更新一个表; no room for ambiguity like in a
SELECT
.没有像
SELECT
那样含糊不清的余地。 Therefore the parser rules don't support an optional table name component there.因此解析器规则不支持可选的表名组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.