Python学习笔记之连接MySQLdb
来源:互联网 发布:淘宝代运营团队 编辑:程序博客网 时间:2024/05/21 08:49
基于Python2.7使用
# coding:utf8
import MySQLdb
conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = '123456',
db = 'test',
charset = 'utf8'
)
cursor = conn.cursor()
#查询数据
sql = 'select * from student'
cursor.execute(sql)
#输出有多条数据
print cursor.rowcount
#查询1条数据
rs = cursor.fetchone()
print rs
#查询3条数据
rs = cursor.fetchmany(3)
print rs
#查询全部
rs = cursor.fetchall()
#对数据进行格式化输出
for row in rs:
print 'id=%s,username=%s' % row
print rs
#输出连接信息
print conn
print cursor
cursor.close()
conn.close()
运行后控制台输出:
<_mysql.connection open to '127.0.0.1' at 26eb8e8>
<MySQLdb.cursors.Cursor object at 0x0000000002A19DA0>
pytho中对mysql数据库curd
# coding:utf8
import MySQLdb
conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = '123456',
db = 'test',
charset = 'utf8'
)
cursor = conn.cursor()
sql_insert = 'insert into student(id,username) values(1004,"张三")'
sql_delete = 'delete from student where id =1002'
print rs
rs = cursor.execute(sql_delete)
print rs
conn.commit()
cursor.close()
conn.close()
连接MySQLdb,需要MySQLdb服务
如果pip install MySQLdb失败,推荐网上直接下载安装Python MySQLdb服务。
https://pypi.python.org/pypi/MySQL-python/1.2.5
http://www.codegood.com/download/11/
python连接mysql数据库# coding:utf8
import MySQLdb
conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = '123456',
db = 'test',
charset = 'utf8'
)
cursor = conn.cursor()
#查询数据
sql = 'select * from student'
cursor.execute(sql)
#输出有多条数据
print cursor.rowcount
#查询1条数据
rs = cursor.fetchone()
print rs
#查询3条数据
rs = cursor.fetchmany(3)
print rs
#查询全部
rs = cursor.fetchall()
#对数据进行格式化输出
for row in rs:
print 'id=%s,username=%s' % row
print rs
#输出连接信息
print conn
print cursor
cursor.close()
conn.close()
运行后控制台输出:
<_mysql.connection open to '127.0.0.1' at 26eb8e8>
<MySQLdb.cursors.Cursor object at 0x0000000002A19DA0>
pytho中对mysql数据库curd
# coding:utf8
import MySQLdb
conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = '123456',
db = 'test',
charset = 'utf8'
)
cursor = conn.cursor()
sql_insert = 'insert into student(id,username) values(1004,"张三")'
sql_delete = 'delete from student where id =1002'
sql_update = 'update student set username="张三丰" where id =1004'
rs = cursor.execute(sql_insert)print rs
rs = cursor.execute(sql_delete)
print rs
rs = cursor.execute(sql_update)print rs
conn.commit()
cursor.close()
conn.close()
阅读全文
0 0
- Python学习笔记之连接MySQLdb
- python MySQLdb学习笔记
- python学习笔记 安装MySQLdb,连接mysql数据库
- python之mysql连接 MySQLdb
- Python模块学习之MySQLdb
- Python爬虫学习笔记--MySQLdb模块
- Python连接MYSQL --- MySQLdb
- python模块之MySQLdb: 用python连接mysql数据库
- python模块之MySQLdb: 用python连接mysql数据库
- Python MySQLdb连接数据库之phpmyadmin报错解决方法
- Python MySQLdb 学习总结
- python学习--MySQLdb
- Python MySQLdb 学习总结
- Python MySQLdb 学习总结
- Python 之 MySQLdb
- python 之 MySQLdb
- python之MySQLdb模块
- python使用mysqldb连接数据库
- 【树状数组--求逆序数(离散化)】poj2299 Ultra-QuickSort
- Groovy的灵活初始化与具名参数
- python 读写文本文件笔记
- hdu 2660 Accepted Necklace dp / dfs
- 采用Vivado HLS为视频处理实现中值滤波器和排序网络
- Python学习笔记之连接MySQLdb
- @property知多少
- 13扩展一个Greenplum System
- HDU5813 Elegant Construction
- Python--键值对配置文件处理
- 带有层号的先根遍历树存储基于plsql的代码实现
- 数组选择排序和冒泡排序
- Python编程_Lesson004_list数据类型
- 函数和包 PL/SQL