'from sqlite3 import dbapi2 as sqlite3' vs 'import sqlite3'?
Asked Answered
B

2

6

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?

Burd answered 20/9, 2010 at 17:57 Comment(2)
technically the second is plain wrong, do you have a copy-paste error?Add
@Eli : I corrected the mistake. Thanks.Burd
A
9

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.

Add answered 20/9, 2010 at 18:26 Comment(0)
B
1

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

Birdwatcher answered 20/9, 2010 at 18:2 Comment(1)
Thanks for the answer, I wanted to ask if they support the same API set or not. I elaborated the question.Burd

© 2022 - 2024 — McMap. All rights reserved.