Django RandomDB

An app for allowing django projects to connect with random, user-specified non-django (or django) databases, and to manipulate them using dynamically-created Models. Requires Django 1.2 or greater (for multi-db support).

A quick example (a simple python script that runs in a django environment):

from randomdb import Database, NotConnected, TableDoesNotExist

    # Database(engine, name, [host], [port], [user], [password], [options])

    # Can connect to any database django can connect to, with the same
    #   options you would use with django.
    # db = Database(
    #           engine='django.db.backends.mysql',
    #           name='myproject_db',
    #           # blank 'host' for 'localhost'
    #           port=3306,
    #           user='username',
    #           password='mypa$$'
    #       )

    db = Database('django.db.backends.sqlite3', '/path/to/some/sqlite3.db')
except NotConnected:
    print "Couldn't connect"
    import sys

# Fetch a list of table names (as [unicode] strings, naturally)
tables = db.get_table_list()

print 'Tables in this database:'
print ', '.join(tables)
print ''

# If a table doesn't exist, fetching a model for it will
#   raise TableDoesNotExist
    Model = db.get_model_for_table('foobar')
except TableDoesNotExist:
    print "(Table 'foobar' doesn't exist; is that good or bad?)"
    print ""

table = None
while table not in tables:
    table = raw_input('Which table to work with? ')
    if table not in tables:
        print "That table isn't available."

# Get a regular django model for any table.
Model = db.get_model_for_table(table)

# And query it like you normally would.
print "Fields for %s:" % table
print ', '.join(Model._meta.get_all_field_names())
print ''

print "Out of %d rows, the first row's values are:" % Model.objects.count()
print Model.objects.values()[0]