Creating a mysql database using sqlalchemy - python

I'm trying to create a mysql database using sqlalchemy.
I have a flask web app which contains an sqlite database. I'm trying to switch over to mysql. I'm fairly new to sqlalchemy and have learned how to create databases via sqlalchemy. However the databases were sqlite databases. I'm trying to use sqlalchemy to create a mysql database and after reading multiple posts i still can't seem to get it.
I've also installed python-mysqldb
Snippets of the original code used to create an sqlite database which was successful.
Base = declarative_base()
class Categories(Base):
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
image = Column(String)
link = Column(String)
description = Column(String)
if __name__ =='__main__':
engine = create_engine('sqlite:///app/database/main.db', echo=True)
Base.metadata.create_all(engine)
mysql attempt
if __name__ =='__main__':
engine = create_engine('mysql://user:password#localhost/app/database/main.db', echo=True)
Base.metadata.create_all(engine)
mysql 2nd attempt
if __name__ =='__main__':
engine = create_engine('mysql://user:password#localhost/app/database/main.db', echo=True)
engine.execute("CREATE DATABASE main.db")
engine.execute("USE main.db")
The error i keep receiving.
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database 'app/database/main.db'") (Background on this error at: http://sqlalche.me/e/e3q8)
My best guess is that there's clearly something i'm missing about using mysql with sqlalchemy.
Any help to even point me in the right direction would be greatly appreciated.

a) The correct DBURI syntax for mysql is:
mysql://username:password#servername/databasename
b) The database databasename must be created first. So before you run the Python .create_all() you should connect to the db server using the command line mysql client and execute the CREATE DATABASE databasename statement to create an empty database:
$ mysql -u username -p
... type password
> CREATE DATABASE databasename;
c) You should now be able to run the Python code to create the tables in the empty database.

Related

Python SQLAlchemy in-memory database connect

I'm creating a in-memory database with python and I want to use SQLAlchemy with it.
All my application is currently working directly with queries to the db.
I've seen multiple ways of connecting but none of it is working. My current attempt stands as:
# Creates an sqlite database in memory
db = Database(filename=':memory:', schema='schema.sql')
db.recreate()
# ORM
engine = create_engine('sqlite:///:memory:')
Base = automap_base()
Base.prepare(engine, reflect=True)
User = Base.classes.user
session = Session(engine)
This gives AttributeError: user. How do I properly connect my database to the SQLAlchemy?
from sqlalchemy import create_engine
from sqlalchemy.orm import registry, Session
engine = create_engine("sqlite+pysqlite:///:memory:", echo=True, future=True)
session = Session(engine)
registry().metadata.create_all(engine)
ATTACH is your friend.
You can attach an in-memory database to the current database session.
E.g.,
db.init('sqlite://')
db.execute("ATTACH DATABASE ':memory:' AS my_database")
db.create_all()

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: database does not exist

I am trying to populate a database with two tables in SQLAlchemy. I have already created the database test_database and now I am trying to create 2 tables inside this. I have already checked that this database is successfully created using \l. Following is the code for the file create.py which creates a database with two tables:
import os
from flask import Flask, render_template, request
from models import *
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://shammun:my_password#localhost:5432/test_database.db'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# db = SQLAlchemy()
db.init_app(app)
def main():
db.create_all()
if __name__ == "__main__":
with app.app_context():
main()
This file create.py imports model.py which generates two tables, the code of which is given below:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Flight(db.Model):
__tablename__ = "flights"
id = db.Column(db.Integer, primary_key=True)
origin = db.Column(db.String, nullable=False)
destination = db.Column(db.String, nullable=False)
duration = db.Column(db.Integer, nullable=False)
class Passenger(db.Model):
__tablename__ = "passengers"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
flight_id = db.Column(db.Integer, db.ForeignKey("flights.id"), nullable=False)
Now, in the terminal when I run the file create.py, I get the following error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: database "test_database.db" does not exist
Actually, this is almost the same question that I asked in this post db.create_all() doesn't create a database a month ago. The only difference is that I asked the wrong question that database was not created. Actually the question would be why the database wasn't found and why would it throw an error. As this question was closed and as I have tried so many times for a long time to resolve it and couldn't find any solution, I am asking almost the same question again. I will much appreciate if someone can help me to lift me from this bottleneck where I am stuck for a long time.
Check on what port is your postgres running using this command \conninfo
cause I doubt your PostgreSQL database is running on some different port.
Default port of PostgreSQL is 5432, but if it is already occupied by some other application then it tries next empty port and starts running on 5433
So in your app config variable of SQLALCHEMY_DATABASE_URI, try changing 5432 to 5433 and see if it works.
Edit 1:
Try removing .db from your database name test_database.db, and just put test_database
Change this:
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://shammun:my_password#localhost:5432/test_database.db'
To this:
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://shammun:my_password#localhost:5432/test_database'

heroku connect to mysql database

I have push a python-django project to heroku and it works well. In my view.py file of django model, I added function that could connect to the local mysql database to retrieve data from the mysql. The function is the view.py is as followed:
#login_required
def results(request):
data=[]
data1 = []
owner = request.user
owner = str(owner)
db = MySQLdb.connect(user='root', db='aaa', passwd='xxxxx', host='localhost')
cursor = db.cursor()
cursor.execute("SELECT search_content, id, title, author, institute, FROM result_split where username = '%s'" % (owner))
data = cursor.fetchall()
db.close()
return render(request, "webdevelop/results.html", {"datas": data})
But when I try to open the page that show the data from mysql database in the deployed heroku website, it show the error:"OperationalError at /results/
(2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")". How could I have this heroku project to connect to my local mysql database? Or I should choose alternative?
Firstly, you need to ensure that the user and password you're using to connect to MySQL is correct and that the user has the correct privileges to work with the selected database.
Then you can check that mysql is accepting connections on localhost.
As for directly addressing the Connection Refused exception, check things like the mysql socket used to communicate with localhost applications like your Django project. The socket must exist and be configured in MySQL.
I also recommend taking a look at something like SQLAlchemy for Python which will help you interact directly with the database using Python objects. For example,
Connecting to the database:
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, scoped_session, mapper
from config import DB_URL
"""Database Declaration"""
metadata = MetaData()
Base = declarative_base(name='Base', mapper=mapper, metadata=metadata)
engine = create_engine(DB_URL, pool_recycle=1800)
Session = sessionmaker(bind=engine, autocommit=False, autoflush=True)
session = scoped_session(Session)
You can now use session variable to perform queries and updates using its inherited functions from the SQLAlchemy Session class.
SQLAlchemy also includes a declarative model for telling Python what your tables look like. For example,
class Clinic(Base):
__tablename__ = 'clinic'
clinic_id = Column(Integer, primary_key=True)
clinic_name = Column(VARCHAR)
address = Column(VARCHAR)
city = Column(VARCHAR)
zip = Column(VARCHAR)
phone = Column(VARCHAR)
user_id = Column(VARCHAR)
These examples work well for my projects in Flask and should work well enough in Django.

How do I create a table in PostgreSQL with SQLAlchemy?

I tried to create a table in the "app" database, but got this error:
app=# CREATE TABLE users;
ERROR: syntax error at or near ";"
LINE 1: LINE 1: CREATE TABLE users;
^
The columns are defined in models.py which is why I don't create them here.
Do not create the tables in PostgreSQL, SQLAlchemy will create the table (and the columns) for you. All you need to do is create the database, which you have done. To create the tables with Flask-SQLAlchemy:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False, unique=True)
with app.app_context():
db.create_all()
To address the error (although as stated above you should not do this), it is because that is not a full CREATE TABLE expression. You need to specify zero or more columns for the table.
CREATE TABLE my_table ();
In my case forgot to import my Model class and code
run without any error:).
If you call create_all() on interactive mode please ensure that you import particular Model class.
Note: Every time you make a change on Model class restart ipython.

sqlalchemy,creating an sqlite database if it doesn't exist

I am trying out sqlalchemy and i am using this connection string to connect to my databases
engine = create_engine('sqlite:///C:\\sqlitedbs\\database.db')
Does sqlalchemy create an sqlite database for you if one is not already present in a directory it was supposed to fetch the database file?.
Yes,sqlalchemy does create a database for you.I confirmed it on windows using this code
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///C:\\sqlitedbs\\school.db', echo=True)
Base = declarative_base()
class School(Base):
__tablename__ = "woot"
id = Column(Integer, primary_key=True)
name = Column(String)
def __init__(self, name):
self.name = name
Base.metadata.create_all(engine)
As others have posted, SQLAlchemy will do this automatically. I encountered this error, however, when I didn't use enough slashes!
I used SQLALCHEMY_DATABASE_URI="sqlite:///path/to/file.db" when I should have used four slashes: SQLALCHEMY_DATABASE_URI="sqlite:////path/to/file.db"
Linux stored SQLite3 database
database will be create in the same folder as the .py file:
engine = create_engine('sqlite:///school.db', echo=True)
will instantiate the school.db file in the same folder as the .py file.
I found (using sqlite+pysqlite) that if the directory exists, it will create it, but if the directory does not exist it throws an exception:
OperationalError: (sqlite3.OperationalError) unable to open database file
My workaround is to do this, although it feels nasty:
if connection_string.startswith('sqlite'):
db_file = re.sub("sqlite.*:///", "", connection_string)
os.makedirs(os.path.dirname(db_file), exist_ok=True)
self.engine = sqlalchemy.create_engine(connection_string)
#Gandolf's answer was good.
The database is created it when you make any connection with your engine.
Here's an example of doing nothing with a database besides connecting to it, and this will create the database.
from sqlalchemy import create_engine
engine = create_engine('sqlite:///database.db')
with engine.connect() as conn:
pass
Without the engine.connect() or some form of metadata.create_all() the database will not be ceated.

Categories

Resources