【问题标题】:Creating Tables with Python and SQLite, no such table用 Python 和 SQLite 创建表,没有这样的表
【发布时间】:2014-02-23 11:47:27
【问题描述】:

我正在使用 Python 通过 SQLite3 创建一个表,我被告知该表已在解释器中创建,但是在 cmd 中查看该表时,没有这样的表。这是我的代码:

import sqlite3 as db

conn = db.connect('test.db')
cursor = conn.cursor()
cursor.execute("create table films(title text, year text, director text)")
print("tables added")

在解释器中我得到:

>>> ================================ RESTART ================================
>>> 
tables added

但是当我在 cmd 中通过 SQLite 查看数据库时,我被告知:

Error: no such table: films

【问题讨论】:

    标签: python sql python-3.x sqlite


    【解决方案1】:

    确保在同一目录中运行脚本和解释器。否则他们会引用两个不同的数据库文件。

    【讨论】:

      【解决方案2】:

      您的代码可以正常工作 :-) 我猜您尝试访问表的方式有问题:

      sucre:~ rlucia$ python sqltable.py
      tables added
      sucre:~ rlucia$ sqlite3 test.db
      SQLite version 3.8.0.2 2013-09-03 17:11:13
      Enter ".help" for instructions
      Enter SQL statements terminated with a ";"
      sqlite> PRAGMA table_info([films]);
      0|title|text|0||0
      1|year|text|0||0
      2|director|text|0||0
      sqlite> ^D
      sucre:~ rlucia$ 
      

      【讨论】:

        【解决方案3】:

        您缺少 conn.commit()。因此您的新表没有保存,SQL 查看器也找不到该表。

        【讨论】:

          猜你喜欢
          • 2012-10-16
          • 2013-03-07
          • 2015-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多