Python MySqlDB 增删改数据库
下载安装MySQLdb
http://sourceforge.net/projects/mysql-python/ 好像没看到windows版本for python2.6的下载,网上搜索到一个
http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
安装后import MySQLdb会出现 DeprecationWarning: the sets module is deprecated 这样一个警告,google之
原因是2.6不知sets这个模块,不过已经添加了set内置函数。找到MySQLdb文件夹的中__init__.py,注释掉from sets import ImmutableSet class DBAPISet(ImmutableSet):添加class DBAPISet(frozenset):;找到converters.py注释掉from sets import BaseSet, Set。然后修改第45行和129行中的Set为set。
搞定。
下面开始操作的demo:
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 =&nb
相关文档:
Python中Range和XRange的区别(Difference between Range and XRange in Python)
最近机器出了点问题,所以一直没有写新的东西出来。之前在读Python的代码的时候,发觉好多人喜欢用XRange,而不是Range,我也只是记得学习的时候开始学到的是Range,后面又看到有个XRange,当时也没有深究,为什么Python里面要有两个同样的功 ......
PLY模块 是Lex/YACCPython 的实现,可以用来实现词法分析/语法分析,但如何用,还没研究,以后有时间再研究吧;
主页: http://www.dabeaz.com/ply/
pycparser模块 是使用PLY模块分析c语言语法的模块,没什么文档,但模块自带了例子和测试用例。
主页: http://code.google.com/p/pycpa ......
1 在想要插入断点的地方插入代码
import pdb
pdb.set_trace()
2然后使用指令进行debug
查看代码上下文,l(小写L)
监视变量 ......
▾ hide table of contents
0. ↑ 显示完整目录
1. 深入#
2. 布尔类型#
3. 数值类型#
1. 将整数强制转换为浮点数及反向转换#
2. 常见数值运算#
3. 分数#
4. 三角函数#
5. 布尔上下文环境中的数值#
4. 列表#
1. 创建列表#
......
前几天,小许给我一份JavaQQ的源代码,用vim打开一看,发现里面的中文都是乱码。不用说,又是可恶的编码问题,在window下的文本文件通常使用GBK或GB18030编码,而在Linux下utf-8编码则大行其道。打开——另存为肯定不是上策,上网找编码批量转换工具也不是咱勤劳勇敢的程序员的作风。自已动手 ......