Article Directory
MySQL database
MySQL and Python interaction
Python operation MySQL steps
Import module
- Introduce the pymysql module in the py file
from pymysql import *
Connection object
- Used to establish a connection with the database
- Create an object: call the connect() method
conn=connect(参数列表)
- Parameter host: the connected mysql host, if the local machine is'localhost'
- Parameter port: the port of the connected mysql host, the default is 3306
- Parameter database: the name of the database
- Parameter user: the user name of the connection
- Parameter password: the password of the connection
- Parameter charset: the encoding method used for communication, utf8 is recommended
Object method
- close()Close the connection
- commit() submit
- cursor() returns a Cursor object, which is used to execute SQL statements and obtain results
Cursor object
- Used to execute SQL statements, the most frequently used statements are select, insert, update, delete
- Obtain the Cursor object: call the cursor() method of the Connection object
cs1=conn.cursor()
Object method
- close()close
- execute(operation [, parameters]) executes the statement, returns the number of rows affected, mainly used to execute insert, update, delete statements, and can also execute create, alter, drop and other statements
- When fetchone() executes the query statement, it obtains the first row data of the query result set and returns a tuple
- When fetchall() executes the query, get all the rows of the result set, one row forms a tuple, and then assemble these elements into a tuple and return
Object properties
- rowcount is a read-only attribute, indicating the number of rows affected after the most recent execute() execution
- connection gets the current connection object
Python operates MySQL to perform'addition, deletion, modification, and check'
Additions and deletions
from pymysql import *
def main():
# 创建Connection连接
conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='此处填写密码',charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()
# 执行insert语句,并返回受影响的行数:添加一条数据
# 增加
count = cs1.execute('insert into goods_cates(name) values("硬盘")')
#打印受影响的行数
print(count)
count = cs1.execute('insert into goods_cates(name) values("光盘")')
print(count)
# # 更新
# count = cs1.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
# # 删除
# count = cs1.execute('delete from goods_cates where id=6')
# 提交之前的操作,如果之前已经之执行过多次的execute,那么就都进行提交
conn.commit()
# 关闭Cursor对象
cs1.close()
# 关闭Connection对象
conn.close()
if __name__ == '__main__':
main()
Query a row of data
from pymysql import *
def main():
# 创建Connection连接
conn = connect(host='localhost',port=3306,user='root',password='此处填写码',database='jing_dong',charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()
# 执行select语句,并返回受影响的行数:查询一条数据
count = cs1.execute('select id,name from goods where id>=4')
# 打印受影响的行数
print("查询到%d条数据:" % count)
for i in range(count):
# 获取查询的结果
result = cs1.fetchone()
# 打印查询的结果
print(result)
# 获取查询的结果
# 关闭Cursor对象
cs1.close()
conn.close()
if __name__ == '__main__':
main()
Query multiple rows of data
from pymysql import *
def main():
# 创建Connection连接
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()
# 执行select语句,并返回受影响的行数:查询一条数据
count = cs1.execute('select id,name from goods where id>=4')
# 打印受影响的行数
print("查询到%d条数据:" % count)
# for i in range(count):
# # 获取查询的结果
# result = cs1.fetchone()
# # 打印查询的结果
# print(result)
# # 获取查询的结果
result = cs1.fetchall()
print(result)
# 关闭Cursor对象
cs1.close()
conn.close()
if __name__ == '__main__':
main()