py 数据库

简介:

# !/usr/bin/python
#
coding=UTF-8
#
sql 参考
#
http://www.kitebird.com/articles/pydbapi.html
import  cx_Oracle
import  re,os,sys

logTxt
= " ./log "

dataFile 
=   " ./data.txt "
fileCharset 
=   " GBK "
dbCharset 
=   " GBK "

username 
=   " .... "
userpwd 
=   " .... "
host 
= " .... "
dbname
= " .... "
# dbname="..."

MuById_SQL
= " select count(*) from mn00_tbl_music tm where tm.id=:mid "

CRById_SQL
= " select tc.music_id from mn05_tbl_copyright tc where tc.copyright_id=:crid "
CRById_In_Music_SQL
= " update  mn05_tbl_copyright  set music_id=:mid where copyright_id=:crid "

CRByMNameAndAName_SQL
= "  select tc.copyright_id from mn05_tbl_copyright tc where  (tc.actor_src_name=':acName' and tc.music_src_name=':muName' ) or (tc.actor_name=':acName' and tc.music_name=':muName')  " ;


#  60056601146|失意酒(童安格)|失意酒(童安格)|10030149
#
 CRID | 编辑后歌曲(歌手)|歌曲(歌手) |MID
def  action(strs):
    result 
=  selectSql( subSql(MuById_SQL,  " mid " , strs[ 3 ]) )
    
if ( int(result[0][0]) > 0):
        actionCR(strs[0],strs[
3 ])
    
if ( strs[ 1 and  strs[ 1 ] != ""  ):
        actionName(strs[
1 ],strs[ 3 ])
    
if ( strs[ 2 and  strs[ 2 ] != ""  ):
        actionName(strs[
2 ],strs[ 3 ])


def  actionName(mu_ac,mid):
    p 
=  re.compile( " (.*)\((.*)\) " )
    m 
=  p.match( mu_ac )
    
if (m):
        crmaSql
= subSql(CRByMNameAndAName_SQL, " muName " , m.group( 1 ))
        crmaSql
= subSql(crmaSql, " acName " ,m.group( 2 ))
        
for  row  in  selectSql( crmaSql ):
            actionCR(row[0].
__str__ () ,mid)
        
# count = cursor.rowcount
    
def  actionCR(crid,mid):
    result 
=  selectSql( subSql(CRById_SQL,  " crid " , crid )  ) 
    
if ( result[0][0] == None  or  int(result[0][0]) == 0):
        upCr
= subSql(CRById_In_Music_SQL, " crid " ,crid)
        upCr
= subSql(upCr, " mid " ,mid)
        
if (execSql(upCr) > 0):
            _log(
' match crid= ' + crid + '  muid= ' + mid)
def  subSql(strSql,subName,subValue):
    p 
=  re.compile( " : " + subName)
    
return  p.sub( subValue, strSql)
            
def  selectSql(sql):
    
print   ' select= ' + sql
    cursor.execute(sql.encode(dbCharset))
    
return  cursor.fetchall()

def  execSql(sql):
    
print   ' exec= ' + sql
    cursor.execute(sql.encode(dbCharset))
    connection.commit()
    
return  cursor.rowcount

def  _log(show):
    
print  show
    log 
=   ' echo  ' + show + '  >>  ' + logTxt
    os.system(log)
    
    
try :
    connStr
= username + " / " + userpwd + " @ " + dbname
    
print  connStr
    connection 
=  cx_Oracle.connect(connStr) 
    cursor 
=  connection.cursor()
    file 
=  open(dataFile)
    
if (len(sys.argv) == 2 ):
        startRow
= int(sys.argv[ 1 ])
    
else :
        startRow
= 1
    row 
=   1
    
for  str  in  file.readlines():
        
if (row >= startRow):
            str 
=  re.compile( " \n " ).sub(  "" , str )
            strs 
=  unicode(str, fileCharset).split( " | " )
            _log(
' start row= ' + row. __str__ () + '  crid= ' + strs[0] + '  muid= ' + strs[ 3 ])
            
try :
                action(strs)
            
except :
                _log(
' err row= ' + row. __str__ ())
            _log(
' end row= ' + row. __str__ () + '  crid= ' + strs[0] + '  muid= ' + strs[ 3 ])
        row
+= 1
finally :
    file.close()
    cursor.close()
    connection.close()   




本文转自博客园刘凯毅的博客,原文链接:py 数据库 ,如需转载请自行联系原博主。



目录
相关文章
|
消息中间件 数据库连接 数据库
py 多进程 引发的 各种数据库连接 消息队列连接 异常问题 简单分析
py 多进程 引发的 各种数据库连接 消息队列连接 异常问题 简单分析
246 0
|
数据库 数据安全/隐私保护 Python
Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统
Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统
Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统
|
数据库 Python
Py之Data Base:Python和数据库的那些嘻嘻哈哈事详细攻略
Py之Data Base:Python和数据库的那些嘻嘻哈哈事详细攻略
Py之Data Base:Python和数据库的那些嘻嘻哈哈事详细攻略
|
11月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
639 158
|
11月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
11月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1710 152
|
11月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1137 156
|
11月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(中)
使用MYSQL Report分析数据库性能
747 156
|
11月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(上)
最终建议:当前系统是完美的读密集型负载模型,优化重点应放在减少行读取量和提高数据定位效率。通过索引优化、分区策略和内存缓存,预期可降低30%的CPU负载,同时保持100%的缓冲池命中率。建议每百万次查询后刷新统计信息以持续优化
802 161
|
存储 运维 关系型数据库
从MySQL到云数据库,数据库迁移真的有必要吗?
本文探讨了企业在业务增长背景下,是否应从 MySQL 迁移至云数据库的决策问题。分析了 MySQL 的优势与瓶颈,对比了云数据库在存储计算分离、自动化运维、多负载支持等方面的优势,并提出判断迁移必要性的五个关键问题及实施路径,帮助企业理性决策并落地迁移方案。

热门文章

最新文章