HarryMoy
HarryMoy

Reputation: 171

Creating Tables with Python and SQLite, no such table

I'm using Python to create a table through SQLite3, I'm getting told the table has been created in the interpreter but when looking at the table in cmd, there's no such table. Here's my code:

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")

In the interpreter I get:

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

But when viewing the database through SQLite in cmd I get told:

Error: no such table: films

Upvotes: 1

Views: 1270

Answers (3)

Scratchcat1
Scratchcat1

Reputation: 53

You are missing conn.commit(). Therefore your new table isn't saved and the SQL viewer cannot find the table.

Upvotes: 0

Rocco Lucia
Rocco Lucia

Reputation: 11

Your code just works :-) I guess there's something wrong on how you're trying to access your table:

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$ 

Upvotes: 0

falsetru
falsetru

Reputation: 368874

Make sure you run the script and interpreter in the same directory. Otherwise they referenece two different db files.

Upvotes: 1

Related Questions