Python Forum
Creating tables with pymysql using Xampp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating tables with pymysql using Xampp
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  XAMPP Configuration of Python lavanyadeepak 1 1,379 Mar-19-2023, 02:52 PM
Last Post: snippsat
  pymysql.err.InterfaceError nikos 1 3,040 Feb-24-2019, 02:07 PM
Last Post: nikos
  Issue with bottle-pymysql nikos 13 5,478 Feb-23-2019, 11:15 AM
Last Post: nikos
  unable to import pymysql IMuriel 3 7,376 Jan-08-2019, 08:56 PM
Last Post: IMuriel
  Insert data to SQL through pymysql and flask iainstott 3 7,724 Oct-24-2017, 03:04 PM
Last Post: iainstott

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020