With old Ziko python. Connect to the database via Python

Source: Internet
Author: User
Using Python to write a Web site, you must be able to manipulate the database through Python, the so-called operation of the database, through Python to achieve the connection to the data, as well as records, fields of various operations. The way of operation mentioned in the previous lecture is that crossing operates the database directly through interactive mode.


To manipulate the database through Python, you also need to install a library called MySQLdb on top of MySQL, an interface program that Python uses to implement various operations on MySQL data.

In programming, you will encounter a lot of similar interface programs, through the interface program for another object to operate, relatively simple. Interface program is like the key, if you want to unlock, people directly with the finger to poke, it is certainly not, then must use tools, inserted into the lock hole, opened, opened, the door opened, you can operate the door inside things. Then open the tool is the interface program. and open the tool will be convenient or not, if the key to use the lock, it is convenient, if using other tools, perhaps not convenient (in fact, people, that is, the level of unlocking, if it is thief or a thief or something, good at unlocking, with other tools also convenient), that is, the interface program, coding level is different, Are all factors of consideration.

Download python-mysqldb:https://pypi.python.org/pypi/mysql-python/here

After downloading, you can install it.

I can only demonstrate the process of installing under Ubuntu .

Copy the Code code as follows:

sudo apt-get install Python-mysqldb

Enter the command line above in the shell and install it. Look, how simple the installation, please quickly use Ubuntu Bar. I would like to be a free endorsement of Ubuntu. Ha ha.

No matter what the system, installation is not a problem. After installation, how to know the results of the installation?

Copy the Code code as follows:

>>> Import MySQLdb

In the Python interactive mode, enter the above instructions, if not an error, congratulations, have been installed. If the error, congratulations, you can use the wrong information to improve their computer level, please turn to Google God.

Connection database of operation database in interactive mode

The database is operational only if there is a database first.

Create a database first.

Copy the Code code as follows:

qw@qw-latitude-e4300:~$ Mysql-u Root-p
Enter Password:

After you open the database and enter the password correctly, the following results are rendered

Copy the Code code as follows:

Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 373
Server version:5.5.38-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c), the Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of the Oracle Corporation and/or its
Affiliates. Other names trademarks of their respective

Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.


In this state, enter the following command to create a database:

Copy the Code code as follows:

Mysql> CREATE database qiwsirtest character set UTF8;
Query OK, 1 row Affected (0.00 sec)

Note that the above instruction, if only input: Create Database qiwsirtest, but, I added the character set UTF8, meaning that the database qiwsirtest, encoding is Utf-8, It is not garbled to deposit Chinese characters in this way.

See that line hint: Query OK, 1 row Affected (0.00 sec), it means the database has been established, the name is called: qiwsirtest

Once the database is established, you can use Python to connect to the library named Qiwsirtest with the installed MySQLdb. Go to Python interactive mode (now this lab is doing experiments).

Copy the Code code as follows:

>>> Import MySQLdb
>>> conn = MySQLdb.connect (host= "localhost", user= "root", passwd= "123123", db= "Qiwsirtest", port=3306, charset= "UTF8")

Explain the meanings of the above commands one by one:

Host: The address of the MySQL database should be filled in after the equals sign, because the database is on this machine (also known as local), so use localhost, note the quotation marks. If you are on a different server, you should fill in the IP address. Generally small and medium-sized web sites, databases and programs are on the same server (computer), the use of localhost.

User: Log in to the database username, where the general "root", or pay attention to the quotation marks. Of course, if it is a larger service, the database will provide a different user, then it can be changed to the appropriate user. However, different users may have different permissions, so in a program, if you want to manipulate the database, you should also be aware of the permissions that you have. Here with root, you can rest assured, what permissions are there. However, this is something that should be avoided in large systems.

passwd: Login mysql password for the user account above. The password I used in the example above is "123123". Don't forget the quotation marks.

DB: Just the database created by the Create command, I built the database name is "Qiwsirtest", or to pay attention to the quotation marks. Crossing if the database name is not this, write the name of the database you created.

Port: In general, the default port for MySQL is 3306, and when MySQL is installed to the server, the server (computer) is going to provide a access port to it in order to allow network access.

CharSet: This setting, in many tutorials do not write, the results in the real data storage, the discovery of garbled. Here I will qiwsirtest this database encoding to the UTF-8 format, so that the Chinese characters are allowed to be stored without garbled. Note that in MySQL setup, Utf-8 is written as UTF8, without the middle horizontal line. But when you set the encoding format at the beginning of the Python file and elsewhere, write Utf-8. Remember!

Note: Connect in the host, user, passwd, etc. can not write, only in the time of writing in accordance with the host, user, passwd, db (Can not write), port sequence can be written, note the port number port=3306 or do not omit the good, If there is no DB in front of Port, direct write 3306 will error.

In fact, there are many parameters about connect, the following excerpt from MYSQLDB Official document content, all the parameters are listed, and related instructions, please crossing read carefully. However, the above several are commonly used, others are used to see the situation.

  • Related Article

    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.