Any thoughts on the best way to implement a table (i.e. a small relational database) in python without using any external databases extra modules and when the sqlite3 module is broken or missing.
user:~ $ python3
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/bns/rma/local/python/lib/python3.1/sqlite3/__init__.py", line 24, in <module>
from sqlite3.dbapi2 import *
File "/bns/rma/local/python/lib/python3.1/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
>>> ^D
user:~ $ python2.7
Python 2.7 (r27:82500, Jul 28 2010, 11:39:31)
[GCC 3.4.3 (csl-sol210-3_4-branch+sol_rpath)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dcottr/local/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/dcottr/local/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
>>>
3 Answers 3
Use sqlite3.
- It comes with python, you don't need external databases or extra modules.
- It can create the whole database on memory. You don't need extra files on disk if you don't want to.
- It's lightning fast.
- It can do modern queries on the
tables, like
JOINs
There's no reason to not use it. It will be faster and more complete than any solution you roll up on your own.
4 Comments
You can basically represent a tables as a list of lists/tuples. Then you can have dict to represent indexes.
Will you do queries in the table or just "have" it represented? What do you need this for?
1 Comment
Since Version 2.5 python supports Sqlite. It is very lightweight and not external.
sqlite3, notsqlite, as searching for "python sqlite" would have told you). -1 for not even trying.import sqlitewhen I made that comment, notimport sqlite3, and edited the post after I made it. I'd recommend recompiling Python locally on the system you're running on, and copying the resulting_sqlite3.soto a local module directory in the Python search path to fix it. Even if you don't have root access to the system, you should be able to drop in a fixed sqlite3 module in this way.