INSERT INTO sql 查询未定义工作列
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
当我 运行 第一个函数工作时,当我 运行 第二个函数时我得到一个错误:
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' 插入到列 Company_Name 时,错误告诉我列 'monoprix' 不存在,而不是 'monoprix'.
但是,当我插入 VALUES 1,2,3,4,5,6,7 时它工作正常。
我被难住了。
我想将 'monoprix' 插入列 'Company_Name'。
您的错误来自字符串中的 "
个引号。根据 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
你试过这个吗:
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
看起来很蠢,但必须勾选所有明显的方框。
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' 插入到列 Company_Name 时,错误告诉我列 'monoprix' 不存在,而不是 'monoprix'.
但是,当我插入 VALUES 1,2,3,4,5,6,7 时它工作正常。
我被难住了。
我想将 'monoprix' 插入列 'Company_Name'。
您的错误来自字符串中的 "
个引号。根据 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
你试过这个吗:
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
看起来很蠢,但必须勾选所有明显的方框。