Python link local database in Ubuntu

Source: Internet
Author: User

Because the Python link database needs to download the DB API module: for example you need to access MySQL data, you need MySQL database module.

DB-API is a specification. To provide a consistent access interface for different underlying database systems and database interface programs.

Python's Db-api, which implements the interface for most databases, uses it to connect to each database and manipulate the databases in the same way.

1, the installation of the MySQL database module in Ubuntu requires the installation of a dependency package, the following commands:

sudo apt-get install Libmysqlclient-dev libmysqld-dev python-dev python-setuptools
2, install MYSQLDB after installing the dependent package
sudo apt-get install Python-mysqldb

Then test the link:

1 ImportMySQLdb as MDB2conn = Mdb.connect (host='127.0.0.1', port=3306, user='Root', passwd='123456', db='Test', charset='UTF8MB4') #链接数据库3cursor =conn.cursor ()4Cursor.execute ('SELECT * FROM Customer') #执行sql语句5R =Cursor.fetchall ()6 Printr# the results of a print query
Conn.close () #关闭数据库链接

Python link local database in Ubuntu

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.