0% found this document useful (0 votes)
22 views1 page

SQL Connection

The document shows how to connect to a MySQL database, create a database and table, and insert data into the table using Python code. It connects to the local MySQL server, creates a database called school if it doesn't exist, creates a student table with fields for roll number, class, name, and marks, and inserts a record into the table by taking user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views1 page

SQL Connection

The document shows how to connect to a MySQL database, create a database and table, and insert data into the table using Python code. It connects to the local MySQL server, creates a database called school if it doesn't exist, creates a student table with fields for roll number, class, name, and marks, and inserts a record into the table by taking user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Connection:

import mysql.connector

mydb=mysql.connector.connect(host="localhost",user="root",passwd="root",database="school")

print(mydb)

Create database:

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="root")
mycursor=mydb.cursor()
mycursor.execute("create databaseif not exists school")
mycursor.execute("show databases")
for x in my cursor:
print(x)

Create table:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="root",database="school")
mycursor=mydb.cursor()
mycursor.execute("create table student(rollnoint(3)primary key,clas int,name varchar(20),marks
int(2))")

insert:

import mysql.connector

mydb=mysql.connector.connect(host="localhost",user="root",passwd="root",database="school")

mycursor=mydb.cursor()

rollno=int(input("Enter rollno"))

clas=int(input("Enter Class"))

name=input("Enter name")

marks=int(input("Enter marks"))

mycursor.execute("insert into student values('"+str(rollno)+"','"+name+"','"+str(clas)+"','"+str(marks)


+"')")

mydb.commit()

You might also like