Python:数据库操作

1、redis

Python:数据库操作
#encoding=utf-8
import redis

def redis_op():
    host = '127.148.167.201'
    port = 9152
    password = 'WCS_REDIS_!(0)'
    r = redis.Redis(host=host, port=port, password=password)
    redis_key = "cache:task:expirefileindeletingnum:cross-target-redis-0*"
    print u'redis_key:', redis_key
    redisQueue = r.keys(redis_key)
    print u'redisQueue',redisQueue
if __name__ == '__main__':
    # redis_op()
View Code

 

2、postsql

Python:数据库操作
#encoding=utf-8
import psycopg2


def postgredb_op():
    database="cloud"
    user="wcs_cloud"
    password="wcs_cloud"
    host="127.148.167.201"
    port="7893"

    bucketname='transcode'
    try:
        try:
            conn = psycopg2.connect(database=database, user=user,password=password, host=host,port=port)
            cur = conn.cursor()
            print(u'connect successful')
            sql = 'select status from fmgr_data_operation where bucket_name = \'{0}\' order by create_time desc limit 1;'.format(
                    bucketname)
            print u'查询的sql语句:{0}'.format(sql)
            cur.execute(sql)
            rows = cur.fetchall()
            print rows[0]


        except psycopg2.OperationalError, E:
            print u'error connect'
            print str(E)
    except psycopg2.ProgrammingError, e:
        print u'error start location:'
        print str(e)

    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    postgredb_op()
View Code

 

3、mongo

Python:数据库操作
from pymongo import MongoClient
import json
from BaseConfig import mongo_info

class  connect_mongo():
    def __init__(self):
        self.host = mongo_info.host
        self.port = mongo_info.port
        self.username = mongo_info.username
        self.password = mongo_info.password
        self.db_name = mongo_info.db_name
        uri = 'mongodb://'+self.username+':'+self.password+'@'+self.host+':'+self.port+'/'
        #创建一个数据库链接诶
        self.client = MongoClient(self.host,int(self.port))


    def search_file_hash(self,bucketname,parentFolder,filename):
        ###################################
        #通过uri的方式链接mongo数据库
        #uri = 'mongodb://WCSMongo:WCS_Admin@10.8.198.23 :40000/'
        try:
            #访问一个数据库对象
            db = self.client[self.db_name]
            db.authenticate(self.username, self.password)
            #访问集合对象,并进行数据查询
            cursor = db[bucketname].find({"showName":filename,'parentFolder':parentFolder})
            for i in cursor:
                return i.get('hash')
        except Exception as e:
            print e
            print '连接mongo异常'
            return -1
        finally:
            self.client.close()

    def search_file_all(self,bucketname,filename,parentFolder=''):
        ###################################
        #通过uri的方式链接mongo数据库
        #uri = 'mongodb://WCSMongo:WCS_Admin@10.8.198.23 :40000/'
        try:
            #访问一个数据库对象
            db = self.client[self.db_name]
            db.authenticate(self.username, self.password)
            #访问集合对象,并进行数据查询
            print '#########',bucketname,filename
            cursor = db[bucketname].find({"showName":filename})
            # cursor = db[bucketname].find({"showName":filename,'parentFolder':parentFolder})
            for i in cursor:
                return format(i)
        except :
            print '连接mongo异常'
            return -1
        finally:
            self.client.close()

    def update_file(self,bucketname,filename,file_key,value,operation="$set"):
        ###################################
        #通过uri的方式链接mongo数据库
        #uri = 'mongodb://WCSMongo:WCS_Admin@10.8.198.23 :40000/'
        try:
            #访问一个数据库对象
            db = self.client[self.db_name]
            db.authenticate(self.username, self.password)
            #访问集合对象,并进行数据查询
            print '#########',bucketname,filename,file_key,value
            cursor = db[bucketname].update({"showName":filename},{operation:{file_key:value}})
            print "update success"
            return 0
        except :
            print '连接mongo异常'
            return -1
        finally:
            self.client.close()

    #删除某字段
    def delete_file_key(self,bucketname,filename,file_key):
        ###################################
        #通过uri的方式链接mongo数据库
        #uri = 'mongodb://WCSMongo:WCS_Admin@10.8.198.23 :40000/'
        try:
            #访问一个数据库对象
            db = self.client[self.db_name]
            db.authenticate(self.username, self.password)
            #访问集合对象,并进行数据查询
            print '#########',bucketname,filename,file_key
            cursor = db[bucketname].update({"showName":filename},{"$unset":{file_key:""}})
            print "update success"
            return 0
        except :
            print '连接mongo异常'
            return -1
        finally:
            self.client.close()


    def delete_file_minetype(self,bucketname,filename):
        ###################################
        #通过uri的方式链接mongo数据库
        #uri = 'mongodb://WCSMongo:WCS_Admin@10.8.198.23 :40000/'
        try:
            #访问一个数据库对象
            db = self.client[self.db_name]
            db.authenticate(self.username, self.password)
            #访问集合对象,并进行数据查询
            cursor = db[bucketname].update({"showName":filename},{"$unset":{"mimeType":""}})
            return 0
        except :
            print '连接mongo异常'
            return -1
        finally:
            self.client.close()
View Code
mongo_fileinfo1 = eval(connect_mongo().search_file_all(bucket_name, fileName))
print u'迁移前mongo所有字段信息:', mongo_fileinfo1

 

上一篇:Java(spring boot)实现操作minio(最新版本minio)


下一篇:Spring Boot 2.x 整合 MinIO 8.x