3

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
>>>
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Nov 10, 2010 at 15:47
6
  • You would have to describe your requirements better. Commented Nov 10, 2010 at 15:49
  • 1
    @sqace_cowboy: get something small implemented fast on a broken system without sqlite sqlite3. Solution is below I think ... Commented Nov 10, 2010 at 16:41
  • Accepted a half-answer, ignored the sane answer, and didn't even bother to Google (the Python module name is sqlite3, not sqlite, as searching for "python sqlite" would have told you). -1 for not even trying. Commented Nov 10, 2010 at 17:29
  • @Glenn Maynar ... I know sqlite3 and the python module. I use it at home. Here it is broken. Trying to find a work-around on a deadline. I find it as frustrating as you but saying "use sqlite3" when I say "no sqlite3" is not a solution. That is why I did accept the simple solution as being evidence of no other simple thing to do. Please read complete post before delivering negative responses. If I have misunderstood your comment, please correct me. Commented Nov 10, 2010 at 19:19
  • You were doing import sqlite when I made that comment, not import 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.so to 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. Commented Nov 10, 2010 at 19:52

3 Answers 3

14

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.

answered Nov 10, 2010 at 15:52
Sign up to request clarification or add additional context in comments.

4 Comments

To qualify this a bit, SQLite is very fast as long as you stay within what it's designed to do. If you try to have many writers on an SQLite database you're going to be crying yourself to sleep, and as its SQL planner isn't very advanced it's not always fast at complex queries. That said, it's the most widely-deployed database software in the world and it's almost certainly what the OP wants.
sqlite3 module comes with python? but that requires sqlite3 which is not standard no? In either case, neither sqlite, sqlite3 nor _sqlite3.so are available on any of the versions of python (2.4.2.7 3.0) installed here. The world is a beautiful place.
@S.Lott ... sorry that was meant to by python versions 2.4, 2.7 & 3.1.2
So I think 3.1.2 is the up-to-date python and there is some sort of pathological problem with the install.
4

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?

answered Nov 10, 2010 at 15:50

1 Comment

Thanks. I will do some queries. I think having a list of dicts probably makes sense. Then I can simply query things with something like [ x for x in data if condition(x)==True ]. Shouldn't be too awful. I just want to get something done before wasting time doing sysadmin tasks trying to get broken installs of python and databases working.
2

Since Version 2.5 python supports Sqlite. It is very lightweight and not external.

answered Nov 10, 2010 at 15:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.