Python连接Mysql数据库
来源:互联网 发布:盛世投资 知乎 编辑:程序博客网 时间:2024/06/07 07:54
最近对Python比较感兴趣
写了一个简单的Demo来连接一下MySQL数据库,实现了简单的增删改查询功能,但是乱码问题还未能解决,等抽时间把所有乱码问题整理一下在写一篇博客。
希望大家多多指点:
注意:
1,Python连接Mysql 数据库需要MysqlDb(MySQL-python-1.2.4b4.win32-py2.7.exe)这个是我用的,貌似不支持Python3.0的,我的Python用的2.7 的版本,MysqlDb下载好之后,直接点击会默认安装到Python的路径下(安装MySQLDb之前,MySQL也是要安装好的,mysqldb的作用就是相当于让Python能够连接到mysql中个人理解)
控制台运行截图如下:
#!/usr/bin/python
#encoding=utf-8import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="ait",charset='utf8')
cursor =conn.cursor()
#删除
sql1="delete from user"
f=cursor.execute(sql1)
if(f>0):
print 'delete success'
conn.commit()
#插入
sql2='insert into user (username,password) value (11,22)'
f=cursor.execute(sql2)
if(f>0):
print 'insert success'
conn.commit()
#修改
sql3='update user set username=123,password=456'
f=cursor.execute(sql3)
if(f>0):
print 'update success'
conn.commit()
#查询
sql4 ="select * from user "
cursor.execute(sql4)
row=cursor.fetchone()
print row
#关闭
cursor.close()
conn.close()
阅读全文
1 0
- python连接mysql数据库
- python连接mysql数据库
- python连接MySQL数据库
- Python连接mysql数据库
- python连接mysql数据库
- Python连接mysql数据库
- Python连接mysql数据库
- Python连接MySql数据库
- Python 连接mysql数据库
- python连接mysql数据库
- Python连接mysql数据库
- Python连接mysql数据库
- python连接MySQL 数据库
- python 连接 MySQL 数据库
- python连接MySQL数据库
- python连接mysql数据库
- Python 连接 MySQL数据库
- python连接MySQL数据库
- applicationContext.xml中设置读取jdbc.properties文件
- Android使用EditText做图文混排
- 蓝牙nrf51822使用s110_nrf51_8.0.0协议栈遇到的 下载问题
- Docker初体验
- Android的HTTP请求方式
- Python连接Mysql数据库
- Ubuntu下Eclipse中文乱码解决
- 正旋波到同频率方波
- JavaScript进阶实战 ---> tab页
- linux源码 网络包接收--从中断到协议栈
- 不知道写个什么,就写个利用for循环打印空心菱形当笔记
- redis集群搭建步骤[在linux模拟搭建]
- 单链表的相关操作
- 问题 C(D): Josephus问题(Ⅰ) (Ⅱ)