I want to use MySQL in flask, and one config is
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://user:password#127.0.0.1:3306/db"
If I use mysql+pymysql, it can work
But when I only use mysql, the erroe message like this
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
But in my code, I don't import pymysql, so what is the pymysql and why need use that can work
I know pymysql is a moudle
Thanks your reply!
The create_engine function (which is what uses the URL given in the config), requires you to give it a "dialect". A "dialect" is the name of the underlying database engine that SQLAlchemy is connecting to.
However, since many databases have multiple different clients (in Python these implement DBAPI), so in many cases (such as for the mysql dialect) you're required to give the name of the client you want SQLAlchemy to use. In this case, you're asking it to use the pymysql library to actually handle connectivity with MySQL.
SQLAlchemy 1.3 supports the following dialect/DBAPI-libraries for connecting to MySQL:
mysqlclient (maintained fork of MySQL-Python)
PyMySQL
MySQL Connector/Python
CyMySQL
OurSQL
Google Cloud SQL
PyODBC
zxjdbc for Jython
Related
I am using Peewee and Psycopg2 via peewee's playhouse.db_url.connect. Is there a way or a parameter to instruct Peewee to reconnect automatically e.g. in case of a short outage in the PostgreSQL database while a given connection was open.
I want to know that if multi-users try to access same database for insert or retrieve data via my python program, do they all need to install PostgreSQL in their computers? How this process works?
If the database is located on another server, then no, clients don't have to install postgresql. They will need to install a postgresql adapter, like psycopg, so that python can communicate with the database, and most applications choose to use some type of ORM, like sqlalchemy, on top of the database adapter to make communication with the database more object-oriented and pythonic.
If you use psycopg2, it already comes with lipq DLL, the library used to communicate with PostgreSQL through TCP.
Actually, psycopg2 is a wrapper for libpq:
http://initd.org/psycopg/docs/
Therefore, you won't need to install PostgreSQL server binaries in the client workstations.
I'm new to Django. It wasted me whole afternoon to config the MySQL engine. I am very confused about the database engine and the database driver. Is the engine also the driver? All the tutorial said that the ENGINE should be 'django.db.backends.mysql', but how the ENGINE decide which driver is used to connect MySQL?
Every time it says 'django.db.backends.mysql', sadly I can't install MySQLDb and mysqlclient, but PyMysql and the official mysql connector 2.1.3 has been installed. How could I set the driver to PyMysql or mysql connector?
Many thanks!
OS: OS X Al Capitan
Python: 3.5
Django: 1.9
This question is not yet solved:
Is the ENGINE also the DRIVER?
You can import pymsql so it presents as MySQLdb. You'll need to do this before any django code is run, so put this in your manage.py file
import pymysql
pymysql.install_as_MySQLdb()
The short answer is no they are not the same.
The engine, in a Django context, is in reference to RDBMS technology. The driver is the library developed to facilitate communication to that actual technology when up and running. Letting Django know what engine to use tells it how to translate the ORM functions from a backend perspective. The developer doesn't see a change in ORM code but Django will know how to convert those actions to a language the technology understands. The driver then takes those actions (e.g. selects, updates, deletes) and sends them over to a running instance to facilitate the action.
I am learning Python / Flask. Now I am at the point where I am learning Flask with MySQL Database. I read lots of posts about using Flask with databases and most of them suggest to use SQLAlchemy. I tried to read about SQLAlchemy, but I didn't like that because I prefer building and executing SQL queries and creating tables in the database only. Is there any way that I can take full benefit of using Flask with MySQL Database without SQLAlchemy?
Please suggest.
You don't have to use SQLAlchemy, no. You just have to connect to the database in Flask and execute your queries manually.
This question is about how to connect to MySQL with Python, and the official docs go over creating a site with a SQLite database. Modify the examples provided there with your MySQL connection.
I have a standard django setup using postgres, but I also want to access data from a mssql database. I have installed pyodbc and was in the process of installing django-pyodbc but the instructions tell me how to make the mssql the default database which is not what I want.
I was hoping somebody could point me in the right direction. It doesnt bother me if the method bypasses django and just uses Python to retrieve the data
Cheers,
JJ
If you're using Django 1.2 or later, you can use Django's built-in multi-database support. You can follow the django-pyodbc directions, and give your database a name other than 'default'.