Master python connection SQL Server, MySQL, MongoDB, Redis database

The bloggers are playing various environments for future learning, and this time it is the turn of various databases. This blog mainly introduces these four databases and their respective visualization tools, and details how to connect to the database with python. To explain, what I am connecting here is a database installed on another computer in the local area network. Of course, you can also directly access your local database!

For those who need remote access to the database, please refer to: Installation of MySQL8.0.18 under Windows and database access in the local area network

One, a brief description of the database

1.1 Introduction: main purpose
  • SQL Server

    SQL Server is a relational database management system launched by Microsoft . It provides more secure and reliable storage functions for relational data and structured data, and can build and manage highly available and high-performance data applications for business.

  • MySQL

    MySQL is a relational database management system developed by the Swedish company MySQL AB. It is a product of Oracle. It is small in size, fast in speed, low in total cost of ownership, and open source. Generally, the development of small and medium-sized websites chooses MySQL as the website database.

  • MongoDB

    MongoDB is a database based on distributed file storage . Written by C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.

  • Redis

    Redis (Remote Dictionary Server), the remote dictionary service, is an open source log-based, Key-Value database written in ANSI C language, supporting the network, memory-based or persistent , and providing APIs in multiple languages.

1.2 Dry goods: default port number visualization tool
database Default port number Visualization tool
SQL Server 1433 Navicat
MySQL 3306 Navicat
MongoDB 27017 Robo 3T
Redis 6379 Redis Manager

Two, python connect to the database

(1) SQL Server database: pymssql
import pymssql
import pandas as pd
conn = pymssql.connect('127.0.0.1', 'sa', '你的sa密码', '你的数据库名')
# 这里(127.0.0.1)是本地IP地址
cursor = conn.cursor()
sql = "select * from " + '你的表名'
# 第1种方法
data1 = pd.read_sql(sql,con=conn)
# 第2种方法
cursor.execute(sql)  # 执行sql语句
rows = cursor.fetchall()  # 读取查询结果
data2 = pd.DataFrame(rows, columns=[x[0] for x in cursor.description])
cursor.close()
conn.close()
data1.head(1)
data2.head(1)

(2) MySQL database: pymysql
import pymysql
import pandas as pd
conn = pymysql.connect('192.168.3.6', 'root', '你的root密码', '你的数据库名')
# 这里(192.168.3.6)是访问数据库所在电脑的IP地址
cursor = conn.cursor()
sql = "select * from " + '你的表名'
# 第1种方法
data1 = pd.read_sql(sql,con=conn)
# 第2种方法
cursor.execute(sql)  # 执行sql语句
rows = cursor.fetchall()  # 读取查询结果
data2 = pd.DataFrame(rows, columns=[x[0] for x in cursor.description])
cursor.close()
conn.close()
data1.head(1)
data2.head(1)

(3) Redis database: redis
import redis
pool = redis.ConnectionPool(host='192.168.3.6', port=6379, db=0) 
# 创建连接池,host为连接电脑IP地址,port为默认端口号3306,db选择连接的数据库
r = redis.Redis(connection_pool=pool) # 获取连接对象
r.set('name','value') # 插入数据
r.get('name') # 获得对应键的value值

(4) MongoDB database: pymongo
import pymongo
client = pymongo.MongoClient(host='192.168.3.6', port=27017) 
# host为连接电脑IP地址,port为默认端口号27017
db = client.test # test为连接的数据库名
collection = db.students # students为选择的集合
student = {
    
      
    'id': '20170101',  
    'name': 'Jordan',  
    'age': 20,  
    'gender': 'male'  
} 
result = collection.insert(student)  # 向集合中插入值
print(result)

Guess you like

Origin blog.csdn.net/xylbill97/article/details/107371624