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