2

I have tried everything and keep getting this error:

pymysql.err.ProgrammingError: (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
'INSERT INTO tabSingles (doctype, field, value) VALUES ('Bank Reconciliation', 'a' at line 2")

Expanded query (after python format expansion):

SELECT value INTO @var FROM tabSingles WHERE doctype = 'Bank Reconciliation' AND field = 'bank_account';
INSERT INTO tabSingles (doctype, field, value) VALUES ('Bank Reconciliation', 'account', @var);
DELETE FROM tabSingles WHERE doctype = 'Bank Reconciliation' AND field = 'bank_account';

Can anyone see the problem? Is there some issue with multi-line queries? I have tried the individual lines on the mariadb command line and they appear to work as expected. I've also tried both frappe.db.sql and multisql (thought it meant multiline sql but doesn't). If I comment line 2 out, it also errors on line 3. Sorry to disturb but I've been staring at this for hours and cannot figure it out!

EDIT: The obvious answer is this, but I'd still like to know why it doesn't like the original query:

UPDATE tabSingles SET field='{new_name}' WHERE doctype='{doctype}' AND field='{old_name}';
3
  • 1
    Can you post an example of the python code that isn't working? FWIW, I would consider this 3 single-line queries, not a multi-line query. Commented Jan 22, 2020 at 0:41
  • @totalhack I could but I don't think it will help will it? The query in the post is a single string printed from python after format expansion as sent to the db. Commented Jan 22, 2020 at 2:59
  • 1
    I see an erpnext user :P Commented Jan 22, 2020 at 6:25

2 Answers 2

3

For security reasons (mainly SQL injection) MariaDB (and MySQL) servers don't support the execution of multiple SQL statements by default.

For supporting multiple statements execution the client needs to send COM_SET_OPTION command and MYSQL_OPTION_MULTI_STATEMENTS_ON flag to the server, which is not supported by PyMySQL.

Sign up to request clarification or add additional context in comments.

Comments

-1

Do not try to run more than one statement in a call.

Do use BEGIN and COMMIT.

Do use FOR UPDATE.

You need 5 separate commands:

BEGIN;
SELECT ... FOR UPDATE;  -- to keep other connections from messing with the row(s).
UPDATE ...;
DELETE ...
COMMIT;   -- do all of the above "atomically"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.