Can i access different database in django than other default database - python

I have the database backup script in python which inserts some data in mysql database .
Now my Django is in different database.
How can i access different database because i don't have any objects in Models.py.
i want to display some data in django interface

Yes, you can setup multiple database and access every one of them.
you can get the specified database connection cursor using this:
from django.db import connections
cursor = connections['my_db_alias'].cursor()
where my_db_alias is your another db alias .
check the doc:
https://docs.djangoproject.com/en/1.3/topics/db/multi-db/

Related

Data from PostgreSQL sql not all inserting to another Sql Server DB

I have to manually push data from my django app to be inserted on another DB which using using another desktop application . Nearlly all the data saved on my postgress db for that particular id is inserted on that other DB .
But it seems only one data is missing. Could it be that i have not set any Connection timeout on my setting.py file fro my DB .

How to drop table and recreate in amazon RDS with Elasticbeanstalk?

My database on Amazon currently has only a little data in it (I am making a web app but it is still in development) and I am looking to delete it, make changes to the schema, and put it back up again. The past few times I have done this, I have completely recreated my elasticbeanstalk app, but there seems like there is a better way. On my local machine, I will take the following steps:
"dropdb databasename" and then "createdb databasename"
python manage.py makemigrations
python manage.py migrate
Is there something like this that I can do on amazon to delete my database and put it back online again without deleting the entire application? When I tried just deleting the RDS instance a while ago and making a new one, I was having problems with elasticbeanstalk.
The easiest way to accomplish this is to SSH to one of your EC2 instances, that has acccess to the RDS DB, and then connect to the DB from there. Make sure that your python scripts can read your app configuration to access the configured DB, or add arguments for DB hostname. To drop and create your DB, you must just add the necessary arguments to connect to the DB. For example:
$ createdb -h <RDS endpoint> -U <user> -W ebdb
You can also create a RDS snapshot when the DB is empty, and use the RDS instance actions Restore to Point in Time or Migrate Latest Snapshot.
I had the same problem and came up with a workaround. In your python code just add and run the following method when deploying your app the next time:
FOR SQLALCHEMY AFTER VERSION 2.0
from sqlalchemy import create_engine, text
tables = ["table1_name", "table2_name"] # the names of the tables you want to delte
engine = create_engine("sqlite:///example.db") # here you create your engine
def delete_tables(tables):
for table in tables:
sql = text(f"DROP TABLE IF EXISTS {table} CASCADE;") # CASCADE deltes the tables even if they had some connections to other tables
with engine.connect() as connection:
with connection.begin():
connection.execute(sql)
delete_tables(tables) # Comment this line out after running it once.
FOR SQLALCHEMY BEFORE VERSION 2 (I guess)
def delete_tables(tables):
for table in tables:
engine.execute(f"DROP TABLE IF EXISTS {table} CASCADE;")
delete_tables(tables) # Comment this line out after running it once.
After you deployed and ran this code 1 time, all your tables will be deleted.
IMPORTANT: Delete or comment out this code after that, otherwise you will delete all your tables every time when you deploy your code to AWS

No SQLlite database in directory after calling create_engine in SQLAlchemy

After navigating to a directory and typing the commands in the Python shell:
from sqlalchemy import *
db = create_engine('sqlite:///tutorial.db')
I do not see a database file called tutorial.db file in the directory. Do I have to use a different command to create the actual database file and save it?
SQLAlchemy's Engine object lazily constructs the actual underlying database connection, waiting until the first database operation before trying to connect to the database.
Try running a query or creating a table and see if the database appears.

Django: Create DatabaseWrapper for dynamic database connection

sorry for my english.
I want to create a DatabaseWrapper because I want to make a dynamic database connection.
I need an object of the DatabaseWrapper for the Django Querybuilder: http://django-query-builder.readthedocs.org/en/latest/ref/query.html#querybuilder.query.Query
Why?
- Because I don't want to store the database config in the settings.py - DATABASE variable.
I want to create a connection during the runtime and close it after my class is used. The reason for that is that if I want to test, Django creates a test_database on the second database. Surely this is not a problem but I don't have writing privileges on the database, hence my tests fail every time.

How to delete pymongo.Database.Database object

I am using pymongo to connect to mongodb in my code. I am writing a google analytic kind of application. My db structure is like that for each new website I create a new db. So when someone registers a website I create a new db with that name, however when unregistering the website I wish the database to be deleted. I remove all the collection but still the database could not be removed
And as such the list of databases is growing very large. When I do
client = MongoClient(host=MONGO_HOST,port=27017,max_pool_size=200)
client.database_names()
I see a more than a 1000 list of apps. Many of them are just empty databases. Is there a way that I remove the mongo databases ?
Use drop_database method:
client = MongoClient(host=MONGO_HOST,port=27017,max_pool_size=200)
client.drop_database("database_name")

Categories

Resources