支持SQL标准的可用数据库有不少,其中多数在Python中都有对应的客户端模块. java
这里我使用的mysql,它须要安装MySQLdb包.它至关于Python的数据接口规范Python DB API. python
root@10.1.1.45:~# apt-get install python-mysqldb root@10.1.1.45:~# python Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>这里导入MySQLdb没有报错,就说明安装成功.
下面就能够链接数据库,能够进行增删改操做. mysql
root@10.1.1.45:python# cat create.py #!/usr/bin/env python #coding=utf-8 #导入相关模块 import MySQLdb #创建和mysql数据库的链接 conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe') #获取游标 curs = conn.cursor() #执行SQL,建立一个数据库 curs.execute("create database pythondb") #选择链接哪一个数据库 conn.select_db('pythondb') #执行SQL,建立一个表 curs.execute("create table test(id int,message varchar(50))") #插入一条记录 value = [1,"davehe"] curs.execute("insert into test values(%s,%s)",value) #插入多条记录 values = [] for i in range(20): values.append((i,'hello mysqldb' + str(i))) curs.executemany("insert into test values(%s,%s)",values) #提交修改 conn.commit() #关闭游标链接,释放资源 curs.close() #关闭链接 conn.close() root@10.1.1.45:python# ./create.py下面利用python查看mysql里刚添加的记录.
root@10.1.1.45:python# cat select.py #!/usr/bin/env python #coding=utf-8 #导入相关模块 import MySQLdb #创建和mysql数据库的链接 conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226') #获取游标 curs = conn.cursor() #选择链接哪一个数据库 conn.select_db('pythondb') #查看共有多少条记录 count = curs.execute('select * from test') print "一共有%s条记录" % count #获取一条记录,以一个元组返回 result = curs.fetchone() print "当前的一条记录 ID:%s message:%s" % result #获取后10条记录,因为以前执行了getchone(),因此游标已经指到第二条记录,下面也就从第二条记录开始返回 results = curs.fetchmany(10) for r in results: print r #重置游标位置,0,为偏移量,mode = relative(默认) curs.scroll(0,mode='absolute') #获取全部记录 results = curs.fetchall() for r in results: print r #提交修改 conn.commit() #关闭游标链接,释放资源 curs.close() #关闭链接 conn.close()
root@10.1.1.45:python# ./select.py 一共有21条记录 当前的一条记录 ID:1 message:davehe (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (1L, 'davehe') (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (10L, 'hello mysqldb10') (11L, 'hello mysqldb11') (12L, 'hello mysqldb12') (13L, 'hello mysqldb13') (14L, 'hello mysqldb14') (15L, 'hello mysqldb15') (16L, 'hello mysqldb16') (17L, 'hello mysqldb17') (18L, 'hello mysqldb18') (19L, 'hello mysqldb19')
附: linux
connect函数的经常使用参数 sql
user #用户名 数据库
password #用户密码 app
host #主机名 函数
database #数据库名 fetch
connect函数会返回链接对象,链接对象方法 code
close() #关闭链接以后,链接对象和它的游标均不可用
commit() #若是支持的话就提交挂起的事务,不然不作任何事
rollback() #回滚挂起的事务(可能不可用)
cursor() #返回链接的游标对象
游标对象方法
close() #关闭游标
execute(oper[,params]) #执行SQL操做,可能使用参数
executemany(oper,pseq) #对序列中的每一个参数执行SQL操做
fetchone() #把查询的结果集中的下一行保存为序列,或者None
fetchmany([size]) #获取查询结果集中的多行,默认为1
fetchall() #将全部(剩余)的行做为序列的序列