简体   繁体   English

“SQLite3::SQLException: near ".": 语法错误”是什么意思?

[英]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.

相关问题 ruby on rails:多次插入SQLite3 :: SQLException:在“,”附近:语法错误: - ruby on rails: multiple insertion SQLite3::SQLException: near “,”: syntax error: sqlite3:near“。”:语法错误 - sqlite3: near “.” : syntax error 如何修复SQLite3 :: SQLException:在“至”附近:语法错误:SELECT“”。* FROM“” WHERE“”。”” =? AND(到&lt;&#39;[Time.now]&#39;)LIMIT? OFFSET? - How to fix SQLite3::SQLException: near “to”: syntax error: SELECT “”.* FROM “” WHERE “”.“” = ? AND (to < '[Time.now]') LIMIT ? OFFSET? SQLite3::SQLException:接近“LIKE”:语法错误:你能在 sqllite3 中使用多个 OR 吗? - SQLite3::SQLException: near "LIKE": syntax error: Can you use more than one OR in sqllite3? OperationalError:“,”附近:语法错误-SQLITE3 - OperationalError: near “,”: syntax error - SQLITE3 SQLite3 :: SQLException:在“&”附近:语法错误:SELECT“ pages”。* FROM“ pages” WHERE(menu_display = true &amp;&amp; is_published = true) - SQLite3::SQLException: near “&”: syntax error: SELECT “pages”.* FROM “pages” WHERE (menu_display=true && is_published=true) sqlite3.OperationalError:靠近“WHERE”:语法错误(Python 2,sqlite3) - sqlite3.OperationalError: near “WHERE”: syntax error (Python 2, sqlite3) SQLException:错误:“ \\”处或附近的语法错误 - SQLException: ERROR: syntax error at or near “\” 在“ CASE”附近的SQLite3 C ++查询:语法错误 - SQLite3 C++ Query near “CASE”: syntax error &#39;GROUP 附近的语法不正确,当它是不正确的语法时是什么意思? - 'Incorrect syntax near GROUP, What does it mean when it is an incorrect syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM