2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PolarsでMySQLデータベースからデータを読み込む

Last updated at Posted at 2023-05-30

Polarsではread_databaseメソッドを使ってMySQLなどのデータベースから読み込むことができます。

準備

特にMySQLデータベースから読み込むときにはconnectorxというエンジンを用いているため、これをインストールする必要があります。さらにpyarrowというものも必要でした:

$ pip install connectorx pyarrow

使い方

引数にはSQLクエリとuriを渡します。uriはMySQLの場合はmysql://{username}:{password}@{server}:{port}/{database}です。

import polars as pl

username = 'root'
password = ''
server = '127.0.0.1'
port = '3306'
database = 'test'

uri = f'mysql://{username}:{password}@{server}:{port}/{database}'
query = 'SELECT * FROM users'
df = pl.read_database(query, uri)
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?