0

I am trying to write create a sqlite3 database using python 3.4.2 called "podcasts". I created two files:

dbcreate.py:

import sqlite3 as db

conn = db.connect('test.db')
cursor = conn.cursor()
cursor.execute("create table podcasts(name text, hosts text, channel text)")
print("table created")

dbinsert.py:

import sqlite3 as db

conn = db.connect('test.db')
cursor = conn.cursor()
cursor.execute('insert into podcasts values("Invisibilia","NPR", "Lulu Miller and Alix Spiegel")')
cursor.execute('insert into podcasts values("Hanselminutes", "independent", "Scott Hanselman")')
conn.commit()
conn.close()

When I ran the module for dbcreate.py, it printed "table created" like it should. But when I ran the module for dbinsert.py, I got no output. So I then went to the command line on my Macbook Air and when I entered the python3.4 shell and wanted to access both files from the command line, I got:

>>> dbcreate.py
Traceback (most recent call last):
  File "stdin", line 1, in module
NameError: name 'dbcreate' is not defined

and:

>>> dbinsert.py
Traceback (most recent call last):
  File "stdin", line 1, in module
NameError: name 'dbinsert' is not defined

When I tried accessing the database from sqlite3 in the command line by typing in .databases, I get:

I don't know how to access the database I created in dbinsert.py so I would appreciate any help you can give me. Thank you.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
brown1001
  • 677
  • 1
  • 9
  • 22
  • For how to run a Python script from inside Python, see [here](http://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-args). The Windows console allows copying/pasting text. And what output did you expect? – CL. Jan 18 '15 at 20:06
  • I'm using a Macbook (Apple) command line. The output I'm expecting are the values that I put inside the conn.execute lines: "Invisibilia", "NPR", and "Lulu Miller and Alix Spiegel", and "Hanselminutes", "independent", and "Scott Hanselman". – brown1001 Jan 18 '15 at 20:13
  • Why would you try to open a Python script inside the sqlite3 shell? – Daniel Roseman Jan 18 '15 at 20:19

1 Answers1

0

You are trying to run your .py files from within the Python shell, which will not work. Instead, from the command line, run

python3 dbcreate.py
python3 dbinsert.py

assuming that python3 points to your installation of Python 3.4.2. This will execute your code, creating the .db file and inserting the values you indicated.

There will be no output from dbinsert.py, as you are not printing anything, just executing database commands.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • I got /Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python: can't open file 'dbcreate.py': [Errno 2] No such file or directory – brown1001 Jan 18 '15 at 20:28
  • @user907 are you in the directory where the files are? – MattDMo Jan 18 '15 at 20:30
  • You were right, there was no output in dbinsert.py. I accessed the right directory. – brown1001 Jan 18 '15 at 20:33