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