商城網(wǎng)站建設(shè)預(yù)算要多少錢關(guān)鍵詞推廣方式
使用Python操作MySQL數(shù)據(jù)庫
MySQL是一種關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它可以用來存儲和管理大量的數(shù)據(jù)。之前介紹了大部分主流數(shù)據(jù)庫,今天將介紹如何使用Python來操作MySQL數(shù)據(jù)庫。
安裝MySQL
首先,我們需要安裝MySQL服務(wù)器,可以從MySQL官網(wǎng)下載安裝包,也可以使用系統(tǒng)自帶的包管理器安裝。
安裝MySQL驅(qū)動
接下來,我們需要安裝MySQL的驅(qū)動,可以使用pip安裝:
pip install mysql-connector-python
連接MySQL
接下來,我們需要連接MySQL服務(wù)器,可以使用MySQL Connector/Python模塊中的connect()函數(shù):
import mysql.connector
# 連接MySQL服務(wù)器
conn = mysql.connector.connect(host="localhost",user="root",passwd="123456"
)
mysql指令
MySQL 指令 | 用法 | 作用 |
---|---|---|
SELECT | SELECT * FROM table_name | 從表中檢索數(shù)據(jù) |
INSERT | INSERT INTO table_name VALUES (value1, value2,…) | 向表中插入新記錄 |
UPDATE | UPDATE table_name SET column1=value1, column2=value2,… | 更新表中的記錄 |
DELETE | DELETE FROM table_name WHERE condition | 從表中刪除記錄 |
CREATE | CREATE TABLE table_name (column1 datatype, column2 datatype,…) | 創(chuàng)建新表 |
ALTER | ALTER TABLE table_name ADD column_name datatype | 向表中添加新列 |
DROP | DROP TABLE table_name | 刪除表 |
創(chuàng)建數(shù)據(jù)庫
接下來,我們可以使用MySQL Connector/Python模塊中的cursor()函數(shù)創(chuàng)建一個游標(biāo),然后使用execute()方法來執(zhí)行SQL語句:
# 創(chuàng)建數(shù)據(jù)庫
cursor = conn.cursor()
cursor.execute("CREATE DATABASE mydb")
創(chuàng)建表
接下來,我們可以使用MySQL Connector/Python模塊中的execute()方法來創(chuàng)建表:
# 創(chuàng)建表
cursor.execute("CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)")
插入數(shù)據(jù)
接下來,我們可以使用MySQL Connector/Python模塊中的execute()方法來插入數(shù)據(jù):
# 插入數(shù)據(jù)
cursor.execute("INSERT INTO users (name, age) VALUES ('John', 20)")
cursor.execute("INSERT INTO users (name, age) VALUES ('Bob', 25)")
查詢數(shù)據(jù)
接下來,我們可以使用MySQL Connector/Python模塊中的execute()方法來查詢數(shù)據(jù):
# 查詢數(shù)據(jù)
cursor.execute("SELECT * FROM users")
# 獲取查詢結(jié)果
result = cursor.fetchall()
# 打印結(jié)果
for row in result:print(row)
更新數(shù)據(jù)
接下來,我們可以使用MySQL Connector/Python模塊中的execute()方法來更新數(shù)據(jù):
# 更新數(shù)據(jù)
cursor.execute("UPDATE users SET age = 30 WHERE name = 'John'")
刪除數(shù)據(jù)
最后,我們可以使用MySQL Connector/Python模塊中的execute()方法來刪除數(shù)據(jù):
# 刪除數(shù)據(jù)
cursor.execute("DELETE FROM users WHERE name = 'Bob'")
結(jié)束連接
最后,我們需要使用MySQL Connector/Python模塊中的close()方法來結(jié)束連接:
# 結(jié)束連接
conn.close()
學(xué)生管理系統(tǒng)demo
# 導(dǎo)入MySQL驅(qū)動
import mysql.connector
# 連接數(shù)據(jù)庫
conn = mysql.connector.connect(host='localhost', user='root', password='123456', database='student_management')
cursor = conn.cursor()
# 管理員登錄
def admin_login():username = input('請輸入管理員賬號:')password = input('請輸入管理員密碼:')sql = 'select * from admin where username=%!s(MISSING) and password=%!s(MISSING)'cursor.execute(sql, (username, password))result = cursor.fetchone()if result:print('登錄成功!')return Trueelse:print('登錄失敗!')return False
# 添加學(xué)生信息
def add_student():stu_no = input('請輸入學(xué)號:')stu_name = input('請輸入姓名:')stu_age = input('請輸入年齡:')sql = 'insert into student (stu_no, stu_name, stu_age) values (%!s(MISSING), %!s(MISSING), %!s(MISSING))'cursor.execute(sql, (stu_no, stu_name, stu_age))conn.commit()print('添加學(xué)生信息成功!')
# 刪除學(xué)生信息
def delete_student():stu_no = input('請輸入要刪除的學(xué)號:')sql = 'delete from student where stu_no=%!s(MISSING)'cursor.execute(sql, (stu_no,))conn.commit()print('刪除學(xué)生信息成功!')
# 修改學(xué)生信息
def update_student():stu_no = input('請輸入要修改的學(xué)號:')stu_name = input('請輸入新的姓名:')stu_age = input('請輸入新的年齡:')sql = 'update student set stu_name=%!s(MISSING), stu_age=%!s(MISSING) where stu_no=%!s(MISSING)'cursor.execute(sql, (stu_name, stu_age, stu_no))conn.commit()print('修改學(xué)生信息成功!')
# 查詢學(xué)生信息
def query_student():stu_no = input('請輸入要查詢的學(xué)號:')sql = 'select * from student where stu_no=%!s(MISSING)'cursor.execute(sql, (stu_no,))result = cursor.fetchone()if result:print('學(xué)號:%!s(MISSING),姓名:%!s(MISSING),年齡:%!s(MISSING)' %!((MISSING)result[0], result[1], result[2]))else:print('查無此人!')
# 主函數(shù)
def main():if admin_login():while True:print('1. 添加學(xué)生信息')print('2. 刪除學(xué)生信息')print('3. 修改學(xué)生信息')print('4. 查詢學(xué)生信息')print('5. 退出系統(tǒng)')choice = input('請輸入您的選擇:')if choice == '1':add_student()elif choice == '2':delete_student()elif choice == '3':update_student()elif choice == '4':query_student()elif choice == '5':breakelse:print('輸入錯誤,請重新輸入!')
if __name__ == '__main__':main()
結(jié)論
本文介紹了如何使用Python來操作MySQL數(shù)據(jù)庫,包括安裝MySQL服務(wù)器、安裝MySQL驅(qū)動、連接MySQL、創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和刪除數(shù)據(jù)等操作。