python Django db utils programming error (1064) - python

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json NOT NULL, category_json json NOT NULL)' at line 1")
occurs during the database migrations in python sharehosting

Related

python mysql syntax error when declaring and printing a variable

For some reason, this simple block of code is giving me a sql syntax error. I have no clue why it's wrong
result = conn.execute(
text(
'''
SET #var = 1;
SELECT #var;
'''
)
)
print(result.fetchall())
Why is this giving me a sql syntax error?
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (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 'SELECT #var' at line 2")
[SQL:
SET #var = 1;
SELECT #var;
]
(Background on this error at: https://sqlalche.me/e/14/f405)

Python mysql connector error in execute query

I know there're similar questions regarding this topic in Stackoverflow, like these ones:
https://stackoverflow.com/a/52303677/1876345
SQL query using %s in Python 3.3
mysql.connector.errors.ProgrammingError: 1064 (4200): You have an error in your SQL syntax;
But i still encounter an error passing a variable at moment execute is run.
Option #1
mycursor = mydb.cursor()
dbname = 'ssl'
mycursor.execute('CREATE DATABASE {}'.format(dbname))
And I get this 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 'ssl' at line 1
Option 2
mycursor = mydb.cursor()
dbname = 'ssl'
mycursor.execute("CREATE DATABASE %s ", (dbname))
Getting similar 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 '%s' at line 1
Any idea why?

Flask-Mysql Python 1064 error when update table into Mysql

Been stuck whole day with this update mysql table error while build a web app with Flask python.
(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 'WHERE 'user_id'='28'' at line 1")
Tried removing comma and adding `` according to solution of similar problems in Stackoverflow but it's not working.
#app.route('/update')
def update():
try:
conn = mysql.connect()
update = conn.cursor(pymysql.cursors.DictCursor)
sql = "UPDATE user SET user_photo=%s, WHERE user_id=%s"
_path="User.30.1.jpg"
_id="28"
data = (_path,_id)
update.execute(sql,data)
conn.commit()
return render_template('training.html')
except Exception as e:
print(e)
Remove the comma from the sql variable:
sql = "UPDATE user SET user_photo=%s WHERE user_id=%s"

Can't insert PDF file into MySQL db with Python

I'm stuck. I've been trying to insert a pdf file in to MySQL db but I can't. I've tried with mysql.connector and MySQLdb classes. I get nearly the same errors. I've read many posts about this issue. I tried commas, variables also. Here are my code and error;
import mysql.connector
db = mysql.connector.connect(host="127.0.0.1", user='root', password='masatablo', db='yukleme')
c = db.cursor()
acilacak_dosya = open("bizim.PDF", "rb")
yuklenecek_dosya = acilacak_dosya.read()
c.execute ("INSERT INTO pdfler (cizim) VALUES(%s)" %(yuklenecek_dosya))
db.commit()
db.close()
ERROR with mysql.connector:
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 'b'%PDF-1.4\r%\xe2\xe3\xcf\xd3\r\n4 0 obj\r<</Linearized 1/L 83892/O 6/E 79669/N ' at line 1
ERROR with MySQLdb:
_mysql_exceptions.ProgrammingError: (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 'b'%PDF-1.4\\r%\\xe2\\xe3\\xcf\\xd3\\r\\n4 0 obj\\r<</Linearized 1/L 83892/O 6/E 79669/N ' at line 1")
You'll need to use parameters, not string interpolation.
String interpolation with SQL is vulnerable to SQL injection attacks anyway.
c.execute("INSERT INTO pdfler (cizim) VALUES (%s)", (yuklenecek_dosya,))

Python 3 MySQL Syntax Error with Select Parameter

I'm trying to execute the following via Flask's MySQLdb module:
cur.execute("SELECT post_id FROM tbl_post WHERE post_file_path = '%s'", (_filePath,))
Yet I get the following error:
1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'static/uploads/adc67db4-7d23-4bf1-a7ef-7e34dbed246a.jpg''' at line 1"
The query works fine via the command line so I'm fairly certain it's something to do with the way I'm providing my string argument. What am I doing wrong with it?
You shouldn't quote the placeholder %s, it's done by the database driver. This should work:
cur.execute("SELECT post_id FROM tbl_post WHERE post_file_path = %s", (_filePath,))

Categories

Resources