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.
Related
for fileName in fileNames:
with open(fileName, mode="rt", encoding="utf-8", newline="") as csvfile:
csvFile = csv.reader(csvfile, delimiter=',')
header = next(csvFile)
headers = map((lambda x: x.strip()), header)
insert = 'INSERT INTO TEST ('.format(tableChoice) + ', '.join(headers) + ') VALUES '
for row , record in enumerate(csvFile, start=1):
values = map((lambda x: "'"+x.strip()+"'"), record)
myCursor.execute(insert +'('+ ', '.join(values) +');' )
cnxn.commit()
I get the below error when I reach the execute line in the script. I just need the data extracted from the csv to be inserted into the database, row by row. Anyone know what's causing the error?
ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '-'. (102) (SQLExecDirectW)")
Edit:
The SQL query string is as follows:
INSERT INTO TEST (this, that, those) VALUES ('1', '11', '111');
INSERT INTO TEST (this, that, those) VALUES ('2', '22', '222');
INSERT INTO TEST (this, that, those) VALUES ('3', '33', '333');
Likely issue is due to special characters in your column names such as - which requires wrapping in square brackets to escape in SQL Server. Additionally, consider using consistent Python string formatting and csv.DictReader to build a parameterized query followed by executemany for insertion:
for fileName in fileNames:
with open(fileName, mode="rt", encoding="utf-8", newline="") as csvfile:
reader = csv.DictReader(f)
data = [row for row in reader]
# BUILD SQL WITH [...] ESCAPED COLUMNS AND ? PARAM PLACEHOLDERS
sql = "INSERT INTO [Test] ([{cols}]) VALUES ({prms})"
sql = sql.format(cols="], [".join(map(lambda x: x.strip(), data[0].keys())),
prms=", ".join(['?'] * len(data[0])))
# APPEND ALL ROWS AND BIND PARAMS
myCursor.executemany(sql, [list(d.values()) for d in data])
cnxn.commit()
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
INSERT INTO "order" SET `user_id` = 5, `week_id` = 1, `box_settings_id` = '1', `delivery_day` = 'Thursday', `delivery_time` = 'Morning 6am - 9am', `notes` = '', `recipes` = '70, 71, 72, 74';
What is wrong with my SQL query? I'm getting this error in PHPMyAdmin:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"order" SET `user_id` = 5, `week_id` = 1, `box_settings_id` = '1', `delivery_day' at line 1
While the SQL debugger isn't showing any error.
Issue likely is due to use of double quotes which MySQL and MariaDB diverge from the ANSI SQL standard. Instead of double quotes (synonymous to single quotes for string literals), MariaDB and MySQL would use backticks. With that said, you can adjust SQL mode to non-default ANSI or ANSI_QUOTES to support double quotes for identifiers:
ANSI_QUOTES
Changes " to be treated as `, the identifier quote character. This may break old MariaDB applications which assume that " is used as a string quote character.
Therefore, simply use backticks on order table like you do for columns. Generally, any identifier can use backticks.
INSERT INTO `order`
SET `user_id` = 5,
`week_id` = 1,
`box_settings_id` = '1',
`delivery_day` = 'Thursday',
`delivery_time` = 'Morning 6am - 9am',
`notes` = '',
`recipes` = '70, 71, 72, 74';
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.
I am attempting to insert data into a table in my database. I am using an Oracle Apache Derby DB. I have the following code-
Insert into P2K_DBA.ODS_CNTRL
(ODS_LOAD_ID, ODS_STATUS, USR_WWID, USR_FIRST_NM,
USR_LAST_NM, USR_DISPLAY_NM, USR_NT_ID,TOT_AMT,
TOT_RCD_CNT, TOT_QTY, LAST_UPD_DT, ODS_ADJ_TYP,
ODS_ADJ_DESC, APRV_WWID, APRV_FIRST_NM,APRV_LAST_NM,
APRV_DISPLAY_NM, APRV_NT_ID, APRV_DT
)
values
(6,'avail','64300339', 'Travis',
'Taylor', 'TT', '3339', 33,
15, 40, '7/10/2012', 'test',
'test', '64300337', 'Travis',
'Taylor', 'TT', '3339', '2/06/2013');
I ran this SQL command and received the following error-
"Error code -1, SQL state 21000: Scalar subquery is only allowed to return a single row.
Line 1, column 1"
I have ran this code successfully a few days ago. On top of that I have tried to manually enter in data in this table (using NetBeans) and have it auto generate the code, which resulted in the same error.
What is causing this error and how can I solve/bypass it?
One way in which you could run into this would be to do something like
CREATE FUNCTION F(...) ...
F((SELECT COL FROM T))
But you could instead write
... (SELECT F(COL) FROM T) provided the new context permits a subquery, that is.