简体   繁体   中英

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. Thanks to CL for his advice to look in the deep to the name of the file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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