Home

Tags

pyodbc, получение нескольких наборов данных с одного запроса MSSQL

2011-03-23 python pyodbc sql mssql linux

Пример под linux

# coding: utf8

import pyodbc

def sql_showtable(cur):
    if cur.description is None: return
    print cur.description
    # cur.fetchall()
    for line in cur:
        for c in line:
            if type(c) is str: c = c.decode('cp1251')
            print c,
        print

def sql_query(sql):
    conn = pyodbc.connect("DRIVER={FreeTDS};SERVER=127.0.0.1;"
        "UID=user;PWD=pasw;DATABASE=demodb", autocommit=True)
    cur = conn.cursor()

    cur.execute(sql)

    sql_showtable(cur)
    while cur.nextset():
        sql_showtable(cur)

    conn.close()

sql_query("select 1+2 as number1,2+3 as number2,4+5 as number3;"
          "select 'hello' as name ")

Результат
(('number1', <type 'int'>, None, 10, 10, 0, True), ('number2', <type 'int'>, None, 10, 10, 0, True), ('number3', <type 'int'>, None, 10, 10, 0, True))
3 5 9
(('name', <type 'str'>, None, 5, 5, 0, False),)
hello

comments powered by Disqus