Access database from a differet host in django - mysql

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()

Related

How to map entity to db already created with Doctrine/Symfony5?

I want to deploy my Symfony5 project on to a remote shared hosting (Hostinger). I've already set up an account on Hostinger and I've also managed to create a database through their database module using "phpMyAdmin".
Now, how can I work with Doctrine if my database is already created and on Hostinger? All the examples I've seen when using Doctrine and Symfony5, create a local database and operate with it. Is it the same steps for a remote database already created?
In my Symfony5 project I've already installed the "symfony/orm-pack" and I've also updated my "DATABASE_URL" on the ".env" file as follows: DATABASE_URL="mysql://u811510647_guillemba:[db_password_hidden_for_obv_reasons]#127.0.0.1:3306/u811510647_containers?serverVersion=10.2"
The parameters of: [db_user, db_password, db_name and serverVersion] are all adapted on what Hostinger's remote database is telling me to enter.
The "symfony console doctrine:database:create" makes no sense if the database is already created no? How can I create an entity on this remote database with Doctrine?
I'd really appreciate some help, thanks a lot!
It sounds like you have an existing database and you want to import the schema to your Symfony project?
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
Note: If you want to specify a specific database from your project then you can also pass in the em
--em=myconnectionname

How to import the data from a data dump (SqLite3) into django?

I have a data dump file(.sql) containing certain data, I would like to import it to my database(currently Sqlite3 but thinking about changing to MySQL) in order to use it in my test website.
Also, I need to know if it's possible to add the models too automatically, I presume it needs to be added manually, but any how, if there is any way to solve it, please suggest it.
There is a way to help you generate the Django models automatically given you have an existing database.
However this is a shortcut. You may then fine tune your models and add them to your app as needed.
Structuring your models in apps might force you to use the Models db_table meta option.
If at some point you would like to switch databases (Sqlite3 -> MySQL) you can export (dump) your current data to json. Then you could import (load) the data to the new database (after creating the database tables with migrate command). To do this you can use Django management commands:
Dump data
Load data
I was able to get an alternative answer after researching a bit.
Since I'm having a PostgreSQL data dump file with a file extension '.sql', I was capable of running a single command that imported the whole data dump into my local database, which is PostgreSQL. I'm using PgAdmin4 as my database management system and I installed psql during the installation of PgAdmin4, I added the psql to the path of my command prompt, hence it was accessible.
In order to import the data dump, I used the command provided below,
psql -U <username> -d <database_name> < <file.sql>
The '<' after database_name is necessary, so be sure to include it.
Here is the username of the configured account, is the database to which the data dump should be added, , is the file containing the data dump.

How to migrate large amount of data from MySQL database to SQL Server database with C# service

I have a MySQL database contains large amount of data.
I wanted to write a C# service program to migrate that data into SQL Server database.(Service should work fine with Insert as well as update with out performance issue).
Please help us if there is a way to accomplish this. If there is another way to achieve this is also fine. But, the MySQL client is not present in the local machine. But we could access the database from the server.
You can use the import and export functions in sqlserver, after installing a odbc mysql

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.

Handling Local and Remote DB in Django

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.