[英]Python and Sqlite, I am receiving operational error
我將在下面帶上一個我無法正常使用的功能代碼段。
def upload_csv():
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
#as far as tkFileDialog returns absolute path to file, we have to slice from last slash till the end
filename = fn[fn.rfind("/")+1:]
cursor.execute("CREATE TABLE IF NOT EXISTS {0}('MSISDN' INTEGER PRIMARY KEY, 'IMEI' TEXT, 'TAC' INTEGER );".format(filename))
reader = csv.reader(open(fn,'r'))
for row in reader:
to_db = [unicode(row[0]),unicode(row[1]),unicode(int(row[2][0:8]))]
print to_db
cursor.execute("INSERT INTO data.{0} (MSISDN,IMEI,TAC) VALUES (?,?,?);".__format__(filename), to_db)
conn.commit()
我收到操作錯誤:
OperationalError: unknown database May2015
伙計們,我發現了問題。 在我的代碼中,我沒有剝離.csv文件擴展名,這就是問題所在。 感謝CL的建議,以深入研究文件名。
對於那些解決類似問題的人,正確的代碼是:
#as far as tkFileDialog returns absolute path to file, we have to slice from last slash till the end and also strip the extention!
filename = fn[fn.rfind("/")+1:].strip('.csv')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.