企业建设网站的目标,网站设计怎么做,东莞培训机构,seo门户 site使用Python操作MySQL数据库
MySQL是一种关系型数据库管理系统#xff0c;它可以用来存储和管理大量的数据。之前介绍了大部分主流数据库#xff0c;今天将介绍如何使用Python来操作MySQL数据库。
安装MySQL
首先#xff0c;我们需要安装MySQL服务器#xff0c;可以从MyS…使用Python操作MySQL数据库
MySQL是一种关系型数据库管理系统它可以用来存储和管理大量的数据。之前介绍了大部分主流数据库今天将介绍如何使用Python来操作MySQL数据库。
安装MySQL
首先我们需要安装MySQL服务器可以从MySQL官网下载安装包也可以使用系统自带的包管理器安装。
安装MySQL驱动
接下来我们需要安装MySQL的驱动可以使用pip安装
pip install mysql-connector-python连接MySQL
接下来我们需要连接MySQL服务器可以使用MySQL Connector/Python模块中的connect()函数
import mysql.connector
# 连接MySQL服务器
conn mysql.connector.connect(hostlocalhost,userroot,passwd123456
)mysql指令
MySQL 指令用法作用SELECTSELECT * FROM table_name从表中检索数据INSERTINSERT INTO table_name VALUES (value1, value2,…)向表中插入新记录UPDATEUPDATE table_name SET column1value1, column2value2,…更新表中的记录DELETEDELETE FROM table_name WHERE condition从表中删除记录CREATECREATE TABLE table_name (column1 datatype, column2 datatype,…)创建新表ALTERALTER TABLE table_name ADD column_name datatype向表中添加新列DROPDROP TABLE table_name删除表
创建数据库
接下来我们可以使用MySQL Connector/Python模块中的cursor()函数创建一个游标然后使用execute()方法来执行SQL语句
# 创建数据库
cursor conn.cursor()
cursor.execute(CREATE DATABASE mydb)创建表
接下来我们可以使用MySQL Connector/Python模块中的execute()方法来创建表
# 创建表
cursor.execute(CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT))插入数据
接下来我们可以使用MySQL Connector/Python模块中的execute()方法来插入数据
# 插入数据
cursor.execute(INSERT INTO users (name, age) VALUES (John, 20))
cursor.execute(INSERT INTO users (name, age) VALUES (Bob, 25))查询数据
接下来我们可以使用MySQL Connector/Python模块中的execute()方法来查询数据
# 查询数据
cursor.execute(SELECT * FROM users)
# 获取查询结果
result cursor.fetchall()
# 打印结果
for row in result:print(row)更新数据
接下来我们可以使用MySQL Connector/Python模块中的execute()方法来更新数据
# 更新数据
cursor.execute(UPDATE users SET age 30 WHERE name John)删除数据
最后我们可以使用MySQL Connector/Python模块中的execute()方法来删除数据
# 删除数据
cursor.execute(DELETE FROM users WHERE name Bob)结束连接
最后我们需要使用MySQL Connector/Python模块中的close()方法来结束连接
# 结束连接
conn.close()学生管理系统demo
# 导入MySQL驱动
import mysql.connector
# 连接数据库
conn mysql.connector.connect(hostlocalhost, userroot, password123456, databasestudent_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
# 添加学生信息
def add_student():stu_no input(请输入学号)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(添加学生信息成功)
# 删除学生信息
def delete_student():stu_no input(请输入要删除的学号)sql delete from student where stu_no%!s(MISSING)cursor.execute(sql, (stu_no,))conn.commit()print(删除学生信息成功)
# 修改学生信息
def update_student():stu_no input(请输入要修改的学号)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(修改学生信息成功)
# 查询学生信息
def query_student():stu_no input(请输入要查询的学号)sql select * from student where stu_no%!s(MISSING)cursor.execute(sql, (stu_no,))result cursor.fetchone()if result:print(学号%!s(MISSING)姓名%!s(MISSING)年龄%!s(MISSING) %!((MISSING)result[0], result[1], result[2]))else:print(查无此人)
# 主函数
def main():if admin_login():while True:print(1. 添加学生信息)print(2. 删除学生信息)print(3. 修改学生信息)print(4. 查询学生信息)print(5. 退出系统)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()结论
本文介绍了如何使用Python来操作MySQL数据库包括安装MySQL服务器、安装MySQL驱动、连接MySQL、创建数据库、创建表、插入数据、查询数据、更新数据和删除数据等操作。 文章转载自: http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.nrxsl.cn.gov.cn.nrxsl.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.bqts.cn.gov.cn.bqts.cn