title: Commands out of sync you can't run this command now
body: <p>I am trying to create some tables using mysqldb.</p>
  
  <p>The issue is that when executing the python script <code>db.py</code> mysql throws the error:</p>
  
  <blockquote>
    <p>_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")</p>
  </blockquote>
  
  <p>db.py:</p>
  
  <pre><code>import MySQLdb
  import MySQLdb.cursors
  
  def init_db():
      db_conn = get_db_conn()
      cursor = db_conn.cursor()
  
      with open("tables.sql", 'r') as f:
          cursor.execute(f.read())
  
  def get_db_conn():
      return MySQLdb.connect(
          host="localhost",
          user="root",
          passwd="secretcat",
          db="uptrender",
          cursorclass=MySQLdb.cursors.DictCursor
      )
  
  init_db() 
  </code></pre>
  
  <p>tables.sql:</p>
  
  <pre><code>DROP TABLE IF EXISTS Test2;
  DROP TABLE IF EXISTS Test;
  
  CREATE TABLE Test (
      id INT NOT NULL
  );
  
  CREATE TABLE Test2 (
      id INT NOT NULL,
      FOREIGN KEY(id) REFERENCES Test(id)
  );
  </code></pre>
  
  <p>According to the <a href="https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html" rel="nofollow noreferrer">mysql docs</a> this error is given when client functions are called in the wrong order. Looking at the ones i use (i only have 3 i think) they look to be in the correct order. First connecting to the database, getting the cursor and finally excecuting the query to create tables. Is this the wrong order? It would not seem logical to do the query before connecting to the database... </p>
  
  <p>EDIT: I tried closing the connection after populating the database with tables but it makes no difference. </p>
  
  <p>EDIT2: Furthermore i tried to drop the database completely and re-create it but mysql still throws the same error.</p>
  
  <p>EDIT3: I found that if i remove the two <code>DROP TABLES IF EXISTS tablename</code>-statements in the top of <code>tables.sql</code> i do not get the error. But only the first table (test) seems to have been created (using <code>SHOW TABLES;</code> in the mysql command line client to verify this)! What the heck is going on there?</p>
  
  <p>EDIT4: So i isolated the problem further, it had nothing to do with flask. </p>