Django的SQL Anywhere数据库后端

sqlany_django的Python项目详细描述


这是django的sql anywhere数据库后端。后端是 作为独立的python模块分发。这个后端已经 使用Django版本1.1.4在SQL Anywhere版本12、16和17中进行测试, 1.2.7、1.3.7、1.4.10、1.5.5、1.6.1、1.7.1和1.8.5。

  1. 安装所需的软件

    1. SQL Anywhere 12.0.0 (or higher)

    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 at http://www.sybase.com/detail?id=1057560

    1. python(2.4或更高版本)

    Install Python if you don’t already have it installed. We recommend Python 2.7 but any version greater than 2.4 is supported. Python 3 is supported in Django 1.6 and later. You can download python from http://www.python.org/download/

    If you are running on Linux you will most likely also be able to find python through your distribution’s package management system.

    1. python设置工具

    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 http://pypi.python.org/pypi/setuptools/

    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.

    1. Django

    Once you have installed setuptools, installing Django is a snap, simply run:

    $ easy_install Django
    

    If you want a specific version of Django, you can give the version using the == syntax. For example, if you want 1.6.1, you can use:

    $ easy_install Django==1.6.1
    
    1. python sql anywhere数据库接口

    If you are using pip to install the SQL Anywhere Django driver, you can skip this step since the SQL Anywhere Python driver will be installed as part of that step.

    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 use pip to make this easy:

    pip install sqlanydb
    

    Alternatively, you can obtain the Python SQL Anywhere Database Interface from https://github.com/sqlanywhere/sqlanydb. Install the driver by downloading the source and running the following command:

    $ python setup.py install
    
    1. SQL Anywhere Django后端

      同样,使用pip可以轻松安装:

      pip install sqlany-django
      

      如果还没有安装sql anywhere python驱动程序 安装。

      或者您可以从 https://github.com/sqlanywhere/sqlany-django/。后端安装方式 下载源并运行以下命令:

      $ python setup.py install
      
  2. 设置您的环境

    (Linux/Unix/Mac OS X only)

    SQL Anywhere requires several environment variables to be set to run 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 you are running 64-bit software and the product is installed in /opt/sqlanywhere16 you should run:

    $ . /opt/sqlanywhere16/bin64/sa_config.sh
    
  3. 创建数据库

    Issue the following command to create a new database to use with Django. Note that we are specifying the UCA collation so that that CHAR columns in the database will support unicode strings.

    $ dbinit -z UCA django.db
    

    If all goes well SQL Anywhere will have created a new database file named ‘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.

  4. 启动数据库服务器

    SQL Anywhere includes two different database servers – The personal server (dbeng12/dbeng16) and the network server (dbsrv12/dbsrv16). Both servers support the same complete set of features except that the personal server is limited to running on one CPU, allows a maximum of 10 concurrent connections and does not accept network connections from other machines. We will use the network server for our example.

    $ dbsrv16 django.db
    
  5. 配置django

    Creating a new Django site and configuring it to use SQL Anywhere is very easy. First create the site in the normal fashion:

    $ django-admin.py startproject mysite
    

    Then edit the file mysite/mysite/settings.py and change the DATABASES setting 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 = DatabaseName (DBN)
    • USER = Userid (UID)
    • PASSWORD = Password (PWD)
    • HOST = Host
    • PORT = (port number in host, i.e. myhost:portnum)

    如果需要指定其他连接参数(例如eng), 您可以使用“选项”键设置一个值,如下所示:

    DATABASES = {
       'default' : {
           'ENGINE': 'sqlany_django',
           'NAME': 'django',
           'USER': 'dba',
           'PASSWORD': 'sql',
           'OPTIONS': {'eng': 'django'}
       }
    }
    

    主机和端口默认为“localhost”和“2638”。如果要使用共享 内存,将主机和端口值设置为无:

    DATABASES = {
       'default' : {
           'ENGINE': 'sqlany_django',
           'NAME': 'django',
           'USER': 'dba',
           'PASSWORD': 'sql',
           'OPTIONS': {'eng': 'django'},
           'HOST': None,
           'PORT': None
       }
    }
    

    或者,可以使用 dbdsn实用程序,然后指定dsn连接参数。引擎 仍然必须指定参数。任何其他参数(如用户、主机等) 指定的将重写DSN中的值。例如:

    DATABASES = {
       'default' : {
           'ENGINE': 'sqlany_django',
           'OPTIONS': {'dsn': 'my_django_dsn'}
       }
    }
    

    注意:sql anywhere允许您在一个数据库服务器上运行多个数据库服务器 机器。因此,您应该始终指定所需的服务器 连接到以及数据库名称。但是如果你想连接到 在sa ondemand(云)环境中运行的服务器,应该指定 名称和主机(以及可选的端口)选项,not指定服务器名称。

  6. 测试以确保一切正常工作

    The SQL Anywhere database backend for Django makes use of the Python SQL Anywhere Database interface. We first want to test that this interface is working correctly before testing Django connectivity itself. Create a file named test_sqlany.py with the following contents:

    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 backend simply 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 then congratulations. Django is now correctly configured to use SQL Anywhere as a backend.

  7. 如果你有问题怎么办?

    If you run into problems, don’t worry. First try re-reading the instructions above and make sure you haven’t missed a step. If you are still having issues here are a few resources to help you figure out what went wrong. You can consult the documentation, or post to a forum where many of the SQL Anywhere engineers hang out.

    SQL Anywhere Online Documentation: http://dcx.sap.com/
    SQL Anywhere Development Forum: http://sqlanywhere-forum.sap.com/
  8. 从这里去哪里?

    SQL Anywhere should now be successfully configured as a backend for your Django site. To learn more about creating web applications with Django try the excellent series of tutorials provided by the Django project: http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01

许可证

此软件包是根据 许可证文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java 安卓 XMLPullParser错误   Java响应网格布局   Java中使用递归的不同方法   java变量可能尚未初始化(边加权图)   java如何将这样的文件插入MySQL数据库   java云端点:不允许实体类型的数组或集合   java(编译器或jvm)是否以不同方式处理类的静态最终成员?如果是,怎么做   java如何从lambda表达式返回新的非抽象映射?   java JDK 7支持的最大Spring版本是什么   我们如何从java类生成DTD   java在我的例子中BindingResult、FieldErrors或GlobalErrors是否可以为null?   java[LibGDX][GWT]读取文件外观时出错。HTML格式的json[序列化异常]   字典Java不可修改密钥集映射   java Admob在firebase sdk之后显示测试广告,但不显示真实广告