Skip to content

Instantly share code, notes, and snippets.

@pvergain
Last active November 20, 2017 13:56
Show Gist options
  • Save pvergain/88421fd1d2bb4d71cab4bf94b941d4cb to your computer and use it in GitHub Desktop.
Save pvergain/88421fd1d2bb4d71cab4bf94b941d4cb to your computer and use it in GitHub Desktop.
How to access a 10.0 SQL Anywhere database (2006) with Python 3.6
"""config.tests.conf_sybase.py
.. seealso::
- http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.sqlanywhere.12.0.1/dbprogramming/pg-python.html
"""
# https://github.com/sqlanywhere/sqlanydb
import sqlanydb
PARAMS_DBGPAO = {
'links': 'tcpip(host=X.Y.Z.A:2639)',
'eng': 'eng_X',
'dbn': 'db_X',
'uid': 'uid_X',
'pwd': 'pwd_X'
}
PARAMS_DBINTRANET = {
'links': 'tcpip(host=X.Y.Z.A:2638)',
'eng': 'eng_Y',
'dbn': 'db_Y',
'uid': 'uid_Y',
'pwd': 'pwd_Y'
}
def read_sybase_table(tablename: str, params) -> None:
"""Lecture des enregistrements d'une table de la base distante
"""
links = params['links']
eng = params['eng']
dbn = params['dbn']
uid = params['uid']
pwd = params['pwd']
try:
connexion_remote_intranet = sqlanydb.connect(
links=links,
eng=eng,
dbn=dbn,
uid=uid,
pwd=pwd,
)
cursor = connexion_remote_intranet.cursor()
cursor.execute(f"select * from {tablename};")
# Get a cursor description which contains column names
desc = cursor.description
print(f"Attributs de la table {tablename}\n")
for i, col in enumerate(range(len(desc))):
attribut = desc[col][0]
print(f"- {i+1}) {attribut}")
print("\n")
# Fetch all results from the cursor into a sequence,
# display the values as column name=value pairs,
# and then close the connection
rowset = cursor.fetchall()
for i, row in enumerate(rowset):
print(f"Enregistrement N°{i+1}:\n")
for col in range(len(desc)):
attribut = desc[col][0]
value = row[col]
print(f"- {attribut}='{value}'")
print("")
cursor.close()
connexion_remote_intranet.close()
except Exception as e:
print(f"Exception:{e} links:'{links}' dbn='{dbn}' on eng='{eng}' uid={uid}")
"""config.tests.test_read_x.py
"""
# https://github.com/google/python-fire/blob/master/docs/guide.md
import fire
from conf_sybase import (
PARAMS_DBGPAO,
read_sybase_table,
)
def test_read_clients():
"""Lecture des clients de la base distante dbgpao.id3semi
Exemple d'appel::
pipenv run python test_read_x.py test_read_clients
"""
read_sybase_table(tablename='CLIENTS', params=PARAMS_DBGPAO)
def test_read_fournisseurs():
"""Lecture des fournisseurs de la base distante dbgpao.id3semi
Exemple d'appel::
pipenv run python test_read_x.py test_read_fournisseurs
"""
read_sybase_table(tablename='FOURNISSEURS', params=PARAMS_DBGPAO)
def test_read_produits():
"""Lecture des produits de la base distante X
Exemple d'appel::
pipenv run python test_read_x.py test_read_produits
"""
read_sybase_table(tablename='PRODUITS', params=PARAMS_DBGPAO)
if __name__ == '__main__':
fire.Fire()
"""config.tests.test_read_y.py
"""
# https://github.com/google/python-fire/blob/master/docs/guide.md
import fire
from conf_sybase import (
PARAMS_DBINTRANET,
read_sybase_table,
)
def test_read_utilisateurs() -> None:
"""Lecture des utilisateurs de la base distante dbintranet.intranet
Exemple d'appel::
pipenv run python test_read_y.py test_read_utilisateurs
"""
read_sybase_table(
tablename='TR_UTILISATEUR',
params=PARAMS_DBINTRANET
)
def test_read_fiches_temps() -> None:
"""Lecture des fiches de temps de la base distante dbintranet.intranet
Exemple d'appel::
pipenv run python test_read_y.py test_read_fiches_temps
"""
read_sybase_table(
tablename='TR_FICHE_TEMPS',
params=PARAMS_DBINTRANET
)
if __name__ == '__main__':
fire.Fire()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment