我有两个几乎相同的程序。一个有效,另一个没有。无法运行的程序显示错误“格式字符串的参数不足”。它可能与变量dbname中的“%”有关,但我无法弄清楚为什么一个程序可以工作而另一个不能。在这两个程序中,我试图在带有LIKE的SELECT语句中使用通配符。
工作计划:
import subprocess
import sys
import commands
from sqlalchemy import create_engine
from sqlalchemy import Date, DateTime
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, Table, ForeignKey
from sqlalchemy import Integer, String
from sqlalchemy.sql import select
engine = create_engine('mysql://UID:PASS@999.999.99.99:9999/access_benchmark_staging', echo=True)
dest = engine.connect()
dest.execute("truncate table TABAUTH")
def dbapull(applid, ssid, host, port, dbname):
print "dbapull " + dbname + ""
source = pyodbc.connect('Driver={IBM DB2 ODBC DRIVER};Database=' + ssid +';Hostname=' + host + ';Port=' + port + ';Protocol=TCPIP;Uid=user;Pwd=password', echo=True)
src = source.cursor()
src.execute("SELECT DISTINCT SUBSTR(CURRENT SERVER,1,7) AS SSID, SUBSTR(B.GRANTEE,1,8) AS GRANTEE, B.UPDATEAUTH AS U, B.INSERTAUTH AS I, B.DELETEAUTH AS D, A.CREATOR, B.DBNAME, B.TTNAME, B.ALTERAUTH AS C FROM " + ssid + ".SYSIBM.SYSTABLES A LEFT JOIN " + ssid + ".SYSIBM.SYSTABAUTH B ON A.CREATOR = B.TCREATOR AND A.NAME = B.TTNAME WHERE A.CREATOR IN ('PFPROD','PGPROD','PSPROD','PS','PROD') AND A.DBNAME LIKE " + dbname + " AND (B.UPDATEAUTH <> ' ' OR B.INSERTAUTH <> ' ' OR B.DELETEAUTH <> ' ') AND A.TYPE IN ('T','V') AND B.GRANTEETYPE = ' ' AND A.NAME NOT IN ('DSN_VIEWREF_TABLE','DSN_PGRANGE_TABLE','DSN_SORTKEY_TABLE','DSN_SORT_TABLE','DSN_DETCOST_TABLE','DSN_FILTER_TABLE','DSN_PTASK_TABLE','DSN_STATEMNT_TABLE','DSN_PGROUP_TABLE','DSN_STRUCT_TABLE','DSN_PREDICAT_TABLE','PLAN_TABLE') ORDER BY 2")
for row in src:
#print (row)
row[0] = str(row[0]).strip()
row[1] = str(row[1]).strip()
row[2] = str(row[2]).strip()
row[3] = str(row[3]).strip()
row[4] = str(row[4]).strip()
row[5] = str(row[5]).strip()
row[6] = str(row[6]).strip()
row[7] = str(row[7]).strip()
row[8] = str(row[8]).strip()
result = dest.execute("insert ignore into TABAUTH values ('" + applid + "','" + row[0] + "','" + row[1] + "','" + row[2] + "','" + row[3] + "','" + row[4] + "','" + row[5] + "','" + row[6] + "','" + row[7] + "','" + row[8] + "')")
dbapull("AAA", "BBB", "CCC", "DDD", "'%PMC%'")
dest.close()
非工作计划:
import subprocess
import sys
import commands
from sqlalchemy import create_engine
from sqlalchemy import Date, DateTime
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, Table, ForeignKey
from sqlalchemy import Integer, String
from sqlalchemy.sql import select
engine = create_engine('mysql://UID:PASS@999.999.99.99:9999/access_benchmark_staging', echo=True)
dest = engine.connect()
dest.execute("truncate table DS_Users")
def userpull(appl, dbname):
print "DS User pull " + dbname + " "
source = create_engine('mysql://UID:PASS@999.999.99.99:9999/access_benchmark_staging', echo=True)
src = engine.connect()
src.execute("SELECT MF.profile_name AS profile_name, MF.groupuser_access as groupuser_access, MF.group_id as group_id, MF.user_id as user_id, MF.user_name as user_name, MF.default_group as default_group, MF.last_racinit as last_racinit, MF.password_last_changed_date as password_last_changed_date, MF.user_id_status as user_id_status, MF.creation_date as creation_date, ldap.uid as uid, ldap.company as company, ldap.emp_name as emp_name, ldap.title as title, ldap.contract_exp as contact_exp, ldap.dept_name as dept_name, ldap.emp_status as emp_status, ldap.disabled_date as disabled_date, ldap.term_date as term_date, ldap.bus_unit as bus_unit, ldap.manager_id as manager_id FROM (SELECT DST.profile_name, DST.groupuser_access, GRP.group_id, USR.user_id, USR.user_name, USR.default_group, USR.last_racinit, USR.password_last_changed_date, USR.user_id_status, USR.creation_date FROM AU_KRC_USER_REPORT USR INNER JOIN AU_KRC_GROUP_REPORT GRP ON USR.user_id = GRP.user_id INNER JOIN AU_KRC_DATASET_REPORT DST ON GRP.group_id = DST.groupuser_id WHERE (DST.profile_name LIKE " + dbname + " AND DST.profile_name NOT IN ('" + appl + ".SYSINFO.ABEND')) AND DST.groupuser_access IN ('UPDAT', 'ALTER') AND DST.groupuser_type = 'GROUP' ) MF LEFT OUTER JOIN ldap.ldap_raw ldap ON MF.user_id = ldap.kmart_mf GROUP BY MF.group_id, MF.user_id, MF.user_name, MF.default_group, MF.last_racinit, MF.password_last_changed_date, MF.user_id_status, MF.creation_date, ldap.uid, ldap.company, ldap.emp_name,ldap.title, ldap.contract_exp, ldap.dept_name, ldap.emp_status, ldap.disabled_date, ldap.term_date, ldap.bus_unit, ldap.manager_id")
for row in src:
#print (row)
row[0] = str(row[0]).strip()
row[1] = str(row[1]).strip()
row[2] = str(row[2]).strip()
row[3] = str(row[3]).strip()
row[4] = str(row[4]).strip()
row[5] = str(row[5]).strip()
row[6] = str(row[6]).strip()
row[7] = str(row[7]).strip()
row[8] = str(row[8]).strip()
row[9] = str(row[9]).strip()
row[10] = str(row[10]).strip()
row[11] = str(row[11]).strip()
row[12] = str(row[12]).strip()
row[13] = str(row[13]).strip()
row[14] = str(row[14]).strip()
row[15] = str(row[15]).strip()
row[16] = str(row[16]).strip()
row[17] = str(row[17]).strip()
row[18] = str(row[18]).strip()
row[19] = str(row[19]).strip()
row[20] = str(row[20]).strip()
result = dest.execute("insert ignore into DS_Users values ('" + appl +"','" + row[0] + "','" + row[1] + "','" + row[2] + "','" + row[3] + "','" + row[4] + "','" + row[5] + "','" + row[6] + "','" + row[7] + "','" + row[8] + "','" + row[9] + "','" + row[10] + "','" + row[11] + "','" + row[12] + "','" + row[13] + "','" + row[14] + "','" + row[15] + "','" + row[16] + "','" + row[17] + "','" + row[18] + "','" + row[19] + "','" + row[20] + "')")
userpull("PP", "'%PP.%'")
答案 0 :(得分:0)
尝试将sql放入事务中,并检查用于数据库的驱动程序是否支持查询的“%”。