Python Forum

Full Version: Creating tables with pymysql using Xampp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to build a webapp using Flask and the part with the database is a little bit confunsing since and want to wrap the creation of the tables in my main app file. I could create tables without flask but now I'm trying to create these using flask. But there is an error that I don't understand and don't know how to fix it.

Here is my code without flask

db_connect.py

import pymysql
db = pymysql.connect(host="localhost",user="root",passwd="",database="db" )
cursor = db.cursor()

create_table_1 = """CREATE TABLE t1(
                                  ...
                        )"""
cursor.execute(create_table_1)

create_table_2 = """CREATE TABLE t2(
                          ...
                        )"""
cursor.execute(create_table_2)

create_table_3 = """CREATE TABLE t3(
                        ...
                      )
cursor.execute(create_table_3)
db.close()
And here is the code not working!

web_app.py

from flask import Flask, render_template, url_for, flash, redirect
import pymysql

app = Flask(__name__)
app.config['SECRET_KEY'] = '...'
app.config['host'] = 'localhost'
app.config['user'] = 'root'
app.config['passwd'] = ''
app.config['database'] = 'db'


db = pymysql.connect()
cursor = db.cursor()

create_table_1 = """CREATE TABLE t1(
                                  ...
                        )"""


create_table_2 = """CREATE TABLE t2(
                          ...
                        )"""


create_table_3 = """CREATE TABLE t3(
                        ...
                      )"""


cursor.execute(create_table_1, create_table_2, create_table_3)


@app.route("/")
@app.route("/home")
def home():
...
    return render_template('home.html', food=foods)


db.commit()
cursor.close()
db.close()

if __name__ == '__main__':
    app.run(debug=True)
Here is the Error

Traceback (most recent call last):
  File "web_app.py", line 75, in <module>
    cursor.execute(create_table_1, create_table_2, create_table_3)
  File "C:\Users\Computer\...\...\env\lib\site-packages\pymysql\cursors.py", line 168, in execute
    query = self.mogrify(query, args)
  File "C:\Users\Computer\...\...\env\lib\site-packages\pymysql\cursors.py", line 147, in mogrify
    query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting