how to print out all rows in python from database code example

Example: how to print out all rows in python from database

import mysql.connector

# Connect to MySql
db = mysql.connector.connect(

	host="localhost",
	user="root",
	passwd="root",
	database="testdatabase"

	)

mycursor = db.cursor()

# Make sure you have created a database before:

# Creating a table named Test
mycursor.execute("CREATE TABLE Test (name VARCHAR(50) NOT NULL, gender ENUM('M', 'F') NOT NULL, id int PRIMARY KEY NOT NULL AUTO_INCREMENT)")

# Inserting the element in the table
mycursor.execute("INSERT INTO Test (name, created, gender) VALUES (%s, %s, %s)", ("Peter", datetime.now(), 'M'))

# Apply the Data to the server
db.commit()

# Iterating over all rows in the Database
for x in mycursor:
	print(x)

Tags:

Misc Example