전체 글
-
pymysql 로 data insert카테고리 없음 2022. 11. 30. 14:38
#pandas 비사용 def csv_to_db(self, config, col_names): openpath = config.readpath if '\\' in openpath: openpath = openpath.replace('\\', '/') file = open(openpath, 'r') csvReader = csv.reader(file) cursor = self.conn.cursor() col_nms = col_names print('csvReader : ',csvReader) for row in csvReader: name = (row[0]) email = (row[1]) dept = (row[2]) salary = (row[3]) sql = """INSERT INTO employee (Nam..
-
[pandas]csv파일을 db로 옮기기 : 만약 csv의 header이름과 갯수가 db컬럼과 다를경우카테고리 없음 2022. 11. 30. 14:34
요구사항: 1. csv의 내용을 읽는다 2. csv의 헤더랑 column name이랑 같은지 확인하고, 같은 것 중에 rename이 있다면 그걸로 바꿔라. 3. 변경된 컬럼명을 포함해서 db에 insert하는데 4. 만약, db에 없는 컬럼이 있다면 있는 컬럼만 넣어라 >> if db컬럼명 == df 컬럼명: insert 실행 import pymysql import csv import pandas as pd from sqlalchemy import create_engine #pandas 사용 def csv_df_db(self, config, savepath, o_names, n_names): conn = self.conn cursor = conn.cursor() db_connection = self.db..
-
[파이썬, pandas]sql 테이블 컬럼명 추출해서 리스트로 담기카테고리 없음 2022. 11. 30. 10:59
conn = self.conn cursor = conn.cursor() #db컬럼명만 추출하는 쿼리 sql = """select column_name from information_schema.columns where table_name = 'employee'""" cursor.execute(sql) df = pd.read_sql_query(sql, conn) cname = df.values.tolist() https://wotres.tistory.com/entry/sql-%EB%AC%B8-%ED%85%8C%EC%9D%B4%EB%B8%94%EC%9D%98-%EC%A0%84%EC%B2%B4-%EC%B9%BC%EB%9F%BC-%EC%9D%B4%EB%A6%84-%EC%A1%B0%ED%9A%8C-%ED%95%9..
-
pandas 가로 세로 변경 (행/열 바꾸기), table 정보조회카테고리 없음 2022. 11. 29. 16:45
https://hermeslog.tistory.com/358 [MySQL] 테이블 및 컬럼, 코멘트 조회 MySQL comment 설정 및 조회 Table 정보조회 (with comment) 명령어를 통한 테이블정보 조회 SHOW TABLE STATUS; Query를 통한 테이블정보 조회 SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='database_name'; SELECT TABLE_ hermeslog.tistory.com https://mizykk.tistory.com/132 [Python] 데이터프레임 행/열 바꾸기 :: transpose, T 데이터프레임의 행과 열을 바꾸기위해 df.transpose나 df.T를 사용하면 된다. # 행/..
-
pymysql.err.OperationalError) (1054, "Unknown column : 컬럼명 변경을 통해 해결하기카테고리 없음 2022. 11. 29. 16:00
def csv_df_db(self, config, savepath, o_name, n_name): openpath = config.readpath file = open(openpath, 'r') df = pd.read_csv(file) db_connection = self.dbconnect() try: df.to_sql(name=savepath, con=db_connection, if_exists='append', index=False) except Exception as e: df.rename(columns={o_name: n_name}, inplace=True) df.to_sql(name=savepath, con=db_connection, if_exists='append', index=False) 이..
-
[pymysql] Database 연동, csv 파일을 DB로 넣고 빼기 & 로그찍기2카테고리 없음 2022. 11. 29. 11:17
미션 : config로 불러온 컬럼명이랑 db 컬럼명이랑 불일치할 때 하는 조치 import logging import pymysql import csv import pandas as pd class MysqlDB(): def __init__(self): self.conn = 0 self.cursor = 0 self.conn = 0 def dbconnect(self): # conn = pymysql.connect(host=connect_info['host'], port=3306, user='root', password='password', db='emp', charset='utf8') self.conn = pymysql.connect(host='localhost', port=3306, user='root..