Python MySQL进行数据库表变更和查询
作者:lqh 时间:2024-01-17 04:35:24
Python连接MySQL,进行数据库表变更和查询:
python mysql insert delete query:
#!/usr/bin/python
import MySQLdb
def doInsert(cursor,db):
#insert
# Prepare SQL query to INSERT a record into the database.
sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = '%c'" %('M')
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def do_query(cursor,db):
sql = "SELECT * FROM EMPLOYEE \
WHERE INCOME > '%d'" % (1000)
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
print 'resuts',cursor.rowcount
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income )
except:
print "Error: unable to fecth data"
def do_delete(cursor,db):
sql = 'DELETE FROM EMPLOYEE WHERE AGE > {}'.format(20)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def do_insert(cursor,db,firstname,lastname,age,sex,income):
sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
LAST_NAME, AGE, SEX, INCOME) \
VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
(firstname,lastname,age,sex,income)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
# Open database connection
# change this to your mysql account
#connect(server,username,password,db_name)
db = MySQLdb.connect("localhost","root","root","pydb" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
do_query(cursor,db)
doInsert(cursor,db)
do_query(cursor,db)
do_delete(cursor,db)
do_query(cursor,db)
do_insert(cursor,db,'hunter','xue',22,'M',2000)
do_insert(cursor,db,'mary','yang',22,'f',5555)
do_insert(cursor,db,'zhang','xue',32,'M',5000)
do_insert(cursor,db,'hunter','xue',22,'M',333)
do_query(cursor,db)
# disconnect from server
db.close()
之后可以在此基础上根据需要进行封装。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/jiyingying_up/article/details/9928905
标签:Python,MySQL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SingleFlight模式的Go并发编程学习
2024-04-29 13:05:39
![](https://img.aspxhome.com/file/2023/3/133923_0s.jpg)
SQL语句练习实例之二——找出销售冠军
2011-10-24 19:52:45
全面理解javascript的caller,callee,call,apply概念
2007-12-02 17:44:00
Python进阶之协程详解
2023-08-23 04:52:52
![](https://img.aspxhome.com/file/2023/0/62870_0s.png)
Python numpy之线性代数与随机漫步
2021-12-04 05:20:01
![](https://img.aspxhome.com/file/2023/2/112132_0s.png)
微信小程序报错:this.setData is not a function的解决办法
2024-04-19 09:47:17
firefox浏览器不支持innerText的解决方法
2024-04-10 10:51:42
网页设计中的对比原则与接近性原则
2010-03-30 14:51:00
![](https://img.aspxhome.com/file/UploadPic/20103/30/1121120-49s.jpg)
Python django框架 web端视频加密的实例详解
2022-08-10 06:05:04
![](https://img.aspxhome.com/file/2023/7/101107_0s.png)
利用python做数据拟合详情
2023-04-22 15:32:17
![](https://img.aspxhome.com/file/2023/1/103221_0s.png)
python批量翻译excel表格中的英文
2022-11-16 08:09:07
Win11平台安装和配置NeoVim0.8.2编辑器搭建Python3开发环境详细过程(2023最新攻略)
2023-06-06 21:28:07
![](https://img.aspxhome.com/file/2023/1/111871_0s.png)
Mootools 1.2教程(22)——同时进行多个形变动画
2008-12-29 14:11:00
基于python和flask实现http接口过程解析
2022-06-01 11:46:36
神经网络python源码分享
2021-10-07 10:41:00
![](https://img.aspxhome.com/file/2023/0/90790_0s.jpg)
兼容主流浏览器,纯CSS下拉菜单
2010-09-05 20:30:00
SQL实现LeetCode(181.员工挣得比经理多)
2024-01-17 03:15:01
Vue项目如何设置反向代理和cookie设置问题
2023-07-02 16:39:03
SQL Server 触发器实例详解
2024-01-28 05:40:46
![](https://img.aspxhome.com/file/2023/0/115300_0s.png)
php验证码的制作思路和实现方法
2023-09-04 13:23:37
![](https://img.aspxhome.com/file/2023/5/55505_0s.jpg)