Handling Local and Remote DB in Django - mysql

I'm using Django and MySQL to serve a website. I have my own MySQL server on localhost for my site. Now I have a third-party MySQL database that I need to use to read and write to that is on a remote host. I set up the new database's information in my settings.py file, but a problem occurs when I try to access the remote DB. Because the new DB is not created by me, I have no respective classes in models.py file. Reading from it the regular way of Model.objects.using('remote_db_name').get(pk=0) doesn't work because it throws a NameError.
I was wondering what is the correct way to handle both a local and remote database in Django, especially when the remote database only gives you SELECT and INSERT privileges. All the tutorials I've found online are about multiple databases defined in the same Django site, so the models.py file has all the data needed.
Thanks for any help in advance!

You have two options:
Use Django's legacy database support to autogenerate models for your existing database.
Skip the Django ORM, and use raw SQL to execute SQL statements against the database.

I had many hard-time moments with Django's legacy support - Django was not designed strictly to support legacy databases. Of course there are some tools/methods (like Ned told above) but I'd rather recommend SQLAlchemy as an alternative for you. It's very fast and it was designed to support any kind of databases no matter if they were created via sqlalchemy nor they were legacy dbs.
Of course if you need all other Django's elements, go for the Ned's solution, but remember that you have to create django core tables in this legacy db, so you'll need CREATE privilege.

Related

Is it possible to access/edit a remote SQLite3 database with Sequelize from a website

So I have an SQLite3 database with Sequelize on a VPS and a Website I hosted on Hostinger.
Is it possible to access and edit the data in the database from the website?
Any help is appreciated.
[my code is in nodejs]
Yes and No.
Easy and simple answer - now;
Longer - yes, but you would need to map the directories, so server running your sequelize will think it accesses normal directory.
You can download the database, edit it and then send it back. sqlite3 is file database, you dont really connect to it (usually)
SQLite is a database format based on a single file. It's simple but not massively performant. It has no networking features.
The SQLite client needs to have access to the filesystem that the database lives on.
If they are on different computers, then you could do it by using some kind of network filesystem (e.g. via fuse) but this would be really slow.
A better approach would be to run a webservice on the computer the file is stored on, and access the database through there.

Replicate mnesia database to Mysql

I have installed ejabberd on an AWS EC2 instance and am using the smack library to connect to it with my android app. At the moment the ejabberd server is using mnesia as the database, however I want to perform some complex queries on some of the data (mainly the MUC room names), as SQL will predominantly the best solution for this, I was wondering if it was possible to replicate the required data to an external MySQL database that I could then query.
Is this possible or am i better looking at a different approach to this problem?
There is no module built into ejabberd to replicate data in Mnesia to MySQL. However, the usual approach is to use the backend you need for each feature. If you want mod_muc to store data into MySQL instead of Mnesia, you can just change the backend to odbc (which means it will store data for that module in a relational database).
You can refer to ejabberd documentation for MUC module: http://docs.ejabberd.im/admin/guide/configuration/#modmuc
Once your MySQL is configured and schema is loaded, you can set db_type to odbc on a case by case basis to choose MySQL for that module.

Access database from a differet host in django

I have a django site and I need to access data from another database which is in another host. how do I connect it and even if i connect, how do I access that table from this database, without having the table's data in the models.py? Please help me
If you want to use the Django ORM, you can use Django's built-in multi-database support. To create the models.py file, you can use the inspectdb management command.
If you don't need the ORM, you can connect still specify the additional database in your settings, but only perform raw SQL queries. You need to get the right connection for your additional database:
from django.db import connections
cursor = connections['my_db_alias'].cursor()

Where are databases stored in the rails server?

I'm having a lot of run with Ruby, creating some basic web apps. When looking at the logs of the rails server in terminal, I see mysql queries.
Refinery::User Load (0.2ms) SELECT "refinery_users".* FROM "refinery_users" WHERE "refinery_users"."id" = 1 LIMIT 1
Theses are relating to databases that I've created, but where do these databases exist? In the rails server? Where is the rails server stored in OSX? Can I browse what's inside, specifically, the databases?
Thanks, I know this doesn't have much practical use, but I want to understand the concepts behind what's going on, rather than just having superficial knowledge.
By default, Rails uses SQLite3. The database files are stored in the /db directory in the root of your app. There should be a file called development.sqlite3.
To access that database, open a terminal session, go to the root directory of your app and enter sqlite3 db/development.sqlite3. More info on the sqlite shell here: http://www.sqlite.org/sqlite.html
Rather than messing around in the SQLite shell, I think you'd be better off 1) looking at /db/schema.rb to see the structure of your database and 2) using rails console to look at the data.
If you want to know, for any given Rails app, what database it's using, look at /config/database.yml. That gives you the adapter, name of the database, location, etc.
Also, SQLite is generally just for kicking off development. I wouldn't recommend using it when your schema starts getting more complex. Personally, I never use it. I immediately set up a mySQL database for any new Rails projects.

Differences between SQLite and MySQL - login and security

At first I searched in Stack Overflow about the difference between MySQL and SQLite. So there is some answers but still I want to know something. When accessing MySQL I need to give host, username, password and database parameters. But when accessing SQLite I just give database name. Can anyone explain me this why, and for security reasons also, should not be there password for SQLite database, can not be it just downloaded for example from server, so I am totally stuck. So need your help.
The difference is that MySQL is a database server, while SQLite is a database engine that works against single database files.
You are correct that the SQLite database file could just be downloaded if it's placed right in the web application where anyone can reach it.
Most server providers offer a folder where you can place files like this, so that the web application itself can reach it, but it's not directly downloadable.
You can also optionally add protection to the SQLite database file. See this question:
Password Protect a SQLite DB. Is it possible?
SQLite is mainly used for mobile and tablet apps.So they are just used for a single system.
But in MySql its a complete database and can be used from multiple system. So for increasing security ,we have to give all these. So that no one can misuse the data...
SQLite is just a file. If you have read access to the file that is the implicit security model (as well as the possibility to use database encryption).
MySQL is a network service. It can listen on an internet facing socket, meaning anyone in the world can access it if they have the right authentication credentials.
SQLite is file based, MySQL is a service which runs on the server. With SQLite you don't need to enter the database name, but actually the database file.
If you put the database file at an inaccessible location, you should be totally fine and the user will not be able to download the file.