简体   繁体   中英

INSERT INTO sql query not working column undefined

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

When I run the first function it works, when I run the 2nd function I get an error:

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, ...
                                                                    ^

I don't know why the error is telling me that column 'monoprix' does not exist when I am attempting to INSERT the value 'monoprix' into the COLUMN Company_Name, not 'monoprix'.

However, when I insert the VALUES 1,2,3,4,5,6,7 it works fine.

I'm stumped.

I want to insert 'monoprix' into the column 'Company_Name'.

Your error has come from the " quotation marks in the string. You need to either use a backslash to delimit them, as per Mike's answer, or use single ones inside a string and double ones to delimit the string itself as I have done below. Try again replacing them with single quotes: '

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

Have you tried this:

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

Seems dumb, but gotta check off all the obvious boxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM