Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- airflow
- FastAPI
- celery
- datetime #zip
- requesthead
- 리눅스 # 기초
- beautifulsoup
- aiflow
- HeidiSQL
- 가상환경 초기세팅
- etl
- pickle #datetime
- mariadb설치 #mysql설치
- 파일저장
- enumerate #함수 # def
- 자동화
- 빗썸api
- Docker
- 모델서빙
- Google Cloud Storage
- ssh operator
- requests
- text.children
- with open
- JavaScripts
- K-ICT
- 정규표현식
- cron
- 자연어처리 환경 컨테이너
- 원하는 태그 찾기
Archives
- Today
- Total
오음
sqlDB와 파이썬 연동 pymysql 본문
pymysql 설치하기
pip install pymysql
파이썬과 연동하기
가상머신위에 mysql설치
2023.03.07 - [데이터 엔지니어링/가상환경] - 가상환경 MySQL 설치하기
가상환경 MySQL 설치하기
가상환경 내에서 필요한 DB 설치하기 # mariadb 설치 : CentOS7부터 MariaDB로 제공 yum install mariadb-server -y # 시작 demon 등록 ※ 데몬이란? 리눅스 시스템이 처음 가동될 때 실행되는 백그라운드 프로세스
oh-um.tistory.com
connection 에 cursor를 생성해준다.
try :
con = pymysql.connect(host='호스트ip', user='사용자',password='비밀번호',charset='utf8',db = '테이블')
cur = con.cursor()
except Exception as e:
print("error->",e)
heidiSQL로 잘 연결 되었는지 확인!
cur.execute로 database, table 생성해보기
cur.execute("create database play")
cur.execute("use play
cur.execute("""CREATE TABLE `class3` (
`InvoiceNo` VARCHAR(30),
`StockCode` VARCHAR(30),
`Description` VARCHAR(255),
`Quantity` INT(3),
`UnitPrice` FLOAT(5,2),
`CustomerID` VARCHAR(30),
`Country` VARCHAR(45),
`InvoiceDate` DATE
)""")
dataset3파일을 db에 넣기
con.commit() 해주기!
# value값 설정
sql = "INSERT INTO class3 VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"
# sql형태로 데이터 DB에 저장하기
with open("./data/dataset3.csv","r",encoding='utf-8') as f:
for idx,line in enumerate(f):
if idx > 0:
cur.execute(sql,[x.replace('"',"") for x in line.split(";")])
# commit 해주기
con.commit()
참고
* sql명령문 확인 사이트
https://www.w3schools.com/mysql/mysql_create_table.asp
MySQL CREATE TABLE Statement
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'데이터 엔지니어링 > DB' 카테고리의 다른 글
데이터 모델링 (0) | 2023.10.12 |
---|---|
DB에 저장,가져오기 (0) | 2023.03.13 |