Answers for "how to print out all rows in python from database"

0

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)
Posted by: Guest on February-25-2021

Code answers related to "how to print out all rows in python from database"

Python Answers by Framework

Browse Popular Code Answers by Language