1

I am using Python to connect to MySQL in XAMPP.I am creating a string and passing it to a new process through queue.The string contains the MySQL query which on execution will create a table and insert into the table.following is my code:-

from MySQLdb import connect
from os import _exit
from multiprocessing import Process,Queue
q=Queue()

def pkt():
    conn=connect(user='root',passwd='257911.',host='localhost',unix_socket="/opt/lampp/var/mysql/mysql.sock")
    cursor=conn.cursor()
    conn.select_db("try")
    while True:
        y=q.get()
        if y=="exit":
            break
        else:
            cursor.execute(y)
            conn.commit()
    cursor.close()
    conn.close()
    _exit(0)


if __name__=="__main__":
    a=Process(target=pkt)
    a.start()
    query="CREATE TABLE hello(id varchar(10) NOT NULL,name varchar(20)); INSERT INTO hello(id,name) VALUES('1234','sujata'); "
    q.put(query)
    q.put("exit")

Upon executing the code, I am getting the following error:-

Traceback (most recent call last):
  File "/usr/lib64/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib64/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "try.py", line 16, in pkt
    conn.commit()
ProgrammingError: (2014, "Commands out of sync; you can't run this command now")

I am getting the same error on inserting into multiple tables in one query.Is it not possible to combine create and insert in one statement?? Thanks.

0

2 Answers 2

1

No, you cannot give multiple semicolon separated queries in the same line. This is because MySQLdb is a wrapper around _mysql which makes it compatible with the Python DB API interface (Read more about the API here). The API works via PREPARE/EXECUTE statements and according to this,

The text must represent a single statement, not multiple statements.

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

1 Comment

@sujata Did that answer your question?
0

I need to close the cursor after execution. This works for me.

from MySQLdb import connect
from os import _exit
from multiprocessing import Process,Queue
q=Queue()

def pkt():
    conn=connect(user='root',host='localhost',unix_socket="/home/mysql/mysql/mysql.sock")
    cursor=conn.cursor()
    conn.select_db("try")
    while True:
        y=q.get()
        if y=="exit":
            break
        else:
            cursor.execute(y)
            cursor.close()
            conn.commit()
    conn.close()
    _exit(0)


if __name__=="__main__":
    a=Process(target=pkt)
    a.start()
    query="CREATE TABLE hello(id varchar(10) NOT NULL,name varchar(20)); INSERT INTO hello(id,name) VALUES('1234','sujata'); "
    q.put(query)
    q.put("exit")

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.