SQLAlchemy Python tutorial
SQLAlchemy quick start tutorial for Python programmers
This tutorial helps Python programmers interact with FairCom servers.
Note
The <root>
directory for this driver is <faircom>\drivers\python.sql.sqlalchemy\
Install and start up the FairCom server if it is not currently running.
Install FairCom Python if not already installed.
Get started immediately with the source code found in the
<faircom>\drivers\python.sql.sqlalchemy\tutorials
directory, or proceed with the following instructions.
SQLAlchemy is one of the most widely used object-relational mapping tools in the Python community. It is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. More details about SQLAlchemy are available at SQLAlchemy.org.
The FairCom SQL Python interface provides support for this technology.
Note
This driver was specifically developed and tested on SQLAlchemy 1.0.19 with both Python 2.7 and Python 3.5-3.9. It supports SQLAlchemy 1.0 and higher and Python 2.7, Python 3.0 and higher.
If you have an existing application that uses SQLAlchemy, simply install the FairCom SQL dialect for SQLAlchemy to begin using the FairCom database.
If you are planning a new SQLAlchemy application, FairCom SQLAlchemy support enables you to take advantage of this powerful programming style.
FairCom also supports Python through a SQL Python interface.
FairCom recommends that the tutorials for the Python APIs be run using Python version 3 - preferably version 3.4+. Accordingly, in the instructions below, we have made the following assumptions about how the user's machine is configured:
Windows: The PATH should be configured so that invoking Python at the command-line will run version 3 of Python and invoking pip
at the command-line will run the instance of pip
that is associated with that version of Python.
Linux: The system should be configured so that invoking python3
at the command-line will run version 3 of Python, and invoking pip3
at the command-line will run the instance of pip
that is associated with that version of Python.
These assumptions are also made by the batch files (Windows) and make files (Linux) that FairCom provides to run the tutorials. Users must configure their machines to match the assumptions. Alternatively, you can modify the instructions in the batch or make files that follow to match the machine's configuration.
Bit Depth: The bit depth of the Python version 3 you are using must match the bit depth of the FairCom product you are using.
Make sure you have Python 2.7, Python 3.0 or higher installed and in your path.
Make sure you have SQLAlchemy 1.0 or higher installed.
Reminder:
setuptools
andwheel
are prerequisites before SQLAlchemy can be installed on Linux. If you do not have them installed, execute the following before installing SQLAlchemy:pip install setuptools
pip install wheel
Make sure you have the FairCom SQL Python driver installed. In a command window, change into the FairCom SQL SQLAlchemy dialect folder
<faircom>\drivers\python.sql.sqlalchemy
Execute the following command found in the above folder:
python setup.py install
Windows:
Launch a command prompt as administrator.
Run the following command:
pip install sqlalchemy
Change to the
<faircom>\drivers\python.sql.sqlalchemy
directory.Run the following command:
python setup.py install
Linux:
Bring up a shell.
Run the following command:
pip3 install sqlalchemy
Change to the
<faircom>/drivers/python.sql.sqlalchemy
directory.Run the following command:
python3 setup.py install
If any steps have permission problems, use sudo
to run that command.
If you get the following error:
setuptools_scm.version.SetuptoolsOutdatedWarning: your setuptools is too old (>12)
You must un-install the old version of the Python setuptools package and install a later version, as follows:
Python 3.x - Install the latest version of setuptools:
pip/pip3 install setuptools
Python 2.x - Install the last version of setuptools that still supports Python 2.x:
pip/pip3 install setuptools==44
After updating setuptools, try again.
To run the tutorial, use the batch file (Windows) or make file (Linux) in the <faircom>\drivers\python.sql.sqlalchemy\tutorials\cmdline
folder:
cd tutorials\cmdline
Windows:
BuildTutorials.bat
Linux:
make build; make run
On Linux, the build command copies a library to the correct location and the run command runs the tutorial.
The tutorial should print the following:
<User(id='1', name='u1')> [<Address(email='one')>, <Address(email='two')>]
Problem: "ImportError: No module named setuptools"
Solution: Install setuptools.
Problem: "pyctree missing"
Solution: Be sure you have installed pyctree.
Problem: sql.sqlalchemy can fail with "shared library not found."
Solution: Run one of the following:
For CSH (c shell):
setenv LD_LIBRARY_PATH /<faircom>/drivers/sql.python/
For BASH (Bourne-Again SHell):
export LD_LIBRARY_PATH=<faircom>/drivers/sql.python/
The Python tutorials currently use “127.0.0.1” to connect to the FairCom server on the local machine instead of using “localhost”. This is because Windows 10 often has “localhost” commented out of its “hosts” file (%systemroot%\System32\drivers\etc\hosts), as follows:
# localhost name resolution is handled within DNS itself. #127.0.0.1 localhost #::1 localhost
Unfortunately, Python does not seem to use DNS to resolve "localhost". This means that these tutorials cannot connect to the FairCom server on some versions of Windows if they use “localhost” instead of “127.0.0.1”. We use “127.0.0.1” in our Python tutorials to work around this problem.
If you are using IPv6, or have some other situation where “127.0.0.1” is not appropriate, feel free to update the call to create_engine() in the demonstration file, <faircom>\drivers\python.sql.sqlalchemy\tutorials\usage.py