为什么Sqlite告诉我没有这样的列存在,而我明明创建了它?

1 投票
1 回答
6854 浏览
提问于 2025-04-16 07:07

在使用Python 2.6.5和sqlite3.version 2.4.1的时候,我写了以下代码:

c = conn.cursor()

# Create table
c.execute('''create table stocks
(date text, trans text, symbol text,
 qty real, price real)''')

# Insert a row of data
c.execute("""insert into stocks
          values ('2006-01-05','BUY','RHAT',100,35.14)""")

# Save (commit) the changes
conn.commit()

c.execute('''insert into stocks values(date=?, trans=?, symbol=?, qty=?, price=?
)''', ('08-26-1984', 'SELL', 'GOGL', 3, 400.00))

# We can also close the cursor 

if we are done with it
c.close()

结果出现了一个错误:

Traceback (most recent call last):
  File "dbtest.py", line 18, in <module>
    c.execute('''insert into stocks values(date=?, trans=?, symbol=?, qty=?, price=?)''', ('08-26-1984', 'SELL', 'GOGL', 3, 400.00))
sqlite3.OperationalError: no such column: date

我想问一下,这到底是怎么回事??我明明创建了一个叫“date”的列!我花了两个小时试图搞明白到底出了什么问题,真是让我很沮丧。而且,当我尝试通过命令行打开它时,系统告诉我:

无法打开数据库“orders”:文件被加密或不是一个数据库

如果能得到任何帮助,我会非常感激,因为我快要把电脑摔墙上了。

谢谢!

1 个回答

8

这个插入语句的写法不对。你可以试试下面这个:

>>> c.execute('''insert into stocks 
                 (date, trans, symbol, qty, price)values(?,?,?,?,?)''', 
                 ('08-26-1984', 'SELL', 'GOGL', 3, 400.00))

撰写回答