ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Python:数据库操作

2021-05-19 15:33:54  阅读:173  来源: 互联网

标签:mongo Python 数据库 db bucketname print 操作 password self


1、redis

#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

#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

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

 

标签:mongo,Python,数据库,db,bucketname,print,操作,password,self
来源: https://www.cnblogs.com/channy14/p/14785190.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有