【问题标题】:no such column: COLIN (SQLite3::SQLException)没有这样的列:COLIN (SQLite3::SQLException)
【发布时间】:2022-08-15 22:53:28
【问题描述】:

我的代码在我的更新功能上给了我没有这样的列错误。它返回正确的输出但仍然给出错误。我只是希望更新功能在运行时修改名字。任何想法为什么?

    
    class User 
        def self.create(user_info)
            DBConnection.execute(<<-SQL)
              INSERT INTO
                users (firstname, lastname, age, password, email)
              VALUES
                (\'#{user_info[:firstname]}\', \'#{user_info[:lastname]}\',
                \'#{user_info[:age]}\', \'#{user_info[:password]}\', \'#{user_info[:email]}\')
            SQL
            DBConnection.last_insert_row_id
          end
    
          def self.find(user_id)
            DBConnection.execute(<<-SQL, user_id)
                SELECT* FROM
                    USERS
                WHERE
                    id = ?
            SQL
            .first
          end  
    
          def self.update(user_id, attribute, value)
            DBConnection.execute(<<-SQL, user_id, attribute, value)
                UPDATE
                    users
                SET
                    #{attribute} = #{value}
                WHERE
                    id = #{user_id}
            SQL
          end 
           
    end    
    
    user1 = User.create(firstname: \"Colin\", lastname: \"Doe\", age: \"25\", password: \"password\", email: \"bla\")
    user2 = User.create(firstname: \"Jane\", lastname: \"Doe\", age: \"25\", password: \"password\", email: \"bla\")
    user3 = User.create(firstname: \"Ted\", lastname: \"Doe\", age: \"25\", password: \"password\", email: \"bla\")
    User.update(1, :firstname, \'COLIN\')
    print User.find(1)``
    ```
  • 定义自己的创建更新和查找方法是否有特定原因?
  • 这只是我正在处理的一项任务。问题是要求我们定义自己的方法。
  • 我知道了。对于您的错误,据我了解,您需要从 DBConnection.execute(&lt;&lt;-SQL, user_id, attribute, value) 行中删除值
  • @MehmetAdilİstikbal - 我试过了,但仍然遇到同样的错误。

标签: ruby sqlite sinatra


【解决方案1】:

只是这个错字吗?

def self.create(user_info)
  DBConnection.execute(<<-SQL)

应该:

def self.create(user_info)
  DBConnection.execute(<<-SQL,

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 2013-10-09
    • 1970-01-01
    • 2020-10-18
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多