包含被调用函数的文件db.py:
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='asdasd',db='db0')
cur = conn.cursor()
def mysql_insert(date,high,low):
try:
param=[date,high,low]
sql="INSERT INTO au_day (date,high,low) VALUES(%s,%s,%s)"
cur.execute(sql,param)
except:
return 0
mysql_insert('2001-11-11',33,44) #在文件下函数可以正常使用
conn.commit()
cur.close()
conn.close()
调用函数的文件a.py:
import database as db
for line in open("C:\\Users\hang\Desktop\\a.txt"):
# a.txt每一行都是这种格式
# 2008-01-09,230.950,230.990,221.880,223.300,10334
l=line.split(",")
x=l[0]
y=l[1]
z=l[2]
db.mysql_insert(x,y,z) //这里运行后没有向数据库插入任何数据,参数传递单引号什么的我也弄乱了理不清
初次使用python,麻烦大家帮忙看一看怎么改能将a.txt每一行的某些数据插入到数据库中
因为库文件名为db.py。
所以应该是import db