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 数据库 ,如需转载请自行联系原博主。



目录
相关文章
|
9月前
|
消息中间件 数据库连接 数据库
py 多进程 引发的 各种数据库连接 消息队列连接 异常问题 简单分析
py 多进程 引发的 各种数据库连接 消息队列连接 异常问题 简单分析
56 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和数据库的那些嘻嘻哈哈事详细攻略
|
21天前
|
SQL 关系型数据库 MySQL
【揭秘】MySQL binlog日志与GTID:如何让数据库备份恢复变得轻松简单?
【8月更文挑战第22天】MySQL的binlog日志记录数据变更,用于恢复、复制和点恢复;GTID为每笔事务分配唯一ID,简化复制和恢复流程。开启binlog和GTID后,可通过`mysqldump`进行逻辑备份,包含binlog位置信息,或用`xtrabackup`做物理备份。恢复时,使用`mysql`命令执行备份文件,或通过`innobackupex`恢复物理备份。GTID模式下的主从复制配置更简便。
91 2
|
16天前
|
弹性计算 关系型数据库 数据库
手把手带你从自建 MySQL 迁移到云数据库,一步就能脱胎换骨
阿里云瑶池数据库来开课啦!自建数据库迁移至云数据库 RDS原来只要一步操作就能搞定!点击阅读原文完成实验就可获得一本日历哦~
|
19天前
|
关系型数据库 MySQL 数据库
RDS MySQL灾备服务协同解决方案构建问题之数据库备份数据的云上云下迁移如何解决
RDS MySQL灾备服务协同解决方案构建问题之数据库备份数据的云上云下迁移如何解决
|
16天前
|
人工智能 小程序 关系型数据库
【MySQL】黑悟空都掌握的技能,数据库隔离级别全攻略
本文以热门游戏《黑神话:悟空》为契机,深入浅出地解析了数据库事务的四种隔离级别:读未提交、读已提交、可重复读和串行化。通过具体示例,展示了不同隔离级别下的事务行为差异及可能遇到的问题,如脏读、不可重复读和幻读等。此外,还介绍了在MySQL中设置隔离级别的方法,包括全局和会话级别的调整,并通过实操演示了各隔离级别下的具体效果。本文旨在帮助开发者更好地理解和运用事务隔离级别,以提升数据库应用的一致性和性能。
95 2
【MySQL】黑悟空都掌握的技能,数据库隔离级别全攻略
|
22天前
|
数据可视化 关系型数据库 MySQL
Mysql8 如何在 Window11系统下完成跳过密钥校验、完成数据库密码的修改?
这篇文章介绍了如何在Windows 11系统下跳过MySQL 8的密钥校验,并通过命令行修改root用户的密码。
Mysql8 如何在 Window11系统下完成跳过密钥校验、完成数据库密码的修改?
|
19天前
|
SQL 关系型数据库 MySQL
【MySQL 慢查询秘籍】慢SQL无处遁形!实战指南:一步步教你揪出数据库性能杀手!
【8月更文挑战第24天】本文以教程形式深入探讨了MySQL慢SQL查询的分析与优化方法。首先介绍了如何配置MySQL以记录执行时间过长的SQL语句。接着,利用内置工具`mysqlslowlog`及第三方工具`pt-query-digest`对慢查询日志进行了详细分析。通过一个具体示例展示了可能导致性能瓶颈的查询,并提出了相应的优化策略,包括添加索引、缩小查询范围、使用`EXPLAIN`分析执行计划等。掌握这些技巧对于提升MySQL数据库性能具有重要意义。
50 1
|
20天前
|
关系型数据库 MySQL Linux
在Linux中,如何配置数据库服务器(如MySQL或PostgreSQL)?
在Linux中,如何配置数据库服务器(如MySQL或PostgreSQL)?