代码之家  ›  专栏  ›  技术社区  ›  Wayne Werner

为什么SQLite告诉我,当我清楚地创建它时,不存在这样的列?

  •  1
  • Wayne Werner  · 技术社区  · 14 年前

    在Python 2.6.5和sqlite3.version2.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
    

    我的问题-怎么回事??? 我创建了一个名为“日期”的列!在过去的两个小时里,我一直在想到底是什么错了,我真的很沮丧。另外,当我试图通过命令行打开它时,我被告知:

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

    当我要把电脑挂在墙上时,任何帮助都将不胜感激。

    谢谢!

    1 回复  |  直到 14 年前
        1
  •  7
  •   mechanical_meat nazca    14 年前

    不正确 syntax 对于插入语句。这将起作用:

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