Connect to the database through Python

Source: Internet
Author: User
Now I need to use the database when I am working on python, so I have reorganized the knowledge of the database and familiarized myself with the functions and functions of the MysqlDB module in python, now, let's systematically summarize how to compile a website using Python. you must be able to operate a database using python. The so-called database operation is to connect data through python, and various operations on records and fields. The operation method mentioned above is that the viewer operates the database directly through the interactive mode.

Install python-MySQLdb

To operate databases using python, you also need to install a database called mysqldb on the basis of mysql, which is an interface program. python implements various operations on mysql data.

In programming, there will be many similar interface programs. it is relatively simple to operate another object through the interface program. An interface program is like a key. if you want to unlock a lock, you can use your fingers to hack it directly. you must use a tool to insert it into the lock hole and open it, when the door opens, you can operate on the inside of the door. Open the tool as an interface program. However, opening the tools can be convenient or not. if you use the lock key, it will be convenient. if you use other tools, it may not be convenient (in fact, it is still divided into people, that is, the level of people unlocking the lock, if you are a hacker or a thief, you are good at unlocking and using other tools.) that is, the interface program is different and the encoding level is different.

Download python-mysqldb: https://pypi.python.org/pypi/MySQL-python/

After the download, you can install it.

I can only demonstrate it hereUbuntuThe installation process.

The code is as follows:


Sudo apt-get install python-MySQLdb

Enter the above command line in shell to install it. Check out how simple the installation is. please use ubuntu quickly. I am willing to make a free ubuntu endorsement. Haha.

Regardless of the system, installation is not a problem. After installation, how can I know the installation result?

The code is as follows:


>>> Import MySQLdb

In the interaction mode of python, enter the preceding command. If no error is reported, congratulations, you have installed it. If an error is reported, congratulations. you can use the error message to improve your computer level. please turn to google.

Database operation in interactive mode

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

Create a database first.

The code is as follows:


Qw @ qw-Latitude-E4300 :~ $ Mysql-u root-p
Enter password:

Open the database and enter the correct password. the following result is displayed:

The code is 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) 2000,201 4, Oracle and/or its affiliates. All rights reserved.

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

Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement.

Mysql>

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

The code is as follows:


Mysql> create database qiwsirtest character set utf8;
Query OK, 1 row affected (0.00 sec)

Note that the preceding command can be used if you only enter create database qiwsirtest. However, I added character set utf8 later, which indicates that the created database qiwsirtest is UTF-8 encoded, in this way, saving Chinese characters is not garbled.

When you see the row prompt: Query OK, 1 row affected (0.00 sec), it indicates that the database has been created and its name is qiwsirtest.

After the database is created, you can use python to connect to the database named qiwsirtest through mysqldb that has been installed. Go to the python interaction mode (now in this lab ).

The code is as follows:


>>> Import MySQLdb
>>> Conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "123123", db = "qiwsirtest", port = 3306, charset = "utf8 ")

Explain the meaning of the preceding commands one by one:

Host: enter the address of the mysql database after the equal sign, because the database is on the local machine (also called local), so use localhost. pay attention to the quotation marks. If it is on another server, enter the IP address here. Generally, for small and medium-sized websites, databases and programs are on the same server (computer) and localhost is used.

User: The username used to log on to the database. enter "root" here. pay attention to the quotation marks. Of course, if it is a relatively large service, the database will provide different users, then it can be changed to the corresponding user. However, different users may have different permissions. Therefore, if you want to operate the database in a program, pay attention to the permissions you have. With root, you can feel relieved that you have all the permissions. However, this should be avoided in large systems.

Passwd: password used to log on to mysql for the preceding user account. The password I used in the above example is "123123 ". Do not forget the quotation marks.

Db: The database just created through the create command. the database name I created is "qiwsirtest". pay attention to the quotation marks. If this is not the name of the database you have created, write the name of the database you have created.

Port: In general, the default port of mysql is 3306. After mysql is installed on the server, the server (computer) must provide an access port to allow network access.

Charset: This setting is not written in many tutorials. as a result, garbled characters are found during real data storage. Here I set the encoding of the qiwsirtest database to UTF-8 format, so that it can be stored in Chinese characters without garbled characters. Note: In mysql settings, UTF-8 is written as utf8 without a hyphen in the middle. However, when you set the encoding format at the beginning of the python file and elsewhere, you must write it as UTF-8. Remember!

Note: The host, user, passwd, and so on in connect can be left empty. only when writing data can be in the order of host, user, passwd, db (can be left empty), and port, note that port = 3306 should not be omitted. If no db is in front of port, an error will be reported if you write 3306 directly.

In fact, there are still a lot of connect parameters. the following excerpt is from the official mysqldb documentation, listing all the parameters and related instructions. Please read carefully. However, the above are commonly used, and others can be used as appropriate.

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.