- Hands-On Artificial Intelligence for IoT
- Amita Kapoor
- 218字
- 2021-07-02 14:02:01
The MySQL database engine
Though we can use SQLite for large databases, MySQL is generally preferred. In addition to being scalable for large databases, MySQL is also useful where data security is paramount. Before using MySQL, you will need to install the Python MySQL connector. There are many possible Python MySQL connectors such as, MySQLdb, PyMySQL, and MySQL; we will use mysql-connector-python.
In all three, after making a connection using the connect method, we define the cursor element and use the execute method to run different SQL queries. To install MySQL, we use the following:
pip install mysql-connector-python
- Now that the Python MySQL connector is installed, we can start a connection with the SQL server. Replace the host, user, and password configurations with your SQL server configuration:
import mysql.connector
connection = mysql.connector.connect(host="127.0.0.1", # your host
user="root", # username
password="**********" ) # password
- Let's check existing databases in the server and list them. To do this, we use the cursor method:
mycursor = connection.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)

- We can access one of the existing databases. Let's list the tables in one of the databases:
connection = mysql.connector.connect(host="127.0.0.1", # your host
user="root", # username
password="**********" , #replace with your password
database = 'mysql')
mycursor = connection.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
推薦閱讀
- Word 2003、Excel 2003、PowerPoint 2003上機指導與練習
- 機器學習實戰:基于Sophon平臺的機器學習理論與實踐
- 玩轉智能機器人程小奔
- 輕松學C語言
- 人工智能超越人類
- Mastering Spark for Data Science
- Getting Started with MariaDB
- Zabbix Network Monitoring(Second Edition)
- PostgreSQL Administration Essentials
- 單片機C語言應用100例
- 電腦日常使用與維護322問
- 工業自動化技術實訓指導
- Mastering GitLab 12
- 深度學習原理與 TensorFlow實踐
- 傳感器原理及實用技術