What is wrong with this SQLite query? - sql

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.

Related

How to use unnest array with dynamic Postgresql query across two tables

I am relatively new to SQL and having some difficulty understanding how to run a conditional query. For some context, this query is being run with Node/Express using Postgresql.
Situation
If a user selects 'Team A' the query (in the backend) will take the data from one table, while if the user selects 'Team B' it will run the query against a different table. To render the table correctly I need to transpose the data and understand I can achieve this with UNNEST(array[]).
I can run the query successfully with the following code:
SELECT "TeamA" AS "Player",
UNNEST(array['Type1', 'Type2', 'Type2']) AS "Type",
UNNEST(array["Column1", "Column2", "Column3"]) AS "Type2",
UNNEST(array["Column4", "Column5", "Column6"]) AS "Type3",
UNNEST(array["Column7", "Column8", "Column9"]) AS "Type4",
UNNEST(array["Column10", "Column11", "Column12"]) AS "Type5"
FROM sportingdb."TeamAData"
WHERE "TeamA" = '${player}'
However this needs to be dynamic and query a different table if the user selects 'Team A' or 'Team B'.
I have attempted to use the same query with a ternary, however was given a syntax error and am unable to identify where I have gone wrong. I also broke it down as an if else statement as follows:
DO
BEGIN
IF '${TeamA}' = "TeamA" THEN
SELECT "TeamA" AS "Player",
UNNEST(array['Type1', 'Type2', 'Type2']) AS "Type",
UNNEST(array["Column1", "Column2", "Column3"]) AS "Type2",
UNNEST(array["Column4", "Column5", "Column6"]) AS "Type3",
UNNEST(array["Column7", "Column8", "Column9"]) AS "Type4",
UNNEST(array["Column10", "Column11", "Column12"]) AS "Type5"
FROM sportingdb."TeamAData"
WHERE "TeamA" = '${player}'
ELSE
SELECT "TeamB" AS "Player",
UNNEST(array['Type1', 'Type2', 'Type2']) AS "Type",
UNNEST(array["Column1", "Column2", "Column3"]) AS "Type2",
UNNEST(array["Column4", "Column5", "Column6"]) AS "Type3",
UNNEST(array["Column7", "Column8", "Column9"]) AS "Type4",
UNNEST(array["Column10", "Column11", "Column12"]) AS "Type5"
FROM sportingdb."TeamBData"
WHERE "TeamB" = '${player}'
END IF
END
I found this link was somewhat helpful, which I followed to write the above code but arrived with an SQL error [42601]: ERROR: syntax error at or near "BEGIN". I also tried adding the dollar signs as per the example as well as a DECLARE line but this didn't seem to work.
I would truly appreciate any assistance on how I can get this to work. Thanking you in advance!

Error at or near square brackets - Postgres

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

Unexpected Beginning of statement - Issue with Syntax?

Currently using PHPMyAdmin, and I am trying to insert some data into a SQL Table.
The error that I am getting is Unexpected Beginning of Statement (near 'brand')
Brand is on the second line of the SQL statement.
The data that I am trying to enter:
INSERT INTO 'vehicles'
('reg_no', 'category', 'brand', 'description', 'dailyrate') VALUES
('BA5923W', 'car', 'Toyota', 'black 4 door 2.6 engine ', '9.99'),
('BA6611A', 'car', 'NISSAN ', '4 Door Saloon, Automatic', '9.99'),
('BM1245a', 'car', 'Golf', NULL, '9.00'),
('GA5955E', 'truck', 'NISSAN CABSTAR 3.0', 'Lorry, Manual ', '18.99')
cheers
You have an syntax error at you query.
For columns names you should use Grave Accent ` char like
(`reg_no`, `category`, `brand`, `description`, `dailyrate`)
and for data line Apostrophe ' char like
('BA5923W', 'car', 'Toyota', 'black 4 door 2.6 engine ', '9.99')
Fixed. I was not closing the block of SQL code that created the table, which comes before this.

Grocery CRUD: How to debug Add / Edit errors

I have a N:M relation beetween 'Museum' and 'Category'. Three tables:
Museum: id, name, ...
Category: id, name, ...
Museum_x_Category: museum_id, category_id
And have set a N:M relation with a sentence like:
$crud->set_relation_n_n('Museum Categories', 'Museum_x_Category', 'Category', 'museum_id', 'category_id', 'name', 'category_id' );
I'm getting "An error ocurred on insert" errors when adding, and "An error has occurred on saving." when editing/uploading.
I guess it is due to an SQL error, and i'd like to see the SQL sentences running behind.
Does anyone know how to see it?
PHP: 5.3.5
MySQL: 5.1.14
Solved it. There were two problems:
1.- there was a non-utf8 character in the relation name:
$crud->set_relation_n_n('Categorías', 'Museum_x_Category', 'Category', 'museum_id', 'category_id', 'name', 'category_id' );
now replaced by:
$crud->set_relation_n_n('Categorias', 'Museum_x_Category', 'Category', 'museum_id', 'category_id', 'name' );
(note the í in Categorías, which means Categories in Spanish).
2.- there was a problem with the last parameter ('category_id'). Note that i've removed it. With the parameter included, it was assigning all the museums to the first category, always, whatever category i select.
It works as desired now :)

Incorrect syntax for inserting data into SQL Server

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)