Python:数据库 *** 作

Python:数据库 *** 作,第1张

概述1、redis#encoding=utf-8importredisdefredis_op():host='127.148.167.201'port=9152password='WCS_REDIS_!(0)'r=redis.Redis(host=host,port=port,password=password)redis_key="cache:task:expirefi

1、redis

#enCoding=utf-8import redisdef 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',redisQueueif __name__ == '__main__':    # redis_op()
VIEw Code

 

2、postsql

#enCoding=utf-8import psycopg2def 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 MongoClIEntimport Jsonfrom BaseConfig import mongo_infoclass  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:[email protected] :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:[email protected] :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:[email protected] :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:[email protected] :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:[email protected] :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

 

总结

以上是内存溢出为你收集整理的Python:数据库 *** 作全部内容,希望文章能够帮你解决Python:数据库 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1186156.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存