Answers for "the best way to connect to a database using python"

1

python project with database connectivity

#APPARENTLY IT IS NOT POSSIBLE TO UPLOAD TOO LONG CODES HERE
# AND MY PROJECT IS OVER 1800 LINES DISTRIBUTED OVER 3 FILES... SO
# HERE ARE THE BASICS... HOPE YOU CAN BUILD UP YOUR OWN PROJECT USING THIS
# THIS IS AN EXAMPLE JUST EXPLAINING THE BASICS OF DATABASE MANNAGEMENT IN PYTHON
# HOPE YOU CAN BUILD UP FROM HERE
import mysql.connector as MC
try:
    Con_o = MC.connect(host="localhost",user="root",passwd="sidspc12345",database="mydb")
    if Con_o.is_connected():
        print("Connection established successfully")
except Exception as E:
    print("Connection Failed !")
    print("ERROR : ",e)

Cur = Con_o.cursor()
Cur.execute("select * from  students where Percentage>= 95")

data = Cur.fetchall()

for row in data:
    print(row)

Con_o.commit()
st = "INSERT INTO students(Roll,Name,Sex,Stream,Phone,Address,Maths,Science,SST,English,Hindi) values({},'{}','{}','{}',{},'{}',{},{},{},{},{})".format(11021,'Siddharth','M','S',7002744892,'Second link road Silchar',100,100,100,94,88)
Cur.execute(st)
Con_o.commit()

Cur.execute("select * from  students")

data = Cur.fetchall()

for row in data:
    print(row)
Con_o.close()
Posted by: Guest on June-02-2021
4

how to connect to mysql database in python

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees')
cnx.close()
Posted by: Guest on March-23-2020

Code answers related to "the best way to connect to a database using python"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language