簡體   English   中英

使用Python和SQLite創建表,沒有此類表

[英]Creating Tables with Python and SQLite, no such table

我正在使用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

確保在同一目錄中運行腳本和解釋器。 否則,它們將引用兩個不同的數據庫文件。

您的代碼才有效:-)我想您嘗試訪問表的方式出了點問題:

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$ 

您缺少conn.commit()。 因此,不會保存您的新表,並且SQL查看器無法找到該表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM