This question already has answers here:
Suggested way to run multiple sql statements in python?
(9 answers)
Closed 1 year ago.
I am building an application with Flask/mysql. I have a raw SQL script needed to seed the database. If I enter a SQL shell and run the command, it works without issues. When I run it from Flask, I am getting an error:
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP TABLE IF EXISTS board_symbol; \n DROP TABLE IF EXISTS symbol; \n ' at line 2")
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
My flask application is very simple, two files. The first, app.py:
from flask import Flask
from db import create_db, seed_db
app = Flask(__name__)
db = create_db(app)
conn = db.connect()
seed_db(conn)
#app.route("/")
def index():
return "Hello world!"
The second, db.py:
from flaskext.mysql import MySQL
def create_db(app):
app.config["MYSQL_DATABASE_USER"] = "root"
app.config["MYSQL_DATABASE_PASSWORD"] = "mysqluser"
app.config["MYSQL_DATABASE_DB"] = "fault_tree"
app.config["MYSQL_DATABASE_HOST"] = "localhost"
mysql = MySQL()
mysql.init_app(app)
return mysql
def seed_db(conn):
try:
cursor = conn.cursor()
cursor.execute("""
DROP TABLE IF EXISTS symbol_connection;
DROP TABLE IF EXISTS board_symbol;
DROP TABLE IF EXISTS symbol;
DROP TABLE IF EXISTS board;
DROP TABLE IF EXISTS symbol_type;
CREATE TABLE IF NOT EXISTS board (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
description VARCHAR(500),
PRIMARY KEY(id)
);
CREATE TABLE IF NOT EXISTS symbol_type (
id INTEGER NOT NULL AUTO_INCREMENT,
type VARCHAR(30),
PRIMARY KEY(id)
);
CREATE TABLE IF NOT EXISTS symbol (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
description VARCHAR(200),
type INT,
child_board INT,
PRIMARY KEY(id),
FOREIGN KEY(type) REFERENCES symbol_type(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(child_board) REFERENCES board(id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS board_symbol (
board_id INTEGER,
symbol_id INTEGER,
FOREIGN KEY(board_id) REFERENCES board(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(symbol_id) REFERENCES symbol(id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY(board_id, symbol_id)
);
CREATE TABLE IF NOT EXISTS symbol_connection (
board_id INTEGER NOT NULL,
start_symbol INT NOT NULL,
destination_symbol INT NOT NULL,
PRIMARY KEY(start_symbol, destination_symbol),
FOREIGN KEY(board_id) REFERENCES board(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(start_symbol) REFERENCES symbol(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(destination_symbol) REFERENCES symbol(id) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO board(name, description) VALUES('test_board', 'test_board');
INSERT INTO symbol_type(type) VALUES('Event/basic');
INSERT INTO symbol_type(type) VALUES('Event/conditioning');
INSERT INTO symbol_type(type) VALUES('Event/intermediate');
INSERT INTO symbol_type(type) VALUES('Event/remote basic');
INSERT INTO symbol_type(type) VALUES('Event/underdeveloped');
INSERT INTO symbol_type(type) VALUES('Gate/and');
INSERT INTO symbol_type(type) VALUES('Gate/or');
INSERT INTO symbol_type(type) VALUES('Gate/priority and');
INSERT INTO symbol_type(type) VALUES('Gate/priority or');
INSERT INTO symbol_type(type) VALUES('Gate/exclusive or');
INSERT INTO symbol_type(type) VALUES('Transfer');
INSERT INTO symbol(name, description, type) VALUES('Test event', 'test basic event', 1);
INSERT INTO symbol(name, description, type) VALUES('Test and gate', 'test and gate', 6);
INSERT INTO symbol(name, description, type) VALUES('Test intermediate event', 'test int event', 3);
INSERT INTO symbol(name, description, type) VALUES('Test or gate', 'test or gate', 7);
INSERT INTO board_symbol(board_id, symbol_id) VALUES(1, 1);
INSERT INTO board_symbol(board_id, symbol_id) VALUES(1, 2);
INSERT INTO board_symbol(board_id, symbol_id) VALUES(1, 3);
INSERT INTO board_symbol(board_id, symbol_id) VALUES(1, 4);
INSERT INTO symbol_connection(board_id, start_symbol, destination_symbol) VALUES(1, 1, 2);
INSERT INTO symbol_connection(board_id, start_symbol, destination_symbol) VALUES(1, 1, 3);
INSERT INTO symbol_connection(board_id, start_symbol, destination_symbol) VALUES(1, 3, 4);
SELECT * FROM symbol_connection;
SELECT * FROM symbol_type;
SELECT * FROM board_symbol;
SELECT * FROM symbol;
SELECT * FROM board;
""")
data = cursor.fetchall()
print(data)
except Exception as e:
print(e)
# raise Exception("Problem initializing MySQL database - check that fault_tree database exists and user root has access")
This is set up using a local database named fault_tree, with creds as listed in db.py if helpful for troubleshooting.
By default, you can only do one SQL statement per invocation of cursor.execute().
To run multiple SQL statements separated by semicolons, use cursor.execute("""...""", multi=True)
See https://instructobit.com/tutorial/130/Executing-multi-query-.sql-files-using-MySQL-connector-in-Python for a tutorial.
I want to create a table through a python script running sql but want to clear any ones with the same name which may already exist. The below code produces an error "mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE groceries(id int(11) NOT NULL AUTO_INCREMENT,item varchar(25) D' at line 1".
I can get the code to work line by line individually but not sure how to combine it all into one script so it runs at the same time through python.
import mysql.connector
db = mysql.connector.connect(
host = "localhost",
user= "test",
password = 'test',
database = "grocery"
)
cursor = db.cursor()
sql = "DROP TABLE IF EXISTS groceries, CREATE TABLE groceries(`id` int(11) NOT NULL AUTO_INCREMENT,`item` varchar(25) DEFAULT NULL, `name` varchar(25) DEFAULT NULL, `quantity` int(11) DEFAULT NULL,PRIMARY KEY (`id`))"
cursor.execute(sql)
You are getting a syntax error because you using commas instead of semicolons for separating the DROP TABLE IF EXISTS and CREATE TABLE statements
If you separate your SQL statements with semicolons, you'll be able to execute your code.
In addition, you need to tell your connector that there are multiple statements with multi=true
The final code looks like:
sql = """
DROP TABLE IF EXISTS groceries;
CREATE TABLE groceries(
`id` int(11) NOT NULL AUTO_INCREMENT,
`item` varchar(25) DEFAULT NULL,
`name` varchar(25) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
"""
cursor.execute(sql, multi=true)
I'm trying do delete a user from my database using python and sqlite.
import sqlite3
database_connection = sqlite3.connect('test.db')
delete_username_input = input("Which user you would like to delete?\n\n")
sql = ("DELETE * from USERS where USERNAME = ?")
args = (delete_username_input)
database_connection.execute(sql, args)
database_connection.commit()
database_connection.close()
When running the code above, I get the following error:
sqlite3.OperationalError: near "*": syntax error
Any idea what might be causing this error?
The table that I use has been created using the following syntax:
conn.execute('''CREATE TABLE USERS
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
USERNAME TEXT NOT NULL,
PASSWORD TEXT NOT NULL,
WINS FLOAT NOT NULL,
LOSES FLOAT NOT NULL,
GAMESPLAYED FLOAT NOT NULL,
WINPERCENT FLOAT NOT NULL );''')
Any help will be appreciated.
Your SQL syntax is wrong. It should be
DELETE from USERS where USERNAME = ?
without the *
Check it out here
I am having a hard time understanding why psycopg2 has a problem with the word 'user'. I am trying to insert values into a table called user with the columns user_id, name, password. I am getting a programmingError: syntax error at or near "user". open_cursor() is a function used to open a cursor for database operations.
Here is my code:
query = """INSERT INTO user (name, password) VALUES (%s, %s);"""
data = ('psycouser', 'sha1$ba316b$52dd71da1e331247f0a7ab869e1b072210add9c1')
with open_cursor() as cursor:
cursor.execute(query, data)
print "Done."
because user is a part of sql language.
try taking it in dbl quotes:
query = 'INSERT INTO "user" (name, password) VALUES (%s, %s);'
I'm trying to connect to one our our internal databases using the following code:
engine = create_engine('postgresql+psycopg2://{user}:{passw}#{host}:{port}/{db}'.format(
user=config3.CANVAS_USERNAME,
passw=config3.CANVAS_PWD,
host=config3.CANVAS_BOUNCER,
port=config3.CANVAS_PORT,
db='cluster17dr'
))
metadata = MetaData()
metadata.reflect(bind=engine)
print(metadata.tables)
And my only result is a table called 'spatial_ref_sys', which I assume is some kind of metadata. I know that my login stuff is correct, because this works perfectly:
with ppg2.connect(
database='cluster17dr',
user=config3.CANVAS_USERNAME,
password=config3.CANVAS_PWD,
host=config3.CANVAS_BOUNCER,
port=config3.CANVAS_PORT) as conn:
cur = conn.cursor()
sql = 'SELECT * FROM canvas.switchman_shards LIMIT 10'
cur.execute(sql)
res = cur.fetchall()
print(res)
Any ideas as to what I'm missing in my connection using SQLAlchemy?
By default, if no schema name is specified, SQLAlchemy will only give you tables under the default schema. If you want to reflect tables in a schema other than the default schema (which defaults to public in PostgreSQL), you need to specify the schema keyword to .reflect():
metadata.reflect(..., schema="canvas")