Written by
in
ในการเชื่อมต่อกับ PostgreSQL จาก Python นั้น จะต้องใช้ Package ‘psycopg2’
คำสั่งในการติดตั้งคือ
pip install psycopg2
วิธีการเรียกใช้จาก python
import numpy as np import pandas as pd import matplotlib.pyplot as plt import psycopg2 from IPython.display import display, HTML # used to print out pretty pandas dataframes import matplotlib.dates as dates import matplotlib.lines as mlines %matplotlib inline plt.style.use('ggplot') # specify user/password/where the database is sqluser = 'username' sqlpass = 'userpasswordgohere' dbname = 'dbname' schema_name = 'someschema' host = 'host.postgresql.server' query_schema = 'SET search_path to ' + schema_name + ';' # connect to the database con = psycopg2.connect(dbname=dbname, user=sqluser, password=sqlpass, host=host) query = query_schema + 'select * from sometable' df = pd.read_sql_query(query,con)
ก็จะได้ df เป็น Dataframe เอาไปใช้งานต่อได้
จบ
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Δ
Leave a Reply