Related
I'm trying to run a PostgreSQL query which is:
insert into client (email, name) values ('johndoe#email.com', 'johnDoe');
insert into client_settings (client_id, data) values (currval('client_id_seq'), 0);
insert into client_verify (client_id, dataFields) values (currval('client_id_seq'), json_build_object('data1', ['a1', 'a2'], 'data2', ['b1', 'b2']) );
But I'm getting an error stating SQL Error [42601]: syntax error at or near "[".
The last json object(i.e., the dataFields) when inserted into the DB it should look like:
{"data1": ["a1", "a2"], "data2": ["b1", "b2"]}
Not sure what I am doing wrong. Is there something that I'm missing or a different way to do that?
After good research I found documentation to put 'Array' in front of those like:
json_build_object('data1', Array['a1', 'a2'], 'data2', Array['b1', 'b2'])
Is this what you need :
insert into client_verify (client_id, dataFields)
values (currval('client_id_seq'), json_build_object('data1', 'a1, a2', 'data2', 'b1, b2') );
Please try this:
insert into client_verify (client_id, dataFields)
values (currval('client_id_seq')
, json_build_object('data1', '["a1", "a2"]', 'data2', '["b1", "b2"]'));
Here you can check how you need to add your string:
https://www.freeformatter.com/json-escape.html#ad-output
The UNESCAPE is the option you need
Here is a demo :
DEMO
I am attempting to use the following insert command in SQL Sever Management Studio 2012:
INSERT INTO 'F' ('date', 'open', 'high', 'low', 'close', 'volume', 'amount_change','percent_change')
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)
When I attempt this the error I get is
parameters supplied for object F which is not a function
I can confirm that there is a table F. I have tried various syntax using dbo.f, "F", 'F', [dbo].[F], all to no avail.
You need to remove the single quotes from the table name and the column names. If you want them escaped use brackets [].
INSERT INTO [F] ([date], [open], [high], [low], [close], [volume],
[amount_change], [percent_change])
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)
Just a tidbit for your information, there is session setting that allows double quotes (ISO rules) for table and column names.
-- Set option on
SET QUOTED_IDENTIFIER ON
GO
-- Insert data
INSERT INTO "F" ("date", "open", "high", "low", "close", "volume", "amount_change","percent_change")
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)
Check out this technet article for more details.
http://technet.microsoft.com/en-us/library/ms174393.aspx
TSQL is a big product with many little idiosyncrasies.
Sincerely
John
www.craftydba.com
You can use [F] for the table and make sure you are in the same database. It appears to work fine when I test it.
INSERT INTO [F] ('date', 'open', 'high', 'low', 'close', 'volume', 'amount_change','percent_change')
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to implement this code into my script but I get a syntax error
INSERT INTO prospectstbl ( 'customerNumber', 'namePerson1', 'LnamePerson1', 'street', 'city', 'state', 'zip', 'homePhone', 'cellPhone', 'clientSince', 'clientLevel', 'closingDate', 'lastPaymentDate', 'currentBalance', 'repurchaseDate', 'repurchaseAmount', 'delinquentBalance', ) VALUES ('20713254', 'Sonia', 'Amaya', '338 Railroad Ave', 'Ctr Moriches', 'NY', '11934', '6318788386', '6318137972', '10/24/2002', '1', '7/26/2011', '8/11/2011', '$792.15', '', '$0.00', '$0.00')
and the error I get:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''customerNumber', 'namePerson1', 'LnamePerson1', 'street', ' at line 2
Any help would be greatly appreciated.
remove the '' at the column names and the , at the last column:
INSERT INTO prospectstbl ( customerNumber, namePerson1, LnamePerson1, street, city,
state, zip, homePhone, cellPhone, clientSince, clientLevel, closingDate,
lastPaymentDate, currentBalance, repurchaseDate, repurchaseAmount, delinquentBalance )
VALUES ('20713254', 'Sonia', 'Amaya', '338 Railroad Ave', 'Ctr Moriches', 'NY', '11934', '6318788386', '6318137972', '10/24/2002', '1', '7/26/2011', '8/11/2011', '$792.15', '', '$0.00', '$0.00')
There is a comma , after 'delinquentBalance'
use
`
instead of
'
And remove , after , delinquentBalance
or you can remove list of your columns
INSERT INTO prospectstbl VALUES ('20713254', 'Sonia', 'Amaya', '338 Railroad Ave', 'Ctr Moriches', 'NY', '11934', '6318788386', '6318137972', '10/24/2002', '1', '7/26/2011', '8/11/2011', '$792.15', '', '$0.00', '$0.00')
IIRC, single quotes (') are generally used for quoting literal values (strings and such) and indicating that certain tokens are NOT fieldnames. Graves (`) are used to indicate fields.
Try quoting your column names with graves (`) instead.
Also, as others have said, you have an extra comma at the end of your column name list. Remove this as well.
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
($title, $introtext, $fulltext, 1, 1, $catid, 5)
Query is made up of php script. Derivation of variables shows that they are not empty.
At the request of phpMyAdmin shows the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
You need to use single quotes to indicate strings to SQL - use:
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('". $title ."', '". $introtext ."', '". $fulltext ."', 1, 1, $catid, 5)
A safer means would be to use:
$query = sprintf("INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('%s', '%s', '%s', 1, 1, %d, 5)",
mysql_real_escape_string($title),
mysql_real_escape_string($introtext),
mysql_real_escape_string($fulltext),
$catid);
Reference:
SPRINTF
MYSQL_REAL_ESCAPE_STRING
You need to surround your PHP variables that are being inserted into string-type fields with quotes:
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('$title', '$introtext', '$fulltext', 1, 1, $catid, 5)
Read about prepared statements in the manual of your programming language or use mysql_escape_string.
I'm creating an AIR application which connects to a SQLite database. The database balks at this insert statement, and I simply cannot figure out why. There is no error, it simply does not move on.
INSERT INTO Attendee (AttendeeId,ShowCode,LastName,FirstName,Company,Address,Address2,City,State,ZipCode,Country,Phone,Fax,Email,BuyNum,PrimaryBusiness,Business,Employees,Job,Value,Volume,SpouseBusiness,DateAdded,ConstructionWorkType,UserPurchaser,DecisionMaker,SafetyProducts,NearFuturePurchase,RepContact,Winner) VALUES('39610','111111','SMITH','JIM','COMPANY','1000 ROAD STREET','','PITTSBURGH','PA','15219','','5555555555','5555555555','PERSON#EXAMPLE.NET','','','0000000000000000000','','','','','0?','Fri Jan 30 14:20:08 GMT-0500 2009','other','neither','no','gas_detection','no','no','winner')
I know that the app can connect to the database, because it creates the table just fine. Here's the schema for the table for your reference:
CREATE TABLE Attendee (AttendeeId TEXT PRIMARY KEY,ShowCode TEXT,LastName TEXT,FirstName TEXT,Company TEXT,Address TEXT,Address2 TEXT,City TEXT,State TEXT,ZipCode TEXT,Country TEXT,Phone TEXT,Fax TEXT,Email TEXT,BuyNum TEXT,PrimaryBusiness TEXT,Business TEXT,Employees TEXT,Job TEXT,Value TEXT,Volume TEXT,SpouseBusiness TEXT,DateAdded TEXT,ConstructionWorkType TEXT,UserPurchaser TEXT,DecisionMaker TEXT,SafetyProducts TEXT,NearFuturePurchase TEXT,RepContact TEXT, Winner TEXT)
There is a good chance that there is an error in the INSERT statement, because if I try to execute it on a separate Adobe Air SQLite admin program, it throws an ambiguous error (#3115).
Thanks for any insight you might have.
EDIT:
For those wondering, if I make a simple table, thus:
CREATE TABLE Attendee (AttendeeId TEXT)
And try to insert, thus:
INSERT INTO Attendee (AttendeeId) VALUES('09283A')
I still get error #3115.
Have you tried running smaller statements? Like, a really simple INSERT on a really simple table?
Have you tried quoting the column names? Maybe one of them is a reserved word.
insert into Attendee ("AttendeeId", "ShowCode", "LastName", "FirstName", "Company", "Address", "Address2", "City", "State", "ZipCode", "Country", "Phone", "Fax", "Email", "BuyNum", "PrimaryBusiness", "Business", "Employees", "Job", "Value", "Volume", "SpouseBusiness", "DateAdded", "ConstructionWorkType", "UserPurchaser", "DecisionMaker", "SafetyProducts", "NearFuturePurchase", "RepContact", "Winner") values ('39610', '111111', 'SMITH', 'JIM', 'COMPANY', '1000 ROAD STREET', '', 'PITTSBURGH', 'PA', '15219', '', '5555555555', '5555555555', 'PERSON#EXAMPLE.NET', '', '', '0000000000000000000', '', '', '', '', '0?', 'Fri Jan 30 14:20:08 GMT-0500 2009', 'other', 'neither', 'no', 'gas_detection', 'no', 'no', 'winner');
Try this one, it was computer generated.
Apparently you need to use double quotes rather than single quotes.