简体   繁体   English

Python和Sqlite,我收到操作错误

[英]Python and Sqlite, I am receiving operational error

I will bring below a function code snippet which I can't make to work correct way. 我将在下面带上一个我无法正常使用的功能代码段。

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()

I receive an Operational error: 我收到操作错误:

OperationalError: unknown database May2015

So guys, I found the problem. 伙计们,我发现了问题。 In my code i didn't strip the .csv file extention and that was the problem. 在我的代码中,我没有剥离.csv文件扩展名,这就是问题所在。 Thanks to CL for his advice to look in the deep to the name of the file. 感谢CL的建议,以深入研究文件名。

For those who stacks on a similar problem, the right code is: 对于那些解决类似问题的人,正确的代码是:

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM