这是处理较长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()
运行时会出现错误“操作错误:表格谱没有列scan_ang”,尽管声明这样的列只有几行。有什么问题?
答案 0 :(得分:1)
你忘了承诺:
dbcon.commit()
只有这样才会发生任何修改查询。