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 |
Tags
- pandas
- wcss
- elbow method
- DataFrame
- sklearn
- data
- Machine Learning
- 덴드로그램
- dendrogram
- DataAccess
- insert()
- hierarchical_clustering
- Python
- 분류 결과표
- del
- list
- 최댓값
- numpy
- Dictionary
- matplotlib
- append()
- function
- 반복문
- IN
- len()
- count()
- nan
- 최솟값
- string
- analizer
Archives
- Today
- Total
개발공부
Python MySQL Connector 를 이용해 Insert 하는 방법 본문
import mysql.connector
def get_connection() :
connection = mysql.connector.connect(
host = '호스트이름',
database = 'DB이름',
user = 'USER명',
password = '비밀번호',
)
return connection
try :
# 데이터 insert
# 1. DB에 연결
connection = get_connection()
# 2. 쿼리문 만들기
query = '''insert into mysql_table
(col1, col2, ...)
values
(value1, value2, ...);'''
# 3. 커서를 가져온다.
cursor = connection.cursor()
# 4. 쿼리문을 커서를 이용해서 실행한다.
cursor.execute(query)
# 5. 커넥션을 커밋해줘야 한다 => 디비에 영구적으로 반영하라는 뜻
connection.commit()
# 6. 자원 해제
cursor.close()
connection.close()
except mysql.connector.Error as e :
cursor.close()
connection.close()
print(e)'Python > Flask' 카테고리의 다른 글
| Python Flask에서 Resource 클래스를 이용한 API 서버 개발 방법 (0) | 2022.06.17 |
|---|---|
| Python MySQL Connector 를 이용해 Delete 하는 방법 (0) | 2022.06.17 |
| Python MySQL Connector 를 이용해 Update 하는 방법 (0) | 2022.06.17 |
| Python MySQL Connector 를 이용해 Select 하는 방법 (0) | 2022.06.17 |
| Restful API (URL, Methods, Message) (0) | 2022.06.17 |