


Introduction to the basic knowledge of Mysql database programming in python learning
In Python web crawlers, it is usually stored in TXT plain text, but it can also be stored in a database; at the same time, in WAMP (Windows, Apache, MySQL, PHP or Python) development websites, it can also be built through Python Web page, so this article mainly talks about the programming knowledge related to Python calling MySQL database
In Python web crawler, it is usually stored in TXT plain text, but it can also be stored in the database; at the same time, in WAMP ( Windows, Apache, MySQL, PHP or Python) can also build web pages through Python, so this article mainly talks about programming knowledge related to Python calling MySQL database. Explain from the following aspects:
1. Configuring MySLQ
2. Basic knowledge of SQL statements
3. Basic knowledge of Python operating MySQL
4. Python calling MySQL example
1. Configure MySQL
First download mysql-5.0.96-winx64, the installation process is as shown in the figure below.
1. Install MySQL 5.0
2. Select Manual Configuration, Service Type, Universal Multi-Function and Installation Path
3. Set the number of database access connections to 15 and the port to 3306 (used to set the URL in the code) , the encoding method is utf-8
4. Set the user name and password of the default super root user, and finally the installation is successful
2. Basic knowledge of SQL statements
After successfully installing MySQL 5.0, perform simple operations on the database.
1. Run MySQL and enter the default user password 123456
## 2. Create database test01 and use the database (use database directly for the second call)
create database test01;
Display the databases contained in the database: show databases;
Create table student(username varchar(20),password varchar(20),stuid int primary key);
## 5. Report to Insert data into the student table and display the queried data
6. Delete the table: drop table student;
7. Update data
8. Delete data Delete from student where username='eastmount;
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
The usual installation method is to use: pip install mysql installs Python's MySQL library, but an error always occurs. Common errors such as: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)mysql.c(42): fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
These may be driver issues.It is recommended to download a MySQL-python-1.2.3.win-amd64-py2.7.exe file for installation. Official website address: https://pypi.python.org/pypi/MySQL-python/
下面我们要详细了解Python数据库API。从Python中访问数据库需要接口程序,接口程序是一个Python模块,它提供数据库客户端库(通常是C语言写成的)的接口供你访问。注意:Python接口程序都一定要遵守Python DB-API规范。
DB-API是一个规范。它定义了一系列必须的对象和数据库存取方式,以便为各种各样的底层数据库系统和多种多样的数据库接口程序提供一致的访问接口。DB-API为不同的数据库提供了一致的访问接口,在不同的数据库之间移植代码成为一件轻松的事情。
下面简单介绍DB-API的使用方法。
1.模块属性
DB-API规范里的以下特性和属性必须提供。一个DB-API兼容模块定义如下所示:
1 2 3 4 |
|
Python调用MsSQL需要导入MySQLdb库,如下:
import MySQLdb
2.connect()函数
其中主要使用的方法是connect对象。connect()方法生成一个connect对象,用于访问数据库,其参数如下:
1 2 3 4 5 |
|
注意并非所有的接口程序都严格按照这种格式,如MySQLdb。
1 2 |
|
connect()对象方法如下:
1 2 3 4 5 |
|
注意,执行close()方法则上述的连接对象方法不能再使用,否则发生异常。commit()、rollback()、cursor()或许更对于支持事务的数据库更有意义。
数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完整地执行,要么完全地不执行。 一旦你完成了数据库连接,关闭了游标对象,然后在执行commit()提交你的操作,然后关闭连接。
3.游标对象
上面说了connect()方法用于提供连接数据库的接口,如果要对数据库操作那么还需要使用游标对象。游标对象的属性和方法:
1 2 3 4 5 |
|
下面通过简单的示例进行讲解。
四. Python调用MySQL示例
在前面数据库中我们创建了数据库“test01”和表“student”,同时插入了数据。那么,怎样通过Python来显示呢?
1.查询所有数据库
首先,我们查看本地数据库中所包含的数据库名称,通过“show databases”语句。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
其中通过链接数据库代码为:
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306)
访问root超级用户,其密码为“123456”,端口为“3306”,其结果如下:
如果不知道本地数据库的名称,可以通过该方法,先查询数据库中包含哪些数据库,然后再连接该数据库进行相关的操作。
2.查询表
下面介绍查询表student中数据,代码如下,代码的具体含义是通过connect()连接数据库,通过conn.cursor()定义游标,然后调用游标的excute(sql)执行数据库操作,此处为查询操作,再通过fetchall()函数获取所有数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
输出结果如图所示:
对应的MySQL中的结果是一致的,下图是对应的结果。
3.创建表
下面这段代码是创建一张教师表,主要是通过commit()提交数据。
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 |
|
输出结果如下所示,插入教师表,包含字段:教师序号(id)、教师名称(name)、教师性别(sex)。
插入数据也可以通过execute(sql)方法实现,如:
cur.execute("insert into student values( 'yxz', '111111', '10')")
但插入的新数据通常是通过变量进行赋值,而不是固定的,所以要对这条语句中的值做修改。我们可以做如下修改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
输出结果如下所示:
1 2 3 4 5 6 7 |
|
同样,对数据库的增删改插都可以进行,请读者自行阅读。
推荐资料:python使用mysql数据库 - 虫师
后面我会结合Python爬虫讲述,如何将爬取的内容存储在数据库中,如我CSDN的博客,爬取博客标题、发布时间、阅读量和评论数。
MySQL数据库中结果如下图所示:
最后希望文章对你有所帮助,如果文章中存在不足或错误的地方,还请海涵~还是那句话,挺享受现在的老师生活,不论科研、项目,还是教学,很充实,加油!
The above is the detailed content of Introduction to the basic knowledge of Mysql database programming in python learning. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
