How to return all rows and columns from SQL SELECT in MySQL database table with Python

1 Answer

0 votes
# c:\Users\user_nm\AppData\Local\Programs\Python\Python35-32\Scripts\pip install mysql-connector
   
import mysql.connector
   
db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="",
    database="python_database"
)
  
db_cursor = db.cursor()
  
db_cursor.execute("SELECT * FROM workers")

result = db_cursor.fetchall()

for row in result:
    print(row)

db.close()

'''
run:
 
(1, 'Fox', 'C Programmer')
(2, 'Aurora', 'C++ Programming')
(3, 'Amy', 'Python Programmer')
(4, 'Arthur', 'Java Programmer')
(5, 'R2D2', 'Super Programmer')

'''

 



answered Nov 30, 2018 by avibootz
edited Dec 1, 2018 by avibootz
...