install
Oracle installation command
pip install orator
##Easy to use
Connection operation of database
config = { 'mysql': { 'driver': 'mysql', 'host': 'localhost', 'database': 'test_one', 'user': 'root', 'password': '123456', 'prefix': '', 'port': 3306 } } db = DatabaseManager(config=config).connection()
increase
Add an information record
db.table('test').insert_get_id({ "published": "2020-11-05", "source": "custom", "title": "Test title", "url": "https://www.baidu.com", "sort": 56 })
Insert multiple information records
db.table('test').insert([ {"published": "2020-03-11", "source": "Source 1", "title": "Line test", "url": "https://sohu.com", "sort": 1}, {"published": "2020-03-12", "source": "Source 2", "title": "Comments", "url": "https://infoq.com", "sort": 2} ])
delete
Delete records that meet the conditions
# Delete the record with the title "sort field as test 1 self.db.table("test").where("title_tort", "=", "Test 1").delete()
Delete all record information
# Delete all records self.db.table("test").delete()
modify
Modify an information record
# Update the record with id 13 to change the sort value to 16 self.db.table("test").where('id', 13).update({"sort": 16})
query
Query information with id 1
# corpus is the table name result = db.table('corpus').where('id', '1').get() print(result)
Multi criteria query (starting from the fifth row of data, query five pieces of data)
result_one = db.table('corpus').offset(3).limit(5).get() for num, data in enumerate(result_one): print(num, data)
Multi conditional query
result_two = db.table('corpus').where('type', 'man').where_between('age', [15, 25]).get() for every_data in result_two: print(every_data)
Other complex query operations can refer to documents
Full code
Restriction: the test table already exists before the corresponding operation can be performed
from loguru import logger from orator import DatabaseManager,Model config = { 'mysql': { 'driver': 'mysql', 'host': 'localhost', 'database': 'test_one', 'user': 'root', 'password': '123456', 'prefix': '', 'port': 3306 } } class OperateMysql(object): def __init__(self): self.config = config self.db = DatabaseManager(config=self.config).connection() def insert_data(self): # If the table has an auto increment id, insert a record with insert get id self.db.table('test').insert_get_id({ "published": "2020-11-05", "source": "custom", "title": "Test title", "url": "https://www.baidu.com", "sort": 56 }) # Insert multiple records self.db.table('test').insert([ {"published": "2020-03-11", "source": "Source 1", "title": "Line test", "url": "https://sohu.com", "sort": 1}, {"published": "2020-03-12", "source": "Source 2", "title": "Comments", "url": "https://infoq.com", "sort": 2} ]) def delete_data(self): # Delete the record with the title "sort field as test 1 self.db.table("test").where("title_tort", "=", "Test 1").delete() # Delete a record with a value of 1 for sort self.db.table("test").where("sort", 1).delete() # Delete all records self.db.table("test").delete() def update_data(self): # Update the record with id 13 to change the sort value to 16 self.db.table("test").where('id', 13).update({"sort": 16}) # Increase all information in the sort column by 3 self.db.table("test").increment('sort', 3) def search_data(self): # Query data with id 1 result = self.db.table('corpus').where('id', '1').get() print(result) # Multi criteria query (starting from the fifth row of data, query five pieces of data) result_one = self.db.table('corpus').offset(3).limit(5).get() for num, data in enumerate(result_one): print(num, data) # Multi conditional query result_two = self.db.table('corpus').where('type', 'man').where_between('age', [15, 25]).get() for every_data in result_two: print(every_data) if __name__ == '__main__': om = OperateMysql() om.update_data()