In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to realize the interaction between Python and database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
MongoDB
Install the module pip install pymongo
Connect to the database import pymongo client = pymongo.MongoClient () db = client ["database_name"] # follow the database name collection = db ["set_name"] # specify the addition, deletion, modification and query of the collection name
Add-- > insert_one | insert_many
Collection.insert_one ({"name": "kongshang", "age": 12})
Find-- > find | find_one
Collection.find ()
Pay attention to the data to be converted with list
Modify-> update_one | update_many
Collection.update_one ({"name": "kongshang"}, {'$set': {"age": 13}})
Delete-> delete_one | delete_many
Collection.delete_one ({"name": "kongshang"}) encapsulates import pymongo class MyMonDB: def _ _ init__ (self, database, collection): # Database and Collection self.client = pymongo.MongoClient () # Connect to the database using self.db = self.client [database] # specified database self.col = self.db [collection] # specified collection def insert (self, data) OnlyOne=True): # onlyOne is used to control the insertion of single or multiple data if onlyOne: self.col.insert_one (data) else: self.col.insert_many (data) def find (self, query=None) OnlyOne=True): # query is the query condition if onlyOne: ret = self.col.find_one (query) return ret else: ret = self.col.find (query) return list (ret) def update (self, data_old, data_new, onlyOne=True): if onlyOne: self.col.update_one (data_old {"$set": data_new}) else: self.col.update_many (data_old, {"$set": data_new}) def delete (self, data, onlyOne=True): if onlyOne: self.col.delete_one (data) else: self.col.delete_many (data)
Note that the database is case sensitive
MySQL
Install the module pip install pymysql
Connect to the database import pymysql# connection mysqldb_config = {"host": "127.0.0.1", "port": 3306, "user": "admin", "password": "qwe123", "db": "stu" # Database "charset" for the specified operation: "utf8"} conn = pymysql.connect (* * db_config) # mysql login * * is a dictionary unpacking print (conn) execution operation cur = conn.cursor () # returns a cursor object for executing database commands Execute the SQL command cur.execute ("INSERT INTO stu (id, name) VALUES (1, 'nihao'), (2,' ci')") # execute the SQL command execute the insert command conn.commit () # transaction through the cursor object Submit save cur.close () # close cursor object conn.close () # close database query data cur.execute ("SELECT * FROM stu") # execute SQL command # print (list (cur)) # print (cur.fetchone ()) # query single # print (cur.fetchmany (3)) # query multiple print (cur.fetchall ()) # query all exception handling try: cur.execute ("INSERT INTO stu (id)" Name) VALUES (1, 'nihao'), (2,' ci') ") except Exception as e: print (e) conn.rollback () # transaction rollback else: conn.commit () # transaction commit finally: cur.close () # close cursor object conn.close () # close database Redis
Install the module pip install redis
Connect to database import redis # login database # host ip address # decode_responses get True returns string data when obtaining key values. Default is False binary data # db specified database The default is 1red = redis.StrictRedis (host= "127.0.0.1", decode_responses=True, db=2) to perform the operation # string red.set ("num", 1) print (red.get ("num")) print (red.type ("num")) red.delete ('num') # to sum up, the method to call the Redis database is red. [enter redis database operation command] () "how to realize the interaction between Python and database" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.