python pyodbc使用方法

1、连接数据库

pip install pyodbc 成功后就可以用了

首先要import pyodbc

1)直接连接数据库和创建一个游标(cursor)

  1. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
  2. cursor = cnxn.cursor()

2)使用DSN连接。通常DSN连接并不需要密码,还是需要提供一个PSW的关键字。

  1. cnxn = pyodbc.connect('DSN=test;PWD=password')
  2. cursor = cnxn.cursor()

关于连接函数还有更多的选项,可以在pyodbc文档中的 connect funtion 和 ConnectionStrings查看更多的细节

2、数据查询(SQL语句为 select ...from..where)

1)所有的SQL语句都用cursor.execute函数运行。如果语句返回行,比如一个查询语句返回的行,你可以通过游标的fetch函数来获取数据,这些函数有(fetchone,fetchall,fetchmany).如果返回空行,fetchone函数将返回None,而fetchall和fetchmany将返回一个空列。

  1. cursor.execute('select user_id, user_name from users')
  2. row = cursor.fetchone()
  3. if row:
  4. print row

2)Row这个类,类似于一个元组,但是他们也可以通过字段名进行访问。

  1. cursor.execute('select user_id, user_name from users')
  2. row = cursor.fetchone()
  3. print 'name:', row[1] # access by column index
  4. print 'name:', row.user_name # or access by name

3)如果所有的行都被检索完,那么fetchone将返回None.

  1. while 1:
  2. row = cursor.fetchone()
  3. if not row:
  4. break
  5. print 'id:', row.user_id

4)使用fetchall函数时,将返回所有剩下的行,如果是空行,那么将返回一个空列。(如果有很多行,这样做的话将会占用很多内存。未读取的行将会被压缩存放在数据库引擎中,然后由数据库服务器分批发送。一次只读取你需要的行,将会大大节省内存空间)

  1. cursor.execute('select user_id, user_name from users')
  2. rows = cursor.fetchall()
  3. for row in rows:
  4. print row.user_id, row.user_name

5)如果你打算一次读完所有数据,那么你可以使用cursor本身。

  1. cursor.execute('select user_id, user_name from users'):
  2. for row in cursor:
  3. print row.user_id, row.user_name

6)由于cursor.execute返回一个cursor,所以你可以把上面的语句简化成:

  1. for row in cursor.execute('select user_id, user_name from users'):
  2. print row.user_id, row.user_name

7)有很多SQL语句用单行来写并不是很方便,所以你也可以使用三引号的字符串来写:

  1. cursor.execute('''
  2. select user_id, user_name
  3. from users
  4. where last_logon < '2001-01-01'
  5. and bill_overdue = 'y'
  6. ''')

3、参数

1)ODBC支持在SQL语句中使用一个问号来作为参数。你可以在SQL语句后面加上值,用来传递给SQL语句中的问号。

  1. cursor.execute('''
  2. select user_id, user_name
  3. from users
  4. where last_logon < ?
  5. and bill_overdue = ?
  6. ''', '2001-01-01', 'y')

这样做比直接把值写在SQL语句中更加安全,这是因为每个参数传递给数据库都是单独进行的。如果你使用不同的参数而运行同样的SQL语句,这样做也更加效率。

3)python DB API明确说明多参数时可以使用一个序列来传递。pyodbc同样支持:

  1. cursor.execute('''
  2. select user_id, user_name
  3. from users
  4. where last_logon < ?
  5. and bill_overdue = ?
  6. ''', ['2001-01-01', 'y'])
  1. cursor.execute('select count(*) as user_count from users where age > ?', 21)
  2. row = cursor.fetchone()
  3. print '%d users' % row.user_count

4、数据插入

1)数据插入,把SQL插入语句传递给cursor的execute函数,可以伴随任何需要的参数。

  1. cursor.execute('insert into products(id, name) values ('pyodbc', 'awesome library')')
  2. cnxn.commit()
  1. cursor.execute('insert into products(id, name) values (?, ?)', 'pyodbc', 'awesome library')
  2. cnxn.commit()

注意调用cnxn.commit()函数:你必须调用commit函数,否者你对数据库的所有操作将会失效!当断开连接时,所有悬挂的修改将会被重置。这很容易导致出错,所以你必须记得调用commit函数。

5、数据修改和删除

1)数据修改和删除也是跟上面的操作一样,把SQL语句传递给execute函数。但是我们常常想知道数据修改和删除时,到底影响了多少条记录,这个时候你可以使用cursor.rowcount的返回值。

  1. cursor.execute('delete from products where id <> ?', 'pyodbc')
  2. print cursor.rowcount, 'products deleted'
  3. cnxn.commit()

2)由于execute函数总是返回cursor,所以有时候你也可以看到像这样的语句:(注意rowcount放在最后面)

  1. deleted = cursor.execute('delete from products where id <> 'pyodbc'').rowcount
  2. cnxn.commit()

同样要注意调用cnxn.commit()函数

6、小窍门

1)由于使用单引号的SQL语句是有效的,那么双引号也同样是有效的:

deleted = cursor.execute('delete from products where id <> 'pyodbc'').rowcount

2)假如你使用的是三引号,那么你也可以这样使用:

  1. deleted = cursor.execute('''
  2. delete
  3. from products
  4. where id <> 'pyodbc'
  5. ''').rowcount

3)有些数据库(比如SQL Server)在计数时并没有产生列名,这种情况下,你想访问数据就必须使用下标。当然你也可以使用“as”关键字来取个列名(下面SQL语句的“as name-count”)

  1. row = cursor.execute('select count(*) as user_count from users').fetchone()
  2. print '%s users' % row.user_count

4)假如你只是需要一个值,那么你可以在同一个行局中使用fetch函数来获取行和第一个列的所有数据。

  1. count = cursor.execute('select count(*) from users').fetchone()[0]
  2. print '%s users' % count

如果列为空,将会导致该语句不能运行。fetchone()函数返回None,而你将会获取一个错误:NoneType不支持下标。如果有一个默认值,你能常常使用ISNULL,或者在SQL数据库直接合并NULLs来覆盖掉默认值。

maxid = cursor.execute('select coalesce(max(id), 0) from users').fetchone()[0]

在这个例子里面,如果max(id)返回NULL,coalesce(max(id),0)将导致查询的值为0。

(0)

相关推荐

  • Python3操作MySQL数据库

    在Python3中操作MySQL数据库 在Python3中使用mysql数据库需要安装pymysql库 pip install pymysql 操作MySQL 导包 import pymysql 第一 ...

  • 简述fetchone()函数

    fetchone()函数报'NoneType' object is not subscriptable的错误今天有人向好程序员Python培训老师请教一道python操作mysql的题,我也是差一点掉 ...

  • Django 使用原生sql语句操作数据库

    Django 使用原生sql语句操作数据库 Django配置连接数据库: 只需要在 settings.py 文件中做好数据库相关的配置就可以了 DATABASES = {'default': { 'E ...

  • selenium+python自动化101-execute_script 方法获取 JavaScript 返回值

    前言 之前经常使用 execute_script() 方法执行 JavaScript 的来解决页面上一些 selenium 无法操作的元素,但是一直无法获取执行的返回值. 最近翻文档,发现 execu ...

  • [638]python os.popen() 方法

    概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.popen(command[, mode[, bufsiz ...

  • Python深入---特殊方法与多范式

    恋习Python 1周前 作者:Vamei 出处:http://www.cnblogs.com/vamei Python一切皆对象,但同时,Python还是一个多范式语言(multi-paradigm ...

  • Android用python抓systrace方法

    Android用python抓systrace方法

  • Python File(文件) 方法 | 菜鸟教程

    Python File(文件) 方法 open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OS ...

  • Python win32api.ShellExecute方法代碼示例

    本文整理匯總了Python中win32api.ShellExecute方法的典型用法代碼示例.如果您正苦於以下問題:Python win32api.ShellExecute方法的具體用法?Python ...

  • 使用OpCode绕过Python沙箱的方法详解

    0x01 OpCode opcode又称为操作码,是将python源代码进行编译之后的结果,python虚拟机无法直接执行human-readable的源代码,因此python编译器第一步先将源代码进 ...

  • python 内置方法都有哪些?通过分类整合成一套简单的备忘教程

    一.入门函数 1.input() 功能: 接受标准输入,返回字符串类型 语法格式: input([提示信息]) 实例: # input 函数介绍text = input('请输入信息:')print( ...

  • Python os.join方法(用法详解)

    本文整理汇总了后端语言Python中os.join方法的典型用法及代码示例,这里的代码示例可能为您提供帮助.也可以进一步了解该方法所在模块os的用法示例. 如果想了解web前端内容,包括html,cs ...