Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import mysql.connector
  2. from mysql.connector import errorcode
  3. def getDeveloperDetails(ID):
  4. try:
  5. mySQLConnection = mysql.connector.connect(host='localhost',
  6. database='python_db',
  7. user='pynative',
  8. password='pynative@#29')
  9.  
  10. cursor = mySQLConnection.cursor(prepared=True)
  11. sql_select_query = """SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;"""
  12.  
  13. cursor.execute(sql_select_query, (ID, ))
  14. record = cursor.fetchall()
  15. for row in record:
  16. print("Id = ", row[0], )
  17. print("Name = ", row[1])
  18. print("Join Date = ", row[2])
  19. print("Salary = ", row[3], "\n")
  20. except mysql.connector.Error as error:
  21. print("Failed to get record from database: {}".format(error))
  22. finally:
  23. # closing database connection.
  24. if (mySQLConnection.is_connected()):
  25. cursor.close()
  26. mySQLConnection.close()
  27. print("connection is closed")
  28. id1 = 1
  29. id2 = 2
  30. getDeveloperDetails(id1)
  31. getDeveloperDetails(id2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement