This is a SQL Anywhere database backend for Django. The backend is distributed as a stand-alone python module. This backend has been tested with SQL Anywhere 12 and Django versions 1.2.7, 1.3.3, and 1.4.1.
This driver is licensed under the terms of the BSD license. See the file name "LICENSE" included with this package for more information.
Getting Started
- Install the required software
- SQL Anywhere 12.0.1 The SQL Anywhere Web Edition is a free, full-featured version for development and deployment of browser based applications. If you don't already have a license for SQL Anywhere, the Web Edition is a great place to start. Get the Web Edition here.
- Python (2.4 or greater) Install Python if you don't already have it installed. We recommend Python 2.7 but any version greater than 2.4 is supported. Note however that Python 3 introduced backwards incompatible features and many projects including Django do not currently have support for Python 3. You can download python from here.
If you are running on Linux you will most likely also be able to find
python through your distribution's package management system. - Python setuptools The setuptools project for python acts as a package manager for Python code. Using setuptools will make it trivial to install the correct version of Django to use with SQL Anywhere. You can get setuptools for python from here.
Again, if you are running on Linux you most likely be able to find
setuptools through your distribution's package management
system. This package is called "python-setuptools" on Ubuntu and
"python-setuptools-devel" on Fedora. - Django 1.4 Once you have installed setuptools, installing Django is a snap, simply run:
$ easy_install Django==1.4
- Python SQL Anywhere Database Interface The SQL Anywhere Database Interface for Python provides a Database API v2 compliant driver (see Python PEP 249) for accessing SQL Anywhere databases from Python. The SQL Anywhere backend for Django is built on top of this interface so installing it is required.
You can obtain the Python SQL Anywhere Database Interface here.
Install the driver by extracting the archive and running the following
command in the resulting directory:
$ python setup.py install
- SQL Anywhere Django Backend You can obtain the SQL Anywhere Database backend for Django here.
Install the backend by extracting the archive and running the following
command in the resulting directory:
$ python setup.py install
correctly -- the most important of which are PATH and LD_LIBRARY_PATH. The SQL Anywhere install creates a file named sa_config.sh to set all necessary environment variables automatically (Note the file is named sa_config.csh if you are using a csh derivative as your shell).
This file is located in the "bin32" and/or the "bin64" directories of
your install. Before trying to run the SQL Anywhere server or connect
to a running server in a given shell you should make sure to source
the file (with the "." command) corresponding to the bitness of the
SQL Anywhere binaries you want to use. For example, if the product is
installed in /opt/sqlanywhere12 you should run:
$ . /opt/sqlanywhere12/bin32/sa_config.sh
$ dbinit -z UCA django.db
If all goes well SQL Anywhere will have created a new database filenamed 'django.db' in the directory where you ran the dbinit
command. Feel free to move this database file to any location you
want. You can even copy it to a machine running a different operating
system if you wish.
$ dbsrv12 django.db
$ django-admin.py startproject mysite
Then edit the file mysite/mysite/settings.py and change the DATABASESsetting to match what is given below:
DATABASES = {
'default' : {
'ENGINE': 'sqlany_django',
'NAME': 'django',
'USER': 'dba',
'PASSWORD': 'sql',
'HOST': 'myhost',
'PORT': 'portnum'
}
}
Here's how the parameters correspond to SQL Anywhere connection parameters:- NAME = DBN
- USER = USR
- PASSWORD = PWD
- HOST = HOST
- PORT = (port number in host, i.e. myhost:portnum)
with the key "OPTIONS", like this:
DATABASES = {
'default' : {
'ENGINE': 'sqlany_django',
'NAME': 'django',
'USER': 'dba',
'PASSWORD': 'sql',
'OPTIONS': {'eng': 'django'}
}
}
Note: SQL Anywhere allows you to run several database servers on onemachine. For this reason you should always specify the server you want
to connect to as well as the database name. However if you want to connect to a server running in a SA OnDemand (cloud) environment, you should specify the NAME and HOST (and optionally PORT) options, and not specify the server name.
import sqlanydb
conn = sqlanydb.connect(uid='dba', pwd='sql', eng='django', dbn='django')
curs = conn.cursor()
curs.execute("select 'Hello, world!'")
print "SQL Anywhere says: %s" % curs.fetchone()
curs.close()
conn.close()
Run the test script and ensure that you get the expected output:$ python test_sqlany.py
SQL Anywhere says: Hello, world!
To test that Django can make use of the SQL Anywhere Database backendsimply change to the "mysite" directory created in step 5 and ask
Django to create the tables for the default applications.
$ python manage.py syncdb
If you don't receive any errors at this point thencongratulations. Django is now correctly configured to use SQL
Anywhere as a backend.
Links:
- SQL Anywhere Online Documentation
- SQL Anywhere Development Forum