一寸大海 劫長之井
PostgreSQL. SQL 파일 읽어 오기. 본문
PostgreSQL. SQL 파일 읽어 오기.
1. load sqlfile.sql 파일 읽어오기.
def create_tables():
""" create table if not exist """
conn = psycopg2.connect("dbname=" + dbname)
c = conn.cursor()
str = ""
for line in open('sqlfile.sql'):
if not line.startswith("--"):
str += line
c.execute(str)
conn.commit()
conn.close()
2. sql 파일 내에서 데이타베이스 생성 하고 접속하기.
DROP DATABASE IF EXISTS dbname;
CREATE DATABASE dbname;
\c
dbname
2-1. PostgreSql 에서 데이타베이스생성을 위하여 별도 데이타베이스 없이 접속 하기.
conn = psycopg2.connect("dbname=postgres");
postgreSQL 기본 데이타 베이스인 postres 로 접속 하여 처리 하면 됩니다.
Comments