Dans cet article on va voir comment on peut accéder a Sybase en utilisant Python. Y a plusieurs possibilités, j’en ai retenu deux :
1. Via PyODBC, qu’on déjà rencontré, dans ce cas rien ne change, voici un simple exemple pyodbc_sybase_demo.py qui affiche toutes les données des tables de la base « pubs2″ :
#! /usr/bin/env python import pyodbc # On se connecte a notre base (comme defini dans odbc.ini) db_connection = pyodbc.connect('DSN=SybasePubs2;UID=tarek1;PWD=123456') # On cree un curseur pour faire des requetes cursor = db_connection.cursor() # On execute une requete cursor.execute("SELECT * FROM sysobjects WHERE type = 'U'") print "Dumping all tables Data from Sybase pubs2 database :" # On parcours et on affiche le resultat de la requete while 1: row = cursor.fetchone() # La table 'au_pix' de la base 'pubs2' contient des donnees binaires # qu'on souhaite pas afficher if not row or (row[0] == 'au_pix'): break print "Dumping table:", row[0] print "\n" print "###########################################" print "\n" db_connection_table = pyodbc.connect('DSN=SybasePubs2;UID=tarek1;PWD=123456') table_cursor = db_connection_table.cursor() table_request = "SELECT * from " + row[0] table_cursor.execute(table_request) while 1: table_row = table_cursor.fetchone() if not table_row: break print table_row print "\n" print "###########################################" print "\n" table_cursor.close() db_connection_table.close() cursor.close() db_connection.close()
Qu’on execute (voici une partie du dump des tables):
(MyFirstVirtualEnv)toc@tocNewServer:~/Python_Projects/My_Python_Src/SYBASE$ ./pyodbc_sybase_demo.py Dumping all tables Data from Sybase pubs2 database : Dumping table: authors ########################################### ('172-32-1176', 'White', 'Johnson', '408 496-7223', '10932 Bigge Rd.', 'Menlo Park', 'CA', 'USA', '94025') ('213-46-8915', 'Green', 'Marjorie', '415 986-7020', '309 63rd St. #411', 'Oakland', 'CA', 'USA', '94618') ('238-95-7766', 'Carson', 'Cheryl', '415 548-7723', '589 Darwin Ln.', 'Berkeley', 'CA', 'USA', '94705') ('267-41-2394', "O'Leary", 'Michael', '408 286-2428', '22 Cleveland Av. #14', 'San Jose', 'CA', 'USA', '95128') ('274-80-9391', 'Straight', 'Dick', '415 834-2919', '5420 College Av.', 'Oakland', 'CA', 'USA', '94609') ('341-22-1782', 'Smith', 'Meander', '913 843-0462', '10 Mississippi Dr.', 'Lawrence', 'KS', 'USA', '66044') ('409-56-7008', 'Bennet', 'Abraham', '415 658-9932', '6223 Bateman St.', 'Berkeley', 'CA', 'USA', '94705') ('427-17-2319', 'Dull', 'Ann', '415 836-7128', '3410 Blonde St.', 'Palo Alto', 'CA', 'USA', '94301') ('472-27-2349', 'Gringlesby', 'Burt', '707 938-6445', 'PO Box 792', 'Covelo', 'CA', 'USA', '95428') ('486-29-1786', 'Locksley', 'Chastity', '415 585-4620', '18 Broadway Av.', 'San Francisco', 'CA', 'USA', '94130') ('527-72-3246', 'Greene', 'Morningstar', '615 297-2723', '22 Graybar House Rd.', 'Nashville', 'TN', 'USA', '37215') ('648-92-1872', 'Blotchet-Halls', 'Reginald', '503 745-6402', '55 Hillsdale Bl.', 'Corvallis', 'OR', 'USA', '97330') ('672-71-3249', 'Yokomoto', 'Akiko', '415 935-4228', '3 Silver Ct.', 'Walnut Creek', 'CA', 'USA', '94595') ('712-45-1867', 'del Castillo', 'Innes', '615 996-8275', '2286 Cram Pl. #86', 'Ann Arbor', 'MI', 'USA', '48105') ('722-51-5454', 'DeFrance', 'Michel', '219 547-9982', '3 Balding Pl.', 'Gary', 'IN', 'USA', '46403') ('724-08-9931', 'Stringer', 'Dirk', '415 843-2991', '5420 Telegraph Av.', 'Oakland', 'CA', 'USA', '94609') ('724-80-9391', 'MacFeather', 'Stearns', '415 354-7128', '44 Upland Hts.', 'Oakland', 'CA', 'USA', '94612') ('756-30-7391', 'Karsen', 'Livia', '415 534-9219', '5720 McAuley St.', 'Oakland', 'CA', 'USA', '94609') ('807-91-6654', 'Panteley', 'Sylvia', '301 946-8853', '1956 Arlington Pl.', 'Rockville', 'MD', 'USA', '20853') ('846-92-7186', 'Hunter', 'Sheryl', '415 836-7128', '3410 Blonde St.', 'Palo Alto', 'CA', 'USA', '94301') ('893-72-1158', 'McBadden', 'Heather', '707 448-4982', '301 Putnam', 'Vacaville', 'CA', 'USA', '95688') ('899-46-2035', 'Ringer', 'Anne', '801 826-0752', '67 Seventh Av.', 'Salt Lake City', 'UT', 'USA', '84152') ('998-72-3567', 'Ringer', 'Albert', '801 826-0752', '67 Seventh Av.', 'Salt Lake City', 'UT', 'USA', '84152') ########################################### Dumping table: publishers ########################################### ('0736', 'New Age Books', 'Boston', 'MA') ('0877', 'Binnet & Hardley', 'Washington', 'DC') ('1389', 'Algodata Infosystems', 'Berkeley', 'CA')
2. Via le module Sybase module for Python, qu’on peut installer comme suit :
– récupérer la dernière version python-sybase-0.40pre2.tar.gz, puis dezipper la
– ensuite :
cd python-sybase-0.40pre2
Modifier le fichier setup.py en ajoutant les deux lignes :
– ligne 90 :
syb_libs = ['sybblk', 'sybct']
juste après la ligne :
lib_names += ['sybblk', 'sybct', 'sybcs', 'sybtcl', 'sybinsck', 'sybcomn', 'sybintl', 'sybunic']
– lignes 126 et 127 :
syb_incdir = os.path.join(sybase, 'OCS-15_0/include') syb_libdir = os.path.join(sybase, 'OCS-15_0/lib')
Puis on installe (dans un virtualenv, si non faut passer par sudo):
python setup.py install
Et on crée un petit fichier demo_sybase_module_for_python.py pour tester tout ça :
#! /usr/bin/env python import Sybase # Connexion a la base "pubs2". 'TCOSERVER' est l'adresse du serveur telle que defini dans "/opt/sybase/interfaces" conn = Sybase.connect('TCOSERVER', 'tarek1', '123456', 'pubs2') # Creer un curseur cursor = conn.cursor() # Executer une requete cursor.execute("select * from titles where price > 15.00") # Recuperer et afficher le resultat rows = cursor.fetchall() for row in rows: print row
Qu’on exécute comme suit :
unset LANG # N'est peut etre pas necessaire export SYBASE=/opt/sybase export SYBASE_OCS=OCS-15_0 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/sybase/OCS-15_0/lib ./demo_sybase_module_for_python.py ('BU1032', "The Busy Executive's Database Guide", 'business ', '1389', 19.99, 5000.00, 4095, 'An overview of available database systems with emphasis on common business applications. Illustrated.', 'Jun 12 1986 12:00AM', 1) ('BU7832', 'Straight Talk About Computers', 'business ', '1389', 19.99, 5000.00, 4095, 'Annotated analysis of what computers can do for you: a no-hype guide for the critical user.', 'Jun 22 1987 12:00AM', 1) ('MC2222', 'Silicon Valley Gastronomic Treats', 'mod_cook ', '0877', 19.99, 0.00, 2032, 'Favorite recipes for quick, easy, and elegant meals, tried and tested by people who never have time to eat, let alone cook.', 'Jun 9 1989 12:00AM', 1) ('PC1035', 'But Is It User Friendly?', 'popular_comp', '1389', 22.95, 7000.00, 8780, "A survey of software for the naive user, focusing on the 'friendliness' of each.", 'Jun 30 1986 12:00AM', 1) ('PC8888', 'Secrets of Silicon Valley', 'popular_comp', '1389', 20.00, 8000.00, 4095, "Muckraking reporting by two courageous women on the world's largest computer hardware and software manufacturers.", 'Jun 12 1987 12:00AM', 1) ('PS1372', 'Computer Phobic and Non-Phobic Individuals: Behavior Variations', 'psychology ', '0877', 21.59, 7000.00, 375, 'A must for the specialist, this book examines the difference between those who hate and fear computers and those who think they are swell.', 'Oct 21 1990 12:00AM', 1) ('PS3333', 'Prolonged Data Deprivation: Four Case Studies', 'psychology ', '0736', 19.99, 2000.00, 4072, 'What happens when the data runs dry? Searching evaluations of information-shortage effects on heavy users.', 'Jun 12 1988 12:00AM', 1) ('TC3218', 'Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean', 'trad_cook ', '0877', 20.95, 7000.00, 375, 'Profusely illustrated in color, this makes a wonderful gift book for a cuisine-oriented friend.', 'Oct 21 1990 12:00AM', 1)
Si vous avez une idée de comment on peut installer le module officiel de Sybase pour Python (sybpydb) sur une machine Linux 32 bits, je suis preneur.