import pymysql
#連接數據庫
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
passwd='123456',
db='test',
charset='utf8',
)
cursor = conn.cursor()
f = open('C:\\Users\\zyl\\Desktop\\新建文件夾\\shangweiwenjian.txt', "r",encoding = 'utf-8')
while True:
#逐行讀取
line = f.readlines()
if line:
#處理每行\n
line = "".join(line)
line = line.strip('\n')
line = line.split(",")
content = line[0]
cursor.execute(
"insert into article(content_detail) values(%s)",
[content])
else:
break
f.close()
cursor.close()
conn.commit()
conn.close()