Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'from sqlite3 import dbapi2 as sqlite3' vs 'import sqlite3'?

When I see the examples for pysqlite, there are two use cases for the SQLite library.

from sqlite3 import dbapi2 as sqlite3

and

import sqlite3

Why there are two ways to support the sqlite3 api? What's the difference between the two? Are they the same? In normal use, which would be preferred.

ADDED

I knew that they are different in terms of namespace, and I wanted ask if they are the same in terms of usage, I mean, do they have the same API set?

like image 366
prosseek Avatar asked Sep 20 '10 17:09

prosseek


People also ask

What is import sqlite3 in Python?

Source code: Lib/sqlite3/ SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage.

What is the correct way to install sqlite3 in Python?

Step 1 − Go to SQLite download page, and download precompiled binaries from Windows section. Step 2 − Download sqlite-shell-win32-*. Step 3 − Create a folder C:\>sqlite and unzip above two zipped files in this folder, which will give you sqlite3.

What is the correct syntax to import sqlite3 module?

#!/usr/bin/python import sqlite3 conn = sqlite3. connect('test. db') print "Opened database successfully"; Here, you can also supply database name as the special name :memory: to create a database in RAM.

Which of the following is not a method from sqlite3?

callback is not a function in sqlite.


2 Answers

They are the same. In the Lib/ directory of my Python installation (v2.6), the sqlite3 package contains a __init__.py file with this:

from dbapi2 import *

Which means that the two ways of importing are absolutely identical.

That said, I definitely recommend just using import sqlite3 - as this is the documented approach.

like image 75
Eli Bendersky Avatar answered Sep 25 '22 16:09

Eli Bendersky


They are not the same.

In the first case, you are importing the dbapi2 symbol from the sqlite3 module into the current namespace.

In the last case, you just import the sqlite3 module in the namespace.

The difference is that in the first case you can directly use dbapi2 (aliased as sqlite3) class, which in the latter case, you would have to reference to sqlite3.dbapi2 all the time you want to reference it.

See for more information the python documentation

like image 26
Ikke Avatar answered Sep 23 '22 16:09

Ikke