I am working on a simple python script for retrieving information from a mysql database. Here are my two examples which are almost IDENTICAL and the first successfully compiles while the second returns:
File "dbconnection.py", line 17
print ip
^
SyntaxError: invalid syntax
I have tried deleting the try catch code from the second example with no results though. There is a relevant post on syntax error on Python 3 which makes my second example successfully run but it doesn't answer the question "why these almost identical examples have different behavior".
First example:
import datetime
import mysql.connector
cnx = mysql.connector.connect(user='user', database='test', password='test')
cursor = cnx.cursor()
query = ("SELECT ip FROM guralps")
cursor.execute(query)
for (ip) in cursor:
print ip
cursor.close()
cnx.close()
Second which does not compile:
from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector
from mysql.connector import errorcode
import time
try:
cnx = mysql.connector.connect(user='user', database='test', password='test')
cursor = cnx.cursor()
query = ("SELECT ip FROM guralps")
cursor.execute(query)
for (ip) in cursor:
print ip
cursor.close()
cnx.close()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exists")
else:
print(err)
else:
cnx.close()