INSERT INTO sql查询无法正常工作的列未定义

时间:2018-10-17 15:50:25

标签: sql ruby postgresql pg

def self.create_tables
  connection = PG.connect(dbname: 'clients')
  connection.exec('CREATE TABLE client_details (
                Company_ID SERIAL PRIMARY KEY,
                Company_Name text,
                Company_Telephone text,
                Company_Representitive text,
                Company_Email text,
                Company_Address text,
                Company_Contract_Start_Date text,
                Company_Contract_End_Date text
                )')
end
def self.create_table_data
  connection = PG.connect(dbname: 'clients')
  connection.exec('INSERT INTO client_detail (
  Company_Name,
  Company_Telephone,
  Company_Representitive,
  Company_Email,
  Company_Address,
  Company_Contract_Start_Date,
  Company_Contract_End_Date) 
  VALUES (
  "Monoprix", 
  "1234", 
  "sarah", 
  "dan@dan.com", 
  "this road", 
  "12", 
  "13");')
end

当我运行第一个功能时,当我运行第二个功能时,我得到一个错误:

Traceback (most recent call last):
         2: from lib/data_generator.rb:22:in `<main>'
         1: from lib/data_generator.rb:6:in `create_table_data'
    lib/data_generator.rb:6:in `async_exec': ERROR:  column"monoprix" does not exist (PG::UndefinedColumn)
    LINE 1: ...act_Start_Date,Company_Contract_End_Date) VALUES (Monoprix, ...
                                                                    ^

当我试图将值“ monoprix”插入到COLUMN Company_Name而不是“ monoprix”中时,我不知道为什么错误告诉我“ monoprix”列不存在。

但是,当我插入值1,2,3,4,5,6,7时,效果很好。

我很困惑。

我想在“ Company_Name”列中插入“ monoprix”。

2 个答案:

答案 0 :(得分:1)

您的错误来自字符串中的"引号。根据Mike的回答,您需要使用反斜杠来分隔它们,或者按照下面的说明,在字符串中使用单斜线并在字符串中使用双斜线来分隔字符串本身。再次尝试用单引号将其替换:'

def self.create_tables
  connection = PG.connect(dbname: 'clients')
  connection.exec('CREATE TABLE client_details (
                Company_ID SERIAL PRIMARY KEY,
                Company_Name text,
                Company_Telephone text,
                Company_Representitive text, 
                    Company_Email text,
                Company_Address text,
                Company_Contract_Start_Date text,
                Company_Contract_End_Date text
                )')
end
def self.create_table_data
  connection = PG.connect(dbname: 'clients')
  connection.exec("INSERT INTO client_detail (
  Company_Name,
  Company_Telephone,
  Company_Representitive,
  Company_Email,
  Company_Address,
  Company_Contract_Start_Date,
  Company_Contract_End_Date) 
  VALUES (
  'Monoprix', 
  '1234', 
  'sarah', 
  'dan@dan.com', 
  'this road', 
  '12', 
  '13');")
end

答案 1 :(得分:0)

您尝试过吗:

def self.create_tables
  connection = PG.connect(dbname: 'clients')
  connection.exec('CREATE TABLE client_details (
                Company_ID SERIAL PRIMARY KEY,
                Company_Name text,
                Company_Telephone text,
                Company_Representitive text,
                Company_Email text,
                Company_Address text,
                Company_Contract_Start_Date text,
                Company_Contract_End_Date text
                )')
end
def self.create_table_data
  connection = PG.connect(dbname: 'clients')
  connection.exec('INSERT INTO client_detail (
  Company_Name,
  Company_Telephone,
  Company_Representitive,
  Company_Email,
  Company_Address,
  Company_Contract_Start_Date,
  Company_Contract_End_Date) 
  VALUES (
  \'Monoprix\', 
  \'1234\', 
  \'sarah\', 
  \'dan@dan.com\', 
  \'this road\', 
  \'12\', 
  \'13\');')
end

似乎很愚蠢,但必须选中所有明显的框。