SQLAlchemyをもっと便利に使いたい
Pythonを使っているならDB処理に例外なくSqlAlchemyを使っていると思います。
しかしSQLAlchemyは独特すぎていったんプロジェクトから離れてしまうと文法を忘れてしまいがちです。
今回はそうならないようにするために、いつでもこのページに戻ってすぐにSQLAlchemyに取り掛かれるようなページにしていこうと思っています
SQLAlchemyのDB別接続方法
基本的な文法は以下です。(PostgreSQLの場合)
from sqlalchemy import create_engine
engine = create_engine('dialect+driver://username:password@host:port/database')
以下が一覧になります。ユーザー名、パスワード、データベース名は仮です
# | DB名 | Database URL |
---|---|---|
1 | MySQL | mysql://scott:tiger@localhost/foo |
2 | PostgreSQL | postgresql://scott:tiger@localhost/mydatabase |
3 | SqLite | sqlite:////absolute/path/to/foo.db' |
4 | Microsoft SQLServer(ODBC) | mssql+pyodbc://scott:tiger@mydsn |
4 | Microsoft SQLServer(Pymssql) | mssql+pymssql://scott:tiger@hostname:port/dbnam |
5 | Oracle | oracle://scott:tiger@127.0.0.1:1521/sidnam |
更新履歴
- 2019/12/16 新規作成