返回顶部

收藏

MySQLdb 使用方法

更多

MySQLdb是Python 连接 MySQL 的模块,下面是它一些简单的使用方法: ```python

-- coding: utf-8 --

mysqldb

import time, MySQLdb

连接

conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8")
cursor = conn.cursor()

写入

sql = "insert into user(name,created) values(%s,%s)"
param = ("aaa",int(time.time()))
n = cursor.execute(sql,param)
print n

更新

sql = "update user set name=%s where id=3"
param = ("bbb")
n = cursor.execute(sql,param)
print n

查询

n = cursor.execute("select * from user")
for row in cursor.fetchall():
for r in row:
print r

删除

sql = "delete from user where name=%s"
param =("aaa")
n = cursor.execute(sql,param)
print n
cursor.close()

关闭

conn.close() ```本文由坤子的blog原创

标签:python

收藏

0人收藏

支持

0

反对

0

发表评论