程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses pymysql to connect to MySQL database

編輯:Python

pymysql It's a Python Third party package for , Manual installation is required before use

1. Use pymysql Query data

'''
pymysql Basic use steps
Use pymysql Query data
'''
import pymysql
# Create database connection object , Except for the port , All other types should use string type
connect_db=pymysql.connect(host='localhost',port=3306,user='root',password='root',charset='utf8',database='jing_dong')
# Get cursor object , Cursor objects are used to operate the database , Similar to the cursor when a computer writes a document , It can indicate the starting position of the operation
# A database connection object can create multiple ( many times ) Cursor object , Generally, only one will be created for operation at the same time
# Cursor objects are created , An implicit transaction environment is enabled by default
cur=connect_db.cursor()
# Operating the database
# Avoid string quotes and sql Quotation mark conflict in statement , Use three quotes
sql_str='''select * from goods'''
# perform sql sentence , The number of data rows of the operation will be returned
row_count=cur.execute(sql_str)
print(f' Query to {row_count} Bar record ')
# The return value of the query data statement is an ancestor , Or Yuanzu taoyuanzu
# Start at the position indicated by the cursor object , Get a query result
result_one=cur.fetchone() # Return to a ancestor
print(result_one)
print('*'*30)
# Return the cursor to the beginning
cur.rownumber=0
# Start at the position indicated by the cursor object , Get the specified number of records
result_many=cur.fetchmany(7) # Returns an iteratable object of primitive ancestor type , A piece of data is like this ((),)
for result in result_many:
print(result)
print('*'*30)
# Return the cursor to the beginning
cur.rownumber=0
# Get all the data
result_all=cur.fetchall() # Returns an iteratable object of primitive ancestor type , A piece of data is like this ((),)
for result in result_all:
print(result)
# Close cursor object
cur.close()
# Close database objects
connect_db.close()

2. Use pymysql insert data

'''
insert data
'''
import pymysql
# Connect = connect = Connection = connections.Connection
connect_db=pymysql.Connect(host='localhost',port=3306,database='jing_dong',user='root',password='root',charset='utf8')
cur=connect_db.cursor()
sql_str='''insert into goods(name,cate_id,brand_id) values('MacBookPro 15',1,5)'''
t=cur.execute(sql_str)
print(t)
'''
When adding, deleting, or modifying a database , By default, it will operate in the transaction environment , After the operation is completed, submit the operation manually , Otherwise, the default operation of the program is rollback , Then the operation just now will not take effect
But when adding data , Even if the rollback , For the field of the primary key auto_increment Value will also be added 1
'''
# The commit operation of the transaction is completed by the database connection object
# The commit operation of the transaction must be performed before sql After the statement
connect_db.commit()
cur.close()
connect_db.close()

3. Use pymysql Modifying data

'''
Use pymysql Update data
'''
import pymysql
connect_db=pymysql.Connect(host='localhost',port=3306,user='root',password='root',charset='utf8',database='jing_dong')
cur=connect_db.cursor()
sql_str='''update goods set price=2099 where id=10'''
cur.execute(sql_str)
connect_db.commit()
cur.close()
connect_db.close()

4. Use pymysql Delete data

'''
Use pymysql Delete data
'''
import pymysql
connect_db=pymysql.Connection(host='localhost',port=3306,user='root',password='root',charset='utf8',database='jing_dong')
cur=connect_db.cursor()
sql_str='''delete from goods where name=' laptop 17' '''
cur.execute(sql_str)
connect_db.commit()
cur.close()
connect_db.close()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved