Python dasturlash tilida mysql ma’lumotlar bazalariga kirish


Download 350.74 Kb.
Sana14.12.2022
Hajmi350.74 Kb.
#1006646
Bog'liq
Mysql


PYTHON DASTURLASH TILIDA MYSQL MA’LUMOTLAR BAZALARIGA KIRISH
REJA
1.MySQLCLIENT kutubxonasi
2.So’rovlarni bajarish
3.PyODBC kutubxonasi
4.So’rovlarni bajarish
MYSQLCLIENT KUTUBXONASI
Agar Python dasturlash tilida Mysql moduli o’rnatilmagan bo’lsa, u holda ekranga quyidagi xatolikni chiqaradi.
MYSQLCLIENT KUTUBXONASI
Yuqorida ta’kidlangan xatolikni bartaraf qilish uchun Siz Mysql.connector modulini o’rnatishingiz lozim.
python -m pip install mysql-connector-python
Shundan keyin, Mysql.connector moduli o’rnatiladi.
Agar dastur o’rnatilmasa, u holda undan oldingi modullarni o’rnatish kerak.
MYSQLCLIENT KUTUBXONASI
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword" ) print(mydb)
MYSQLCLIENT KUTUBXONASINI ULASH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password"
)
# cursor obektni cursor() metod orqali shakllantirish
cursorObject = dataBase.cursor()
# ma’lumotlar bazasini yaratish
cursorObject.execute("CREATE DATABASE gfg")
MYSQLDA JADVAL YARATISH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
studentRecord = """CREATE TABLE STUDENT (
NAME VARCHAR(20) NOT NULL,
BRANCH VARCHAR(50),
ROLL INT NOT NULL,
SECTION VARCHAR(5),
AGE INT
)"""
cursorObject.execute(studentRecord)
dataBase.close()# serverdan uzish
MYSQLDA JADVAL YARATISH NATIJASI
MYSQLDA JADVALGA MA’LUMOT KIRITISH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
sql = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE)\
VALUES (%s, %s, %s, %s, %s)"
val = ("Ram", "CSE", "85", "B", "19")
cursorObject.execute(sql, val)
dataBase.commit()
dataBase.close()
MYSQLDA JADVALGA MA’LUMOT KIRITISH NATIJASI
MYSQLDA JADVALGA MA’LUMOT KIRITISH 2-usul
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
sql = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE)\
VALUES (%s, %s, %s, %s, %s)"
val = [("Nikhil", "CSE", "98", "A", "18"),
("Nisha", "CSE", "99", "A", "18"),
("Rohan", "MAE", "43", "B", "20"),
("Amit", "ECE", "24", "A", "21"),
("Anil", "MAE", "45", "B", "20"),
("Megha", "ECE", "55", "A", "22"),
("Sita", "CSE", "95", "A", "19")]
cursorObject.executemany(sql, val)
dataBase.commit()
dataBase.close()
MYSQLDA JADVALGA MA’LUMOT KIRITISH NATIJASI
MYSQLDA JADVALDAN MA’LUMOT TANLASH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
query = "SELECT NAME, ROLL FROM STUDENT"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
dataBase.close()
MYSQLDA JADVALDAN MA’LUMOT TANLASH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
query = "SELECT * FROM STUDENT where AGE >=20"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
dataBase.close()
MYSQLDA JADVALDAN MA’LUMOT TANLASH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
query = "SELECT * FROM STUDENT ORDER BY NAME DESC"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
dataBase.close()
MYSQLDA JADVALDAN MA’LUMOT TANLASH
import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
cursorObject = dataBase.cursor()
query = "SELECT * FROM STUDENT LIMIT 2 OFFSET 1"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
dataBase.close()
Download 350.74 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling