Access data from second external SQLite database in Django
NickName:Mihir Ask DateTime:2021-11-29T13:31:41

Access data from second external SQLite database in Django

This is my project structure

Project Structure

I am able to access the default SQLite database db.sqlite3 created by Django, by importing the models directly inside of my views files

Like - from basic.models import table1

Now, I have another Database called UTF.db which is created by someone else, and I want to access it's data and perform normal QuerySet operations on the retrieved data

The problem is I don't know how to import the tables inside that database, as they are not inside any model file inside my project as it's created by someone else

I tried adding the tables inside the UTF.db database to a models.py file by first adding it to the settings.py file like the following

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'otherdb':{
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'UTF.db',
    }
}

And then using the inspectdb command to add the tables to an existing models.py file

The command I tried out - python manage.py inspectdb > models.py

But, that just causes my models.py file to get emptied out

Does anyone know how this can be solved?

In the end, I wish to import the table data inside of my views files by importing the respective model

Copyright Notice:Content Author:「Mihir」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70150128/access-data-from-second-external-sqlite-database-in-django

Answers
sevdimali 2021-11-29T05:43:44

You can specify specific database as specified in Documentation\npython manage.py inspectdb --database=otherdb > your_app/models.py\n\nAlso if possible putting otherdb in a different App is better.",


Slavian Petrov 2021-11-29T09:21:06

You can attach the second database to the first one and use it from within the first one. You can use tables from both databases in single sql query.\nHere is the doc https://www.sqlite.org/lang_attach.html.\nattach database '/path/to/dbfile.sqlite' as db_remote;\n\nselect * \nfrom some_table\njoin db_remote.remote_table on ....\n\ndetach database db_remote;\n",


More about “Access data from second external SQLite database in Django” related questions

Access data from second external SQLite database in Django

This is my project structure I am able to access the default SQLite database db.sqlite3 created by Django, by importing the models directly inside of my views files Like - from basic.models import

Show Detail

Django Accessing external Database to get data into project database

i'm looking for a "best-practice" guide/solution to the following situation. I have a Django project with a MySql DB which i created and manage. I have to import data, every 5 minutes, f...

Show Detail

Django and external sqlite db driven by python script

I am just beginning learning Django and working through the tutorial, so sorry if this is very obvious. I have already a set of Python scripts whose ultimate result is an sqlite3 db that gets

Show Detail

How to use external database in django?

I am new to django, I have pretty good idea of basics. I am able to build model(Django database) and use it in templates. But now I want to connect external database to the django templates. How d...

Show Detail

Django - How does Django access data in sqlite?

How does Django access data in sqlite database? Lets assume this scenario: Very basic models.py, and views.py are created to accept user input from page and store it in sqlite database. If a user

Show Detail

Write to Django sqlite database with external script

I don't know if what I am trying to do makes the most sense or if there is a better way to do this. However, I am learning how to build a website using django. I am wondering, can I use an external

Show Detail

Access same SQLite database from 2 apps

I want to know if the following is possible. I want to create a new Settings App in Android and then another App as well. I want both to use the same database and I do not want to use a content pro...

Show Detail

Update second sqlite database from first database

I have database A which gets new data every second day. Now I have a second database B, which is a duplicate of database A, which is stored on a different server for read access. What I do at the m...

Show Detail

Django - Reading from Oracle database and writing to SQLite

I have configured settings.py already to have two databases: an existing Oracle database, and SQLite. I am new to Django, so I hope someone can give me a detailed answer. I want to create an app that

Show Detail

List Django database table names from external script

I am trying to access sqlite3 database that I have in my Django project from within external script that is not part of the project. However, the following will return an empty list: con = sqlite3.

Show Detail