0

I am using the following function in class implementation to insert values username_s , ipadress_s, and so on MYSQL database with column username , ipadress and so on but I keep getting the following error

mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'username_s' in 'field list'

The confusion is the interchange of column names with the column values

def insert_server(self , username_s , ipadress_s , BLACKLISTED_s , clientid_s):
    self.connect.database = self.m_database

    self.cursor.execute(
        "INSERT INTO server"
        " ( username , ipadress , blacklisted , clientid )"
        "VALUES ( username_s , ip_adress_s , BLACKLISTED_s , clientid_s)")

1 Answer 1

0

MySQL will interpret 'username_s' and the other names in VALUES as literal column names because there are no format codes indicating that these are actually variables. You can take a look at the example in the MySQL documentation Inserting data using Connector/Python. For clarification, here is a short MWE:

import sqlalchemy

# insert connection details
engine = sqlalchemy.create_engine('<YourConnection>')

# insert statement with variable format coding in VALUES
add_client = (
    'INSERT INTO server'
    ' (username, ipadress, blacklisted, clientid )'
    ' VALUES ( %s, %s, %s, %s)')

# invent some client data
username_s = 'Mary'
ipadress_s = '321.45.67.89'
BLACKLISTED_s = True
clientid_s = 123456
client_data = (username_s, ipadress_s, BLACKLISTED_s, clientid_s)

with engine.connect() as cnx:
    cnx.execute(add_client, client_data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.