This is an aggregation on how to use SQLAlchemy to create a database, table and insert data into the created table. Refer to this tutorial on how to use SQLAlchemy. from sqlalchemy_utils import database_exists, create_database from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker url = "mysql+pymysql://username:password@database_address/database_name" # Create … Continue reading [python]SQLAlchemy usage example
Tag: SQLAlchemy
[python]VARCHAR requires a length on dialect mysql with sqlalchemy
I remember I did not need to define the varchar length, however I encountered this error: sqlalchemy.exc.CompileError: (in table 'test_table', column 'name'): VARCHAR requires a length on dialect mysql So I would need to define the length of varchar with the String object from SqlAlchemy. meta = MetaData() """ Define the table. The below is … Continue reading [python]VARCHAR requires a length on dialect mysql with sqlalchemy
[python]Create database if not exists with sqlalchemy
This is an example of using SQLAlchemy module to create database if it does not exist otherwise connect to the requested database. from sqlalchemy import create_engine from sqlalchemy_utils import database_exists, create_database from getpass import getpass """ database url is dialect+driver://username:password@db_address/db_name To connect to mysql/mariadb, pymysql module is required to install. The purpose of using SqlAlchemy … Continue reading [python]Create database if not exists with sqlalchemy
[python]Create table with SQLAlchemy
I am using the raw module of SQLAlchemy, and not the Flask's version of SQLAlchemy, Flask's version is easier to use than the original as more abstraction has been implemented by Flask. This post is to record how tables can be created. Requirements.txt These python libraries are necessary, for this example I am using Mariadb … Continue reading [python]Create table with SQLAlchemy
[python]Flask Migrate
Flask Migrate advantage This is for easy update of the existing database, such as create a new column, create a new table, drop a table, revert previous database. With migrate, I do not need to do the db.create_all() anymore. A note on sqlite3, once a table is created you cannot insert column directly, need to … Continue reading [python]Flask Migrate
[python]Query object from table with Flask SQLAlchemy
The purpose of using SQLAlchemy is to avoid using SQL syntax, and Flask's extension of SQLAlchemy makes usage easier than the original SQLAlchemy. So here are two demonstrations to query every row and query one row with a condition.
[python] Flask SQLAlchemy insert data
On previous post it was demonstrated that creating database is easier with Flask SQLAlchemy than using SQLAlchemy itself. This post demonstrates how data can be inserted to existing database, and also to check if the tables exist before executing create_all(). create_all() does not re-create table if the table exists, the if statement is to demonstrate … Continue reading [python] Flask SQLAlchemy insert data
[python] Create database with flask-sqlalchemy
On previous post I have used a pure SQLAlchemy module to just create the database, this time I am using Flask_SQLAlchemy to do it, which makes the creation simpler. I will need to put in two configuration parameters: SQLALCHEMY_DATABASE_URI SQLALCHEMY_TRACK_MODIFICATIONS For the configuration I have put them in a config.py file. As usual you need … Continue reading [python] Create database with flask-sqlalchemy
[python]SQLAlchemy create mysql table
On previous post, SQlite3 was created using SQLAlchemy, to change from SQLite to MySQL, you need to download pymysql module then change the URI from sqlite:///objects.db to mysql+pysql://[username]:[password]@[ip address of the mysql server]/[database name] example if username is cyruslab and password is mypassword and ip address of mysql is 192.168.1.1 and database name is firewall_objects … Continue reading [python]SQLAlchemy create mysql table
[python]Create database with SQLAlchemy
The objective of learning SQLAlchemy is to use its Object Relational Mapper (ORM), this allows programmer who does not do SQL syntax to also do CRUD on supported database, the ORM does the "translation or mapping" for us in the background, in our code we only need to do CRUD with python syntax. The SQLAlchemy … Continue reading [python]Create database with SQLAlchemy