13
14

More than 3 years have passed since last update.

SQLAlchemyのDB別接続方法まとめ

Last updated at Posted at 2019-12-15

SQLAlchemyをもっと便利に使いたい

Pythonを使っているならDB処理に例外なくSqlAlchemyを使っていると思います。
しかしSQLAlchemyは独特すぎていったんプロジェクトから離れてしまうと文法を忘れてしまいがちです。

今回はそうならないようにするために、いつでもこのページに戻ってすぐにSQLAlchemyに取り掛かれるようなページにしていこうと思っています

SQLAlchemyのDB別接続方法

元ネタのURL

基本的な文法は以下です。(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 新規作成
13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14