S
S
Spoon in the brain2019-07-31 18:50:52
MySQL
Spoon in the brain, 2019-07-31 18:50:52

Is it possible to do something similar in python?

Good evening, I worked on the site and thought about how to make it easier to access the database, yes, I know about sqlalchemy and so on. but since I'm a beginner I need to learn everything!

class Set(object):
  def __init__(self, table, column, value, value_id):
    conn = db.connect()
    cursor = conn.cursor()
    cursor.execute('''UPDATE %s SET %s = %s WHERE id = %s''', (table, column, value, value_id))
    conn.commit()


@Init.route('/')
def init():
  Set(table = "app", column = "state", value = "true", value_id = 1)
  return ('success')

There is an instance of the class into which I pass all the necessary variables, the initialization function also accepts all these variables and already works with them with the library Flask-MySQL, from below I have a router that transmits the very information necessary for the class to work.
To my regret, nothing happened, the traceback looks like this:
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 ''app' SET 'state' = 'true' WHERE id = 1' at line 1")

I understand that he is complaining about
Set(table = "app", column = "state", value = "true", value_id = 1)
namely, with quotes, MySQL does not accept them, and if you try without them, then this is tantamount to assigning a variable to a variable.
So, is it possible to do something similar in python?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-07-31
@alternativshik

if state is in the database as a BooleanField, then it is not a string that needs to be passed there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question