1.准备 INSERT 的字典,其实有三个字段(物品名称
,型号
,序号
)内容,与表中已有的条目三个相同,这个准备插入的字典已经可以被认为是重复的,不需 INSERT,
字典全部字段有 32 个.
2.for 循环字典列表,逐条判断写入,字典列表有 900 个字典的话,需时 79 秒,虽然是练手学习,也无法接受
3.用
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(50) as executor:
for each in testDictList:
executor.submit(thDictSQL, SQLServerInfo,'testTBL',each,checkField)
仅需 48 秒能写入.
改成 ThreadPoolExecutor(500),39 秒,但是很大几率会丢了一些条目
# dict 转换成 SQL 语句
def DicttoSQLText(obj, tblName, SQLcmd):
returnText = ''
FiledStr = ''
ValueStr = ''
ccount = 0
if isinstance(obj, list):
for i in obj:
FiledStr = ', '.join(list(i.keys()))
ValueStr = "'" + '\', \''.join(list([str(x) for x in i.values()])) + "'"
SQLText = SQLcmd.format(tblName, FiledStr, ValueStr)
returnText += SQLText
ccount += 1
return returnText
if isinstance(obj, dict):
FiledStr = ', '.join(list(obj.keys()))
ValueStr = "'" + '\', \''.join(list([str(x) for x in obj.values()])) + "'"
SQLText = SQLcmd.format(tblName, FiledStr, ValueStr)
returnText += SQLText
ccount += 1
return returnText
# 执行 SQL 语句,返回字典结果
def SQLcmdData(cur, sqlcmd):
cur.execute(sqlcmd)
data = cur.fetchall()
if len(data) == 1:
return data[0]
else:
return data
# Insert 字典数据(函数可以用在 thread)
def thDictSQL(SerInfo, tblName, dataDict, checkField):
thSQLconn = pymysql.connect(host=SQLServerInfo['ip'], port=SQLServerInfo['port'], user=SQLServerInfo['user'],
password=SQLServerInfo['password'], database=SQLServerInfo['database'],
charset=SQLServerInfo['charset'])
thSQLCursor = thSQLconn.cursor(cursor=pymysql.cursors.DictCursor)
# 检查有无相同项目
checkStr = ''
# 组装检查 SQL 语句
for i in checkField:
checkStr += f"`{i}`='{dataDict.get(i)}' AND "
checkStr = checkStr.rstrip(checkStr[-4:])
thSQLSelectText = "SELECT id FROM {} WHERE ({})".format(tblName, checkStr)
ret = thSQLCursor.execute(thSQLSelectText)
# 根据检查结果写入
if not thSQLCursor.fetchall():
thSQLInsertText = DicttoSQLText(dataDict, "INSERT INTO {} ({}) VALUE ({});")
ret = SQLcmdData(thSQLCursor, thSQLInsertText)
thSQLCursor.close()
thSQLconn.close()
return thResult
if __name__ == '__main__':
testDictList = None
with open('h:/dd.dict', 'r') as f:
testDictList = eval(f.read())
StartTime = time.clock()
checkField = ['物品名称', '型号', '序号']
for i in testDictList:
i['DL'] = 0
i['DB'] = 0
i['UpdateTime'] = myFunc.nowTimeForStr()
thDictSQL(SQLServerInfo, 'cangku', i, checkField)
print(time.clock() - StartTime)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.