python如何查询数据库_Python数据库操作手册-数据库专栏,SQL Server

数据库的操作在现在的python里面已经变得十分的好用,有了一套api标准.下面的就是讲讲如何的去使用这套框架定义.此框架包含以下部分模块接口 连接对象 游标对象 dbi辅助对象 数据类型与定义 如何实现的提示 从1.0到2.0的变化 例子 模块接口

connect(parameters…) 其中的参数格式如下:

dsn 数据源名称user 用户名(可选)password 密码(可选)host 主机名(可选)database 数据库名(可选)举个例子: connect(dsn=myhost:mydb,user=guido,password=234$)又或者 connect(218.244.20.22,username,password,databasename)

此标准规定了以下的一些全局变量:

apilevel:

表示了db-api的版本,分1.0和2.0.如果没有定义,默认为1.0

threadsafety:

0 threads may not share the module.1 threads may share the module, but not connections.2 threads may share the module and connections.3 threads may share the module, connections and cursors.

paramstyle:

用于表示参数的传递方法,分为以下五种:qmark 问号标识风格. e.g … where name=?numeric 数字,占位符风格. e.g … where name=:1named 命名风格. e.g where name=:nameformat ansi c printf风格. e.g … where name=%spyformat python扩展表示法. e.g … where name=%(name)s

异常类:

standarderror|__warning|__error |__interfaceerror |__databaseerror |__dataerror |__operationalerror |__integerityerror |__internalerror |__programmingerror |__notsupportederror

连接对象

连接对象包含如下方法:

.close() 关闭连接 .commit() 用于事务处理里面的提交操作 .rollback() 用于事务处理里面的回滚操作 .cursor() 获得一个游标 游标对象

游标对象包含如下属性和方法:

.description 一个列表(name,type_code,display_size,internal_size,precision,scale,null_ok) 此属性只有在取得了数据之后才有,不然会是null值 .rowcount 表示返回值的行数.如果没有执行executexxx()方法或者此模块没有实现这个方法,就会返回-1 .callproc(procname[,parameters]) (此为可选方法,应为不是所有的数据库都支持存储过程的) .close() 关闭游标 .execute(operation[,parameters]) 准备并执行一个数据库操作(包括查询和命令) .executemany(operation,seq_of_parameters) 准备一个数据库命令,然后根据参数执行多次命令 .fetchone() 返回第一行的查询结果 .fetchmany([size=cursor.arraysize]) 返回指定个多个行的值 .fetchall() 返回所有的查询结果 .arraysize 这个参数值表示fetchmany默认情况之下获取的行数 数据类型与定义定义一些常用的数据类型.但是目前用不到,就先不分析备注

当然,我们要知道的是,这个只是一个标准,一般来说标准里面定义了的会实现,但还有很多特定的实现,我们也需要去掌握哪些东西,不过如果我们将这些标准的掌握了,那么操作一般的就不会有问题了.

下面给出几个数据库相关的网址

database topic guide python的数据库使用向导,有相当不错的资料,包括api定义,驱动联结等等 mssql 驱动 就是mssql的驱动程序 例子

下面举的例子是以mssql为样板的,但是换成其他的驱动也一样可以做,这个就和perl的数据库操作十分的类似,可以让我们很方便的实现不同数据库之间的移植工作.

1. 查询数据

import mssqldb = mssql.connect(sql server ip, username, password, db_name)c = db.cursor()sql = select top 20 rtrim(ip), rtrim(dns) from detailc.execute(sql)for f in c.fetchall(): print “ip is %s, dns is %s” % (f[0], f[1])

2. 插入数据

sql = insert into detail values(192.168.0.1, www.dns.com.cn)c.execute(sql)

3. odbc的一个例子

import dbi, odbc # odbc modulesimport time # standard time moduledbc = odbc.odbc( # open a database connection sample/monty/spam # datasource/user/password )crsr = dbc.cursor() # create a cursorcrsr.execute( # execute some sql “”” select country_id, name, insert_change_date from country order by name “”” )print column descriptions: # show column descriptionsfor col in crsr.description: print , colresult = crsr.fetchall() # fetch the results all at onceprint \nfirst result row:\n , result[0] # show first result rowprint \ndate conversions: # play with dbidate objectdate = result[0][-1]fmt = %-25s%-20sprint fmt % (standard string:, str(date))print fmt % (seconds since epoch:, float(date))timetuple = time.localtime(date)print fmt % (time tuple:, timetuple)print fmt % (user defined:, time.strftime(%d %b %y, timetuple))——————————-output——————————–column descriptions: (country_id, number, 12, 10, 10, 0, 0) (name, string, 45, 45, 0, 0, 0) (insert_change_date, date, 19, 19, 0, 0, 1)first result row: (24l, argentina, )date conversions: standard string: fri dec 19 01:51:53 1997 seconds since epoch: 882517913.0 time tuple: (1997, 12, 19, 1, 51, 53, 4, 353, 0) user defined: 19 december 1997

回本栏首页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值