0

im trying to escape characters to create a transaction block in python and commit it all at once with no success...

for example:

def transaction_block(sql):
    global sql_transaction
    sql_transaction.append(sql)
    if len(sql_transaction) > 1000:
        db.execute('BEGIN TRANSACTION')
        for s in sql_transaction:
            try:
                db.execute(s)
            except Exception as e:
                print(e)
        db.commit()
        sql_transaction = []

def sql_insert_comment(parentid,comment):
    try:
        sql = "INSERT INTO p_reply (parent_id, comment) VALUES ('{}','{}');".format(parentid, comment)
        transaction_block(sql)
    except Exception as e:
        print('error s02',str(e))

how should i adapt this to use the %s so i can escape it all? or there is a better way to accomplish this?

###UPDATE 1###

found a possible answer using execute_values() credits to: https://billyfung.com/writing/2017/06/improving-multiple-inserts-with-psycopg2/

700% increase performance

IF you know a better way yet, please let me know ;)

0

2 Answers 2

1

What about something like this:

entries = [
    ("parent_id1", "comment1"),
    ("parent_id2", "comment2"),
    ("parent_id3", "comment3")
]
query = "INSERT INTO p_reply (parent_id, comment) VALUES %s;" % ", ".join(str(entry) for entry in entries)

?

That may fit your needs:

>>> "INSERT INTO p_reply (parent_id, comment) VALUES %s;" % ", ".join(str(entry) for entry in [("parent_id1", "comment1"), ("parent_id2", "comment2")])
"INSERT INTO p_reply (parent_id, comment) VALUES ('parent_id1', 'comment1'), ('parent_id2', 'comment2');"

If you have integers (parent_id values) in your dataset, you may also have to convert it into str or modify the formatted string to add the '.

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

1 Comment

Thanks, I think it's something close to the execute_values() function, it works.
0

You should escape like this:

try:
    sql = "INSERT INTO p_reply (parent_id, comment) VALUES (%s,%s)"
    db.execute(sql,[parent_id,comment])
except Exception as e:
    print('error s02',str(e))

1 Comment

Tks, i know that i can escape with %s, but how can i add this for example: (sql,[parent_id,comment]) to a single variable and add the variable to a list so i can iterate through it and commit every 1000 queries...? that is my problem... i can do it as tuple item to a list, and loop n^2 but then... i think its just less time consume to execute 1 by 1.... what do you think? Tks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.