博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习笔记之连接MySQLdb
阅读量:4228 次
发布时间:2019-05-26

本文共 1367 字,大约阅读时间需要 4 分钟。

基于Python2.7使用

连接MySQLdb,需要MySQLdb服务

如果pip install MySQLdb失败,推荐网上直接下载安装Python MySQLdb服务。

1.官网32位的

https://pypi.python.org/pypi/MySQL-python/1.2.5

2.这个是可以找到64位的

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()
 

转载地址:http://xkcqi.baihongyu.com/

你可能感兴趣的文章
ZLG7290键盘驱动开发心得
查看>>
WinCE中的虚拟地址和实际的物理地址是如何对应
查看>>
Microsoft Windows CE 的内存使用
查看>>
makefile入门
查看>>
中科院计算所Goddon CPU诞生历史!牛!
查看>>
ispPAC
查看>>
迷你型的 Linux 系统
查看>>
为人处世小技巧
查看>>
C++ 堆、栈及静态数据区详解
查看>>
VC++6.0编译环境介绍
查看>>
74芯片特性分类使总汇
查看>>
ttl和cmos的区别
查看>>
常见逻辑电平标准
查看>>
逻辑电平的含义
查看>>
103个Windows XP运行命令
查看>>
常用linux命令大全
查看>>
上下拉电阻的用法
查看>>
TTL和CMOS的区别
查看>>
什么是上拉电阻和下拉电阻,上拉电阻和下拉电阻有什么应用?
查看>>
基于嵌入式微处理器EP9315的二次开发技术
查看>>