Lectures List
capscom technology computer course almora

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")
Author/Written by: Mr Hemant Singh (Founder)
Terms & Conditions CAPSCOM TECHNOLOGY Pvt. Ltd. का बेहतर रूप से संचालन करने के लिए यहॉ कुछ नियमें और शर्ते लागू है अगर आप कैप्सकॉम टैक्नोलॉजी में पढ़ रहे है तो यह आप पर लागू होता है।