python mysqldb
2015-08-23 16:26
513 查看
参考:http://blog.csdn.net/vincent_czz/article/details/7697039/
# -*- coding: utf-8 -*- #mysqldb import time, MySQLdb #连接 conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",charset="utf8") cursor = conn.cursor() #删除表 sql = "drop table if exists user" cursor.execute(sql) #创建 sql = "create table if not exists user(name varchar(128) primary key, created int(10))" cursor.execute(sql) #写入 sql = "insert into user(name,created) values(%s,%s)" param = ("aaa",int(time.time())) n = cursor.execute(sql,param) print 'insert',n #写入多行 sql = "insert into user(name,created) values(%s,%s)" param = (("bbb",int(time.time())), ("ccc",33), ("ddd",44) ) n = cursor.executemany(sql,param) print 'insertmany',n #更新 sql = "update user set name=%s where name='aaa'" param = ("zzz") n = cursor.execute(sql,param) print 'update',n #查询 n = cursor.execute("select * from user") for row in cursor.fetchall(): print row for r in row: print r #删除 sql = "delete from user where name=%s" param =("bbb") n = cursor.execute(sql,param) print 'delete',n #查询 n = cursor.execute("select * from user") print cursor.fetchall() cursor.close() #提交 conn.commit() #关闭 conn.close()
相关文章推荐
- Python 基础学习-类变量与对象变量
- 阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+部分答案)
- Python日志输出格式和时间格式
- python爬虫 - Urllib库及cookie的使用
- python爬虫 - Urllib库及cookie的使用
- Python属性、方法和类管理系列之----__slots__属性
- Python中通过注释修改编码格式的底层来源
- Python 爬虫框架 scrapy
- RPC协议及其python实例
- Python属性、方法和类管理系列之----属性初探
- Python抓取网页数据 生成 iOS plist 文件
- python中两个整数相除得到浮点数的方法
- python连接,操作pgsql
- python获取实时股票信息
- Python脚本:获取股票信息
- python获取实时股票信息
- Python脚本:获取股票信息
- 关于python抓取google搜索结果的若干问题
- 关于python抓取google搜索结果的若干问题
- python中操作MySQLdb