mysql MongoDB redis 的链接

1.python和mysql关联

如果没有pip

1.安装sudo apt-get install python3-pip
2.安装pymysql第三方模块,通过该模块来关联mysql
pip3 install pymysql

3.在pycharm中导入pymysql模块
import pymysql
如果可以导入---》成功

步骤:
1.进行连接---连接数据库
con = pymysql.Connect(.......地址)
2.获取操作的游标
cursor = con.cursor()
3.执行sql语句
cursor.execute(sql语句)
4.查询结果
cuesor.fetchone()---->获取光标的下一个结果集
cuesor.fetchall()---->获取所有结果集
5.关闭连接
close()

详细说明:
   Connection对象
   作用:用于建立与数据库的连接
   创建connection对象直接调用connect(参数列表)

host:连接mysql主机,本机localhost
port:连接mysql主机的端口;3306
db:数据库的名字
user:连接的用户名
password:连接密码
charset:通信采用的编码格式,默认gb2312


对象的方法:
close():关闭连接
commit():事务,需要提交才会生效
rollback():事务,放弃之前的操作
cursor():返回cursor对象,用于执行失去了语句并获取结果

cursor对象:
    作用:执行sql语句    

execute():执行语句,返回受影响的行数

fetchone():获取第一行数据
fetchall():获取结果集的所有行数据
next():执行查询语句时,获取当前行的下一行

 

2.python和MongoDB链接

1.安装第三方模块:
pymongo
pip3 install pymongo

2.使用MongoClient建立连接
from pymongo import MongoClient
client = MongoClient(host,port)
  host:主机-----》localhost--->mongodb://localhost:27017
  port:端口---->27017
client = MongoClient('localhost',27017)
client = MongoClient('mongodb://localhost:27017/')

3.获取数据库
db = client.pythondb
或者:
db = client['python-db']

4.获取集合
collection = client.python_collection
或者:
collection = db['pyhton-collection'] 

 

3.python和redis链接

1.安装redis
pip3 install redis

引入模块:
from redis import strictredis

2.创建对象:
init对象:
参数:
  host:主机  localhost
  port:端口   6397
  db:0
  sr = strictredis(host='localhost',port=6379,db=0)

4.通过python对redis数据库进行增删查改的操作,获取键
 
 4.1 增:
 
 步骤:
 
    1.导入strictredis模块
    2.创建strictredis对象,此对象用于和redis数据库之间产生交互
     sr = strictredis(host='localhost',port=6379,db=0)
     3.添加键,和值
     reslut = sr.set('key',"value")
     print(reslut)
     
 
 from redis import *

if __name__ == "__main__":
    try:
        sr = StrictRedis(host='localhost',port=6379,db=0)

        result = sr.set('py10','hahah')
        print(result)
    except Exception as e:
        print(e)

2.删除:
 from redis import *

if __name__ == "__main__":
    try:
        sr = StrictRedis(host='localhost',port=6379,db=0)

        # result = sr.set('py11','hahah')
        result = sr.delete('py10')
        print(result)
    except Exception as e:
        print(e)


3.修改

from redis import *

if __name__ == "__main__":
    try:
        sr = StrictRedis(host='localhost',port=6379,db=0)

        # result = sr.set('py11','hahah')
        # result = sr.delete('py10')
        result = sr.set('py11','heihei')
        print(result)
    except Exception as e:
        print(e)


4.查找:
   
from redis import *

if __name__ == "__main__":
    try:
        sr = StrictRedis(host='localhost',port=6379,db=0)

        # result = sr.set('py11','hahah')
        # result = sr.delete('py10')
        # result = sr.set('py11','heihei')
        result = sr.get('py11')
        print(result)
    except Exception as e:
        print(e)


5.获取键
keys()
 from redis import *

if __name__ == "__main__":
    try:
        sr = StrictRedis(host='localhost',port=6379,db=0)

        # result = sr.set('py11','hahah')
        # result = sr.delete('py10')
        # result = sr.set('py11','heihei')
        # result = sr.get('py11')
        result = sr.keys()
        print(result)
    except Exception as e:
        print(e)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据库,数据库)