Using SQLite in Python - Connecting to a Database (
Page 2 of 5 )
Now we're ready to begin working with pysqlite. To start using the library, it, of course, needs to be imported. It can be found under the name of pysqlite2, but we'll need the dbapi2 submodule. This should be imported under the name of sqlite for clarity:
>>> from pysqlite2 import dbapi2 as sqlite
Now we can begin using pysqlite. The first thing we'll do is create a new database to work with. This is done by creating a connection object and passing the name of a database file. If the database file does not exist, which, in our case, it doesn't, then it will be created. If it does exist, then it will be opened:
>>> connection = sqlite.connect('test.db')
It's also possible to create a temporary database that exists in memory:
>>> memoryConnection = sqlite.connect(':memory:')
Once we have a connection object, we must create a cursor object, which does the interaction for us. This is done by calling the cursor method of our connection object:
>>> cursor = connection.cursor()
| | Discuss Using SQLite in Python | | | | | | | Hello, all,
There are a number of ways to store data in Python, and I believe... | | | | | | The commit() method is a member of the connection class and not the cursor... | | | | | | Hello at first,
I\'ve found this article late, but not to late. Thank You. I\'m... | | | | | | Nice tutorial Peyton. Lately I've found myself wrestling with a bunch of poorly... | | | | | | This tutorial-articles is definitely the best for beginners like me. :) Easy to... | | | | | | Hmm, I see you posted this article in 2006, and here in 2008, I find it.
After... | | | | | | hello, your article is great and it shall help me a lot in my program but i am... | | | | | | Hello, I have gone through the whole explanation pasting dutifully
consecutive... | | | | | | hi i think at the top of the exam code should be:
row = cursor.fetchone()
for row... | | | | | | >>> Post your comment now! | | | | | |
|