In-depth analysis of Python Package Technology

Source: Internet
Author: User

To fully master the Python Package Technology, you must understand the structure, statements, variables, functions, pre-processing commands, input and output of the Python package, the following is a study.

Object relationship Ing tools help increase productivity by providing classes and objects used to operate database tables. Python's best object relationship ing tool is SQLObject, an open source project. It completes almost all the operations required for programming databases.

This topic describes SQLObject and its functions. After reading this article, you will be able to connect Python to the database without writing any SQL code. When the object-oriented programming example satisfies the relational examples of most databases, the object relationship ing is usually seen. Object relationship ing is a bridge between the two worlds.

It allows you to define classes corresponding to database tables. Then you can use these classes and their methods on the instance to interact with the database, instead of writing SQL. Using Object link ing does not mean that you do not need to know how relational databases work, but do not need to write SQL statements to avoid programming errors.

You can find more than a dozen open-source Python packages for Operating SQL databases. This does not include the special-purpose modules used to connect Python to specific databases. SQLObject is the best module. It is a simple and easy-to-use full object link ing package. SQLObject can complete almost all the operations required by the programming database.

This article shows how SQLObject interacts with the database and how to use SQLObject to write database access and data verification code. SQLObject has a setup. py file, which is installed in the same way as any other Python package. If you are using Python V2.2, you also need to install the mxDateTime Python package SQLObject to use the built-in datetime module of Python V2.3, if this module is available ).

To actually use SQLObject, you need to set the database package and the Python interface of the database. SQLObject connects to multiple databases, including three major open source products: MySQL, PostgreSQL, and serverless SQLite. Finally, you need to create a database for the application. For SQLite, this means creating a file to store the database. For other databases, this means connecting to the database server.

Execute the create database Command and authorize DATABASE users to access the new DATABASE, so that SQLObject can be connected using this user account. The first Python code to be written is the database connection code. Based on the database used, this is the only place where different code needs to be written.

For example, if you want your application to use the SQLite database, you need to write the path of the database file to the sqlite connection builder in the sqlobject. SQLite package. If the database file does not exist, QLObject will tell SQLite to create one. The Code is as follows:

 
 
  1. mysql> use mysql;  
  2. Database changed  
  3. mysql> create database sqlobject_demo;  
  4. Query OK, 1 row affected (0.00 sec)  
  5. mysql> grant all privileges on sqlobject_demo to 'dbuser'@'localhost'   
  6. identified by 'dbpassword';  
  7. Query OK, 0 rows affected (0.00 sec)  
  8. mysql> flush privileges;  
  9. Query OK, 0 rows affected (0.00 sec)  

If you are using a Python package or other databases with servers, the database connection information is passed to the connection builder. Listing 2 provides an example of the MySQL database created in the previous section, I hope this article will teach you more things.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.