How to connect to a mysql database from Django? - mysql

I have an inscription.sql on a WAMP localhost:80 server, and I have also Django running on visual studio via a localhost:xxxx server.
I want to connect Django to the mysql database located on the wamp server (I have successfully installed MysqlClient).
Is there anyway to do this?

See the official guide to using an already existant database with Django.
To sum up, what you need to do is:
Make sure Django can connect to your database by setting the right DATABASE setting. From your comment, this seems to be the case.
Generate models (Python code) that match the tables in your database using the python manage.py inspectdb command. You must paste the generated code in the models.py file of an installed app and review them to make sure they fit your needs.
Create the tables required by Django with the python manage.py migrate command.

From the docs
Jus set the DATABASES variable in settings, then run python manage.py inspectdb > models.py put the generated models.py in the app's folder and finally run python manage.py migrate

Related

How to switch from MySQL to Postgre on Heroku

I want to host a django project on heroku. Locally, I developed it using MySQL database. And I have also pushed it to heroku but I get an Error message whenever I run heroku open command on command prompt. The error message is Can't connect to local MySQL server through socket. Though I'm a newbie in using databases, the way I understand the error is that heroku can't connect to local MySQL server which I'm using for local development. I don't want to connect to a remote MySQL database, instead, I want to connect to Postgre database just as heroku recommended.
Being a newbie in the use of database, I don't know the best way to migrate to Postgre or how to add MySQL in my project to .gitignore. Is it possible for me to start running Postgre on heroku while MySQL isn't added to . gitignore and is still the database in settings.py? Must I clear MySQL database or add it to gitignore before I can use Postgre?
PostgreSQL settings for Heroku:
Please install dj_database_url using below command:
pip install dj-database-url
In settings.py , import dj_database_url and add below settings at the end of the file:
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
Done !! Now, deploy again to Heroku.

django template does not change after update or insert data MySQL

My website running with django, uwsgi & nginx. whenever I insert or update data to mysql, the new content that rendered to page supposed to be changed but it wasn't until I refresh the pages many times or restart the uwsgi, I'm new using django, I don't know if it has something to do with caches.
the question is how can I manage the cache at django to maintain the database access in order to direct any changes in the page without restarting the uwsgi. Thank you.
Try to apply migration.Applying migration will update your database and the try to run server or visit your website it will show you change.
If you're at Django1.8+
use this command for migration:
python manage.py makemigrations
and after that use migrate command:
python manage.py migrate
and then run your server or relode the webpage.
hope it solve your issue

Kallithea sqlite database to mysql database

I've Kallithea running on my own server with sqlite database. I would like to move all this data to MySQL database instead. On default Kallithea uses sqlite if not any other database is specified. From Kallithea documentation pdf https://media.readthedocs.org/pdf/kallithea/latest/kallithea.pdf they recommend to use https://github.com/shazow/sqlalchemygrate to migrate data from database to another.
I'm installed sqlalchemygrate using Python pip but when I try to migrate there is parts what I don't understand at all. Help menu doesn't not clearly specify how to use migrate command. At least I don't understand it at all. For example when I try to type
grate migrate "sqlite:./kallithea.db" "mysql://kallithea#localhost/kallithea"
I get error saying
ImportError: No module named sqlite
In help menu it says to use
migrate METADATA ENGINE_FROM ENGINE_TO
Migrate schema or data from one engine to another.
And example how to use it
grate migrate model.meta:metadata \
"mysql://foo:bar#localhost/baz" "sqlite:///:memory:" \
What I don't really understand is what is that metadata it needs? And how to specify sqlite .db file for this. And how to migrate data to new kallithea database in mysql with user kallithea which has all privileges to that database.
Not sure about Kallithea but RhodeCode users had a great success using TAPS (https://github.com/ricardochimal/taps) project to migrate they databases to different format.
ImportError: No module named sqlite
You probably need to install sqlite, and pip install sqlite or similar.
Regarding what metadata it needs, it's referring to metadata in the context of SQLAlchemy: http://docs.sqlalchemy.org/en/latest/core/metadata.html

How can I import a sqlite database created in django?

Iam a beginner to django. I did run syncdb after mentioning the database file in manage.py. Is it possible to view the contents (tables) of the database in mysql or phpmyadmin?
Run dbshell:
python manage.py dbshell
This will put you in a shell as if running mysql with the correct connection params.
If you want to use phpmyadmin with it, that's a matter of correct phpmyadmin configuration, which depends only on your database, it has nothing to do with Django. So check your phpmyadmin settings and configure your database the same way you did in settings.py of the Django project.
phpMyAdmin does not support SQLite databases.

How do I make changes to a Django Model after it has been deployed to a webserver?

In development, whenever I make changes to the underlying model in my Django application, I'm used to:
Stopping the development server
In MySql, dropping the existing database
running $>python manage.py syncdb
Restarting the development server by running $>python manage.py runserver
Once deployed using WSGI & Apache, however, I don't have the development server running. As such, I don't know how to stop the server, and just running:
$>python manage.py syncdb
didn't appear to update the underlying MySql database that instantiates the model. So, short of tearing down the entire instance, how do I update my database?
Note: I implemented this before I learned about db migrations using "south" so I'm looking at raw Django, I suspect.
Django doesn't have a migration framework built-in (yet!), so there's no such thing as "raw django for migrations".
Consequently, you either do that manually (using SQL to alter the table), or you use south.
Now, my recommendation would be to use south.