0
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?

ChatGPTに次の問を出しました。

Julia で、PostgreSQL のテーブルを Dataframe に読み込む方法

この質問だけで、サンプルのコードが出てきました。

結果を修正したプログラム

chatgpt.jl
#! /snap/bin/julia

using DataFrames, LibPQ

# PostgreSQLへの接続情報を設定
# conn = LibPQ.Connection("dbname=city user=scott password=tiger123")
conn = LibPQ.Connection("postgresql://scott:tiger123@localhost/city")

# クエリの作成
query = "SELECT * FROM cities"

# クエリを実行して結果を取得
result = LibPQ.execute(conn, query)

# 結果をDataframeに変換
df = DataFrame(result)
println(df)

# PostgreSQLとの接続を閉じる
# LibPQ.finish(conn)
close(conn)

# 最初の数行を表示してみる
println(first(df, 5))

実行結果

$ ./chatgpt.jl 
9×4 DataFrame
 Row │ id       name     population  date_mod   
     │ String?  String?  Int32?      Date       
─────┼──────────────────────────────────────────
   1 │ t3461    広島          25176  2003-09-24
   2 │ t3462    福山          47935  2003-05-15
   3 │ t3463    東広島        28654  2003-02-08
   4 │ t3464    呉            83152  2003-10-09
   5 │ t3465    尾道          42791  2003-08-04
   6 │ t3466    竹原          35687  2003-01-21
   7 │ t3467    三次          81296  2003-07-23
   8 │ t3468    大竹          21764  2003-10-26
   9 │ t3469    府中          75423  2003-12-15
5×4 DataFrame
 Row │ id       name     population  date_mod   
     │ String?  String?  Int32?      Date       
─────┼──────────────────────────────────────────
   1 │ t3461    広島          25176  2003-09-24
   2 │ t3462    福山          47935  2003-05-15
   3 │ t3463    東広島        28654  2003-02-08
   4 │ t3464    呉            83152  2003-10-09
   5 │ t3465    尾道          42791  2003-08-04
0
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
0
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?