[Tutor] mysqlite

Alan Gauld alan.gauld at btinternet.com
Thu Mar 7 00:44:24 CET 2013


On 06/03/13 18:50, Lolo Lolo wrote:

> i then closed this interactive IDLE session. i reopened another session and simply did
>
>>>> import sqlite3
>>>> cxn = sqlite3.connect('sqlite_test/test')

This gets you a connection to the data base

>>>> cur = cxn.cursor()

and this sets up a storage area for any results

>>>> for user in cur.fetchall():
>              print(user)

But at this point the cursor is empty, you haven't read anything from 
the database. You need to execute some kind of SELECT statement to 
populate the cursor from the database.

> My questions about http://www.alan-g.me.uk/tutor/tutdbms.htm is to
 > open a database it uses the command line directly
> and doesnt creat cursor() or connect() objects e.g.

Thats becsause the connection and cursor mechanism is how Python 
interacts with SQLite. When you use the SQLite prompt you don't need 
them SQLite connects itself automatically.

AS you read further in the tutorial page you'll see the section where we 
jump from direct SQL command line into using Python and there you will 
see connect/cursor.


> E:\PROJECTS\SQL> sqlite3 employee.db
> sqlite> create table Employee
>     ...> (EmpID,Name,HireDate,Grade,ManagerID);
> sqlite> insert into Employee (EmpID, Name, HireDate, Grade, ManagerID)
>     ...> values ('1020304','John Brown','20030623','Foreman','1020311');
>
> i tried this in my command line but got :
> python.exe: can't open file 'sqlite': [Errno 2] No
>   such file or directory

I'm not running it from Python I'm running it from the OS command line.
You need to open a CMD shell window and cd to your SQL database folder
(E:\PROJECTS\SQL in the example above) and then run sqlite3.

> i tried creating the a folder called test and doing:  test employee.db
> but no test.db was created.

You need to change into the test directory. Then run

sqlite3 employee.db

and when you are done a new employee.db file should exist.

> my main questions are it possible to use the command line
 > without cursor() and connect() in python 3

No only from the sqlite command line.

The great thing about command lines is there are so many
to choose from:

The OS
SQLite
Python

and then from within Python you can use:
the debugger
the help system
and, if you are really perverse, you can even use Tcl from within Tkinter!!

such choice! ...such confusion...

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list