网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python之数据库操作

1.插入数据

站在用户的角度思考问题,与客户深入沟通,找到茅箭网站设计与茅箭网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖茅箭地区。

import MySQLdb

#创建连接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='test')
cur = conn.cursor()

# execute 执行,%s形式是为了防止sql注入
reCount = cur.execute('insert INTO test1 (name,country,age,address) VALUES (%s,%s,%s,%s,%s)',('2','jerry','china','23','beijing'))

'''
#另一种写法
sql = "insert INTO test1 (name,country,age,address) VALUES (%s,%s,%s,%s,%s)"
params = ('2','jerry','china','23','beijing')
reCount = cur.execute(sql,params)
'''

# 提交
conn.commit()
# 关闭连接
cur.close()
conn.close()

print reCount


2. 更新数据库
import MySQLdb
# 创建连接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='test')
cur = conn.cursor()

sql = "update test1 set address = %s where id = 2"
# 添加逗号表示是一个序列
params = ('BeiJing',)
reCount = cur.execute(sql,params)

# 提交
conn.commit()
# 关闭连接
cur.close()
conn.close()

print reCount


3. 查找数据
import MySQLdb
# 创建连接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='test')
cur = conn.cursor()

sql = "select * FROM test1 "
reCount = cur.execute(sql)

#显示查找到的数据,结果以字典形式展示
print cur.fetchall()

# 关闭连接cur.close()
conn.close()

print reCount

4. 删除数据
import MySQLdb
# 创建连接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='test')
cur = conn.cursor()


sql = "delete FROM test1 WHERE id = 2"
reCount = cur.execute(sql)

# 提交
conn.commit()
# 关闭连接
cur.close()
conn.close()

print reCount


名称栏目:python之数据库操作
本文网址:http://bjjierui.cn/article/ihshcc.html

其他资讯