[Tutor] mysqlite :p:

Paradox paradox at pobox.com
Wed Mar 6 23:16:01 CET 2013


On 03/07/2013 02:50 AM, Lolo Lolo wrote:
> im working through alan's sql tutorial. i have a few questions from there but 1st i wanted to try out an example in Core python's sql example.
>
>>>> import sqlite3
>>>> cxn = sqlite3.connect('sqlite_test/test')
>>>> cur = cxn.cursor()
> after this i created a table called users and inserted some data. then i did:
>
>>>> cur.execute('SELECT * from users')
>>>> for user in cur.fetchall():
>              print(user)
>
> i got the results of all data i gave the table. then did
>
>>>> cur.close()
>>>> cxn.commit()
>>>> cxn.close()
> i then closed this interactive IDLE session. i reopened another session and simply did
>
>>>> import sqlite3
>>>> cxn = sqlite3.connect('sqlite_test/test')
>>>> cur = cxn.cursor()
>>>> for user in cur.fetchall():
>              print(user)
I normally use sqlalchemy for manipulating sqlite databases but I think 
you need to do a select command or something before trying to do a 
fetchall method.  It looks like you are doing a fetchall on a cursor 
object rather than a query object.
> but this time no data was printed back to me, even though the database file 'test' already existed from the previous session. Why did this happen, cant i just connect and my data is still intact? or do i have to always re enter data from the previously saved session?
>
> 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.
>
> 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
>
Are you trying to do this from the command line or from within python? 
It looks like python is complaining but if you are using the regular 
commandline interface to sqlite3, python shouldn't be invoked at all and 
thus never complain.  Incidentally (and I haven't gone through Allan's 
tutorial on sql) I love the -column and -headers commands when invoking 
sqlite from the command line.  Put them like this:

sqlite3 -column -header employee.db

Makes the data much easier to read when you do a select command (at 
least for me).
> i tried creating the a folder called test and doing:  test employee.db
> but no test.db was created.
>
> my main questions are it possible to use the command line without cursor() and connect() in python 3 or is it only for python 2. is it better from the command line or in a .py file. And also my previous question i had about having to re enter previously stored data
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> .
>
thomas



More information about the Tutor mailing list