Python DBUtils数据连接池与ms sql配合用法,pythondbutils,[Python]代码#c
Python DBUtils数据连接池与ms sql配合用法,pythondbutils,[Python]代码#c
[Python]代码
#coding=utf-8import os,psycopg2import _mssqlimport pymssqlfrom DBUtils.PooledDB import PooledDBhosts="192.168.1.32"users="test"passwords="12345"dbnames="testdb"ms_hosts="192.168.1.20"ms_users="sa"ms_passwords="sa"ms_databases="ABS0323"def initDatabase(filename,conn): fs=open(filename,"r") sql=fs.read() cursor=conn.cursor() cursor.execute(sql) conn.commit() cursor.close() conn.close() fs.close() print "ok"def ExportFromMs(): conn=pymssql.connect(host=ms_hosts,user=ms_users,password=ms_passwords,database=ms_databases) cursor=conn.cursor() lst_result=[] try: cursor.execute("SELECT PSP_ID,PSP_NAME,PSP_DTS_ID,PSP_GND_ID,PSP_ADDRESS,PSP_ZIP,PSP_CTY_ID,PSP_MOBILE,PSP_VIPNUM,"\ " PSP_REGISTRATION_DT FROM BPROSPECT") table=cursor.fetchall() if(table is not None and len(table)>0): for row in table: line=[row[0],row[1].decode("cp936"),row[2],row[3],row[4].decode("cp936"), row[5].decode("cp936"),row[6],row[7],row[8],row[9]] lst_result.append(line) except Exception,e: print e cursor.close() conn.close() return lst_resultdef importToPostgresql(lstprospect,conn): cursor=conn.cursor() sql=("INSERT INTO BPROSPECT(PSP_ID,PSP_NAME,PSP_DTS_ID,PSP_GND_ID,PSP_ADDRESS,PSP_ZIP,PSP_CTY_ID,PSP_MOBILE,PSP_VIPNUM,"\ " PSP_REGISTRATION_DT,PSP_CREATIONUID,PSP_CREATION_DT,PSP_UPDATEUID,PSP_UPDATE_DT) VALUES(" \ "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'import',now(),'import',now())") try: #for row in lstprospect: #print dir(cursor) cursor.executemany(sql,lstprospect) except Exception,e: print e conn.rollback() else: conn.commit() print "import success..." cursor.close() conn.close()if __name__=="__main__": pool=PooledDB(psycopg2,2,port=5432,database=dbnames,host=hosts,user=users,password=passwords) initDatabase("ob.sql",pool.connection()) #lstprospect=ExportFromMs() #importToPostgresql(lstprospect,pool.connection()) pool.close()
相关内容
- python实现的ping,python实现ping,#!/usr/bin/e
- python解压缩*.tar.gz 文件,python.tar.gz,下面的脚本可以在
- Python 在字节数组中搜索 findBytes,pythonfindbytes,python代码
- python返回lambda函数,pythonlambda函数,def action(x
- python不换行输出内容,python换行输出,原理: 输
- Function emulation using __call__,emulation__call_,A simple but
- python匿名函数lambda,python匿名lambda,#lambda#普通的函
- python实现插入法排序算法,python算法,def insertso
- python函数中使用global语句代码演示,pythonglobal,#!/usr/b
- 遗传算法的神经网络python实现代码,神经网络python,##
评论关闭