डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

Full Stack Web Development with Laravel

Full Stack Web Development with Laravel

Affiliate Program

Affiliate Program

Database connection between python and mysql

import mysql.connector
# creating new connection with mysql database
conn= mysql.connector.connect(
	host="localhost",
	user="root",
	password="",
	database="python_school"
	)
# print(conn)
cursor=conn.cursor()

try:
	# creating new database
	cursor.execute("CREATE DATABASE python_school")
except:
	print("Unable to create new database with name python_school, may be already exist.")
finally:
	# creating table user
	cursor.execute("CREATE TABLE IF NOT EXISTS users(id INT(10) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, password VARCHAR(40) NOT NULL)")
	print("New table users created successfully")

try:	
	cursor.execute("SELECT * FROM users")
	# fetching all records
	results= cursor.fetchall()	
	print(results)

	# fetching single record
	cursor.execute("SELECT * FROM users")
	user = cursor.fetchone()
	print(user)
except:
	print("Something wrong when fetching data from database")
© 2016 - 2023, All Rights are Reserved.