How to use Python to play with MySQL database
May 26, 2023 pm 02:46 PM1. Background
I conducted the connection experiment in Anaconda notebook, using the environment Python3.6. Of course, the operation can also be performed in the Python Shell.
The most commonly used and stable python library for connecting to MySQL database is PyMySQL.
2. Basic operations
1. Install the PyMySQL library
The simplest way:
Enter on the command linepip install pymysql
Or:
Download the whl file [1] for installation. The installation process is done by yourself.
2. Install MySQL database
There are two MySQL databases:
MySQL and MariaDB
I'm using MariaDB, which is a fork of MySQL.
The two are compatible in most aspects of performance, and you can’t feel any difference when using them.
gives the download address: MySQL[2], MariaDB[3], the installation process is very simple, just follow Next Step, but remember the password.
There is a small episode. MySQL and MariaDB are equivalent to the relationship between sisters and sisters. They were created by the same person (Widenius).
After MySQL was acquired by Oracle, Mr. Widenius felt unhappy, so he built MariaDB, which can completely replace MySQL.
Daniel is willful.
3. SQL basic syntax
Next, we will use SQL table creation, query, data insertion and other functions. Here is a brief introduction to the basic statements of SQL language.
View database:
SHOW DATABASES;
Create database:
CREATE DATEBASE database name;
Use database:
USE database name;
View data table:
SHOW TABLES;
Create data table:
CREATE TABLE table name (column name 1 (data type 1), column name 2 (data type 2));
Insert data:
INSERT INTO table name (column name 1, column name 2) VALUES (data 1, data 2);
View data:
SELECT * FROM table name;
Update data:
UPDATE table name SET column name 1 = new data 1, column name 2 = New data 2 WHERE A certain column = a certain data;
4. Connect to the database
After installing the necessary files and libraries, officially start connecting to the database Well, it’s mysterious but not difficult!
1 2 3 4 5 6 7 8 9 |
|
Execute this code and the connection will be completed!
5. Add, delete, modify and query operations
First check which databases there are:
1 2 3 4 5 |
|
Print out all databases:
1 2 3 4 5 |
|
Create in the test database Table:
1 2 3 4 |
|
Insert a piece of data into the data table student:
1 2 |
|
View the content of the data table student:
1 2 |
|
The printout is: ('XiaoMing', 23)
Bingo! It’s a piece of data we just inserted
Finally, remember to close the cursor and connection:
1 2 3 4 |
|
OK, the whole process is roughly like this.
Of course, these are very basic operations. More usage methods need to be found in the PyMySQL official documentation [4].
3. Import big data files
Take csv files as an example. There are generally two methods for importing csv files into the database:
1. Import one by one through the insert method of SQL , suitable for CSV files with small data volume, and will not be described in detail here.
2. Importing through the load data method is fast and suitable for big data files, which is also the focus of this article.
The sample CSV file is as follows:
The overall work is divided into 3 steps:
1. Use python to connect to the mysql database;
2、基于CSV文件表格字段创建表;
3、使用load data方法导入CSV文件内容。
sql的load data语法简介:
1 |
|
csv_file_path
指文件绝对路径table_name
指表名称FIELDS TERMINATED BY ','
指以逗号分隔LINES TERMINATED BY '\\r\\n'
指换行IGNORE 1 LINES
指跳过第一行,因为第一行是表的字段名
下面给出全部代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
The above is the detailed content of How to use Python to play with MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to optimize MySQL query performance in PHP?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP?
