表X没有名为Y的列(Python)

0 投票
1 回答
837 浏览
提问于 2025-04-18 12:30

这是一个较长的Python程序中处理数据库的部分:

dbcon = lite.connect('spcbase.db')  # Connects to database file spcbase.db.
cur=dbcon.cursor()                  # Creates database cursor.
cur.execute("CREATE TABLE IF NOT EXISTS spectrum(spectrumID INTEGER PRIMARY KEY AUTOINCREMENT, seriesID INTEGER, scan_ang DECIMAL, Path TEXT)")
cur.execute("CREATE TABLE IF NOT EXISTS series(seriesID INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, gpsx DECIMAL, gpsy DECIMAL, colprec DECIMAL, refangle DECIMAL)")
    # Executes SQL-query that will create one table of spectrums
    # and one table over series of measurements, should these
    # tables or this database not exist.
with dbcon:
    cur.execute("INSERT INTO series(date, gpsx, gpsy, colprec, refangle) VALUES(CURRENT_DATE, ?, ?, ?, ?)", [AP[0], AP[1], 6, refangle])
cur.execute("SELECT MAX(seriesID) FROM series")
current_series = cur.fetchone()[0]
src = u'.\\MaestroData'
dest = u'.\\target'
files=getspc(src)
i=0
for mfile in files:
    oldpath=os.path.normpath(os.path.join(src,mfile))
    print "oldpath: ", oldpath
    newpath=os.path.normpath(os.path.join(dest,mfile))
    print "newpath", newpath
    os.rename(oldpath,newpath)
    with dbcon:
        cur.execute("INSERT INTO spectrum(seriesID, scan_ang, Path) VALUES (?, ?, ?)", [current_series, scan_dirs[i], newpath])
    i=i+1
dbcon.close()

运行时出现了错误:“操作错误:表spectrum没有列scan_ang”,尽管在几行之前已经声明了这个列。到底出了什么问题呢?

1 个回答

1

你忘记提交了:

dbcon.commit()

只有这样,任何修改的请求才会真正生效。

撰写回答