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?

More than 1 year has passed since last update.

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

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

3つのプログラムが提示されましたが、どれも存在しないと思われるパッケージを使っていました。

一番、正解に近いプログラムを修正しました。

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

groq.jl
#! /snap/bin/julia

using DataFrames
# using PostgreSQL
using LibPQ

# PostgreSQL 接続情報
host = "localhost"
port = 5432
username = "scott"
password = "tiger123"
database = "city"

# 接続
# conn = PostgreSQL.connect(host, port, username, password, database)
conn = LibPQ.Connection("postgresql://scott:tiger123@localhost/city")

# テーブル名
table_name = "cities"

# Dataframe に読み込む
# df = DataFrame(PostgreSQL.query(conn, "SELECT * FROM $table_name"))
df = DataFrame(LibPQ.execute(conn, "SELECT * FROM $table_name"))

println(df)

実行結果

$ ./groq.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
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?