[Asm] 纯文本查看 复制代码 # encoding=utf-8
import xlrd
from configparser import ConfigParser
import pymysql
import sys
try:
boo = xlrd.open_workbook("222.xls") # 文件名,把文件与py文件放在同一目录下
except:
print("打开excel文件失败!")
try:
sheet = boo.sheet_by_name("Page1") # execl里面的worksheet1
except:
print("加载工作表失w败")
# 连接数据库
try:
database = pymysql.connect(host="4x'x'x'x'x'x69",
port = 3306,
user="x'x'x'x'x'x",xxxxxx
passwd = "xxxxxxxxxx",
db = "x'x'x'x'x'xz",
charset = 'utf8')
except:
print("未连接到数据库")
cursor = database.cursor()
select = "select count(id) from wp_posts" # 获取表中xxxxx记录数
cursor.execute(select) # 执行sql语句
line_count = cursor.fetchone()
# print(line_count[0])
for i in range(1, sheet.nrows): # 第一行是标题名,对应表中的字段名所以应该从第二行开始,计算机以0开始计数,所以值是1
wzm = sheet.cell(i, 1).value # 取第i行第0列
tp = sheet.cell(i, 2).value # 取第i行第1列,下面依次类推
lj = sheet.cell(i, 11).value
# create_time = now()
value = (wzm, tp, lj )
insert = "INSERT INTO dm_intention_pool(post_title,post_content,guid)VALUES(%s,%s,%s)"
cursor.execute(insert,value) # 执行sql语句
update = "UPDATE `wp_posts` SET `create_time` =now(),`update_time` =now() where id > %s"
cursor.execute(update, line_count[0]) # 执行sql语句
database.commit() # 提交
cursor.close() # 关闭连接
database.close()
运行到cursor.execute(insert,value) # 执行sql语句这个地方就出错了 代码是抄来的 sql语句不会 大神帮帮我把 万分感谢 |