0

I keep getting a sql syntax error when I run the script to import the data into sql.

here is the data in question,

filtered_interfaces = (['interface Gi1/0/7']
filtered_tech =  ['description TECH_5750'])
cab =  u'10.210.44.5'

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 '['description TECH_5750'],['interface Gi1/0/7'],10.20.94.5)' at line 1")

Ive attempted dictation and tried to match values as it gets placed in to the database, I have also tried %s inplace of {}.

Im getting the same error as above on both of these methods.

sql = "INSERT INTO device (hostname, cab, localint) VALUES ({0},{1},{2})".format(filtered_tech,filtered_interface,cab)

mycursor.execute(sql)

Long story short this application logs into switches at our DC's. It looks for CDP LLDP devices then spits out all that info into an array. I then filter the info looking for hostname and local interface. The goal is to import that data into sql (Its different on each switch). Then retrieve data on a html page and edit the hostname if applicable. I had the editable piece working but it wasn't scalable hints the sql additions. Apologies if this is an easy fix im not a programmer by trade, just trying to make life easier and learn python.

1 Answer 1

1

If you are using MySQL behind your Python script, then you should be following the usage pattern in the documentation for prepared statements:

params = (filtered_tech, filtered_interface, cab,)
sql = """INSERT INTO device (hostname, cab, localint)
         VALUES (%s, %s, %s)"""
cursor.execute(sql, params)

# retrieve data here
Sign up to request clarification or add additional context in comments.

1 Comment

works perfectly, one more question when i run that insert can i do some sort of .append to an existing query ? Right now its appending a new row with the same data.

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.