With old Ziko python to connect to the database via Python _python

Source: Internet
Author: User
Tags create database

Using Python to write a Web site, you have to be able to manipulate the database through Python, the so-called Operation database, that is, through Python to the data connection, as well as records, fields of various operations. The type of operation mentioned in the previous lecture is that the reader operates the database directly through interactive mode.

Install Python-mysqldb

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

In programming, will encounter many similar interface program, through the interface program to another object to operate, relatively simple. Interface program is like a key, if you want to unlock, the person directly with the finger to poke, certainly is not, then must use tools, inserted into the keyhole, opened, opened the door, opened, you can operate the door inside the things. Then the tool that opens is the interface program. And the tools that open them will be convenient or not, if the key to use this lock, to facilitate, if the use of other tools, perhaps not convenient (in fact, also divided people, that is, the level of unlocking, if it is Jiangyangdadao or a thief or something, good at unlocking, with other tools are also convenient), that is, interface programs, coding levels are different are factors of concern.

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

After downloading, you can install it.

I can only demonstrate the installation process under Ubuntu .

Copy 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'd like to do a free endorsement for Ubuntu. Ha ha.

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

Copy Code code as follows:

>>> Import MySQLdb

In the Python interactive mode, enter the instructions above, if you do not complain, congratulations, has been installed. If the error, congratulations, you can use the wrong message to improve your computer level, please turn to Google great God.

Operation database connection database in interactive mode

The prerequisite for manipulating a database is to have a database first.

Create a database first.

Copy 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 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), 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark the Oracle Corporation and/or its
Affiliates. Names may trademarks of their respective
Owners.

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

Mysql>

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

Copy Code code as follows:

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

Note the instructions above, if you just type: Create Database qiwsirtest, but I added character set UTF8 in the back, meaning that the database was created qiwsirtest, the code is UTF-8, It is not garbled to deposit Chinese characters in this way.

See that line of hints: Query OK, 1 row Affected (0.00 sec), it means that 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 via the installed MySQLdb. Go into the Python interaction mode (now this lab is doing experiments).

Copy Code code as follows:

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

Explain the meaning of the above command individually:

Host: The address of the MySQL database should be filled in after the equal sign, because the database is on the local (also known as native), so use localhost to pay attention to quotes. If you are on another server, you should fill in the IP address here. Generally small and medium-sized sites, databases and programs are on the same server (computer) on the use of localhost.

User: Login to the database username, here generally fill in "root", or pay attention to quotes. Of course, if you are a larger service, the database will provide different users, and then you can change to the appropriate user. However, the permissions of different users may be different, so in a program, if you want to manipulate the database, you should also pay attention to the permissions you have. Here with Root, I feel relieved, what permissions are there. Doing so, however, should be avoided in large systems.

passwd: The above user account corresponds to the login MySQL password. The password I used in the example above is "123123". Don't forget the quotes.

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

Port: Generally, MySQL's default port is 3306, when MySQL is installed on the server, in order to be able to allow network access, the server (computer) to provide an access port to it.

CharSet: This setting, in many tutorials are not written, the results in the real data storage, found that there are garbled. Here I will qiwsirtest the encoding of this database to the UTF-8 format, so that it can be stored in Chinese characters without garbled. Note that in the 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 writing in accordance with host, user, passwd, db (Can not write), the port sequence can be written, pay attention to the port number port=3306 or not omitted as good, If there is no DB in front of the port, write 3306 directly will report an error.

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

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.