close

(1) 使用Pyodbc 連接syabse資料庫

import pyodbc
print ("Begin Syabse ODBC connect.....")

dsn="DSN=mysybase_db"
cnnctn=pyodbc.connect(dsn)
print("Successfully established connection....")
cnnctn.autocommit = True
cur = cnnctn.cursor()
cur.execute("select book_no,year,author from booksell where year = 2016")
while True:
   row = cur.fetchone()
   if (not row):
       break
   print("%s:%s" % (row[0],row[1]))
cur.close()
cnnctn.close()
input("press any key ccontinue...")

(2) 使用Podbc 連接access資料庫

import pyodbc

print ("Begin ACCESS ODBC connect.....")

DBfile = 'booksell.mdb'
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+DBfile)
print("Successfully established connection....")

#use below conn if using with Access 2007, 2010 .accdb file
#conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBfile)
cursor = conn.cursor()

SQL = 'select book_no,year,author from booksell where year = 2016;'
for row in cursor.execute(SQL):                  # cursors are iterable
    print row.book_no,row.year,row.author

 # print row
# if print row it will return tuple of all fields

cursor.close()
conn.close()

回主目錄

 

 

arrow
arrow
    全站熱搜

    stanley 發表在 痞客邦 留言(0) 人氣()