1) Jupyter Notebook 실행
2) 실행위치
3) MySQL
4) 데이터 삭제
mysqldump --set-gtid-purged=OFF -h "엔트포인트" -P 3306 -u admin -p zerobase police_station > backup_police.sql
use zerobase;
show tables;
delete from police_station;
show tables;
select * from police_station;
5) python.ipynb 파일 생성
pip install mysql-connector-python
import mysql.connector
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>"
)
<Create Connection 예제 1>
import mysql.connector
local = mysql.connector.connect(
host = "localhost",
user = "root",
password = "*******"
)
<Create Connection 예제 2>
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306
user = "admin",
password = "*******"
)
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>"
)
mydb.close()
<Close Database 예제 1>
local.close()
remote.close()
mydb = mysql.connector.connect(
host = "<hostname>",
port = <port>,
user = "<username>",
password = "<password>",
database = "<databasename>"
)
<Connect to Database 예제 1>
local = mysql.connector.connect(
host = "localhost",
user = "root",
password = "*******",
database = "zerobase"
)
<Connect to Database 예제 2>
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "admin",
password = "*******",
database = "zerobase"
)
local.close()
remote.close()
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
database = "<databasename>"
)
mycursor = mydb.cursor()
mycursor.execute(<query>);
<Execute SQL 예제 1>
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "admin",
password = "*******",
database = "zerobase"
)
cur = remote.cursor()
cur.execute("create table sql_file (id int, filename varchar(16))")
remote.close()
desc sql_file;
<Execute SQL 예제 2>
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "admin",
password = "*******",
database = "zerobase"
cur = remote.cursor()
cur.execute("drop table sql_file")
remote.close()
)
desc sql_file;
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
database = "<databasename>"
)
mycursor = mydb.cursor()
sql = open("<filename>.sql").read()
mycursor.execute(sql)
<Execute SQL File 1 예제>
create table sql_file
(
id int,
filename varchar(16)
)
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "admin",
password = "*******",
database = "zerobase"
)
cur = remote.cursor()
sql = open("test03.sql").read()
cur.execute(sql)
remote.close()
desc sql_file;
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
database = "<databasename>"
)
mycursor = mydb.cursor()
sql = open("<filename>.sql").read()
result = mycursor.execute(sql, multi = True)
<Execute SQL File 2 예제>
insert into sql_file values(1, "test01.sql");
insert into sql_file values(2, "test02.sql");
insert into sql_file values(3, "test03.sql");
insert into sql_file values(4, "test04.sql");
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "admin",
password = "*******",
database = "zerobase"
)
cur = remote.cursor()
sql = open("test04.sql").read()
for result_iterator in cur.execute(sql, multi = True):
if result_iterator.with_rows:
print(result_iterator.fetchall())
else:
print(result_iterator.statement)
remote.commit()
remote.close()
desc sql_file;
select * from sql_file;
mycursor.execute(<query>)
result = mycursor.fetchall()
for data in result:
print(data)
<Fetch All 예제>
remote = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "admin",
password = "*******",
database = "zerobase"
)
cur = remote.cursor(buffered = True)
cur.execute("select * from sql_file")
result = cur.fetchall()
for result_iterator in result:
print(result_iterator)
remote.close()
import pandas as pd
df = pd.DataFrame(result)
df.head()
import pandas as pd
df = pd.read_csv("police_station.csv")
df.head()
import mysql.connector
conn = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "zero",
password = "*******",
database = "zerobase"
)
cursor = conn.cursor(buffered = True)
sql = "insert into police_station values (%s, %s)"
for i, row in df.iterrows():
cursor.execute(sql, tuple(row))
print(tuple(row))
conn.commit()
cursor.execute("select * from police_station")
result = cursor.fetchall()
for row in result:
print(row)
df = pd.read_csv('2020_crime.csv', encoding = 'euc-kr')
df.head()
1) AWS RDS(database-1) zerobase에 접속
import mysql.connector
conn = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "zero",
password = "*******",
database = "zerobase"
)
2) 2020_crime.csv 데이터(encoding='euc-kr') 읽어오기
import pandas as pd
df = pd.read_csv('2020_crime.csv', encoding = 'euc-kr')
df.head()
3) INSERT 쿼리 작성
sql = """insert into crime_status values ("2020", %s, %s, %s, %s)"""
cursor = conn.cursor(buffered = True)
4) 데이터를 crime_status 테이블에 INSERT
for i, row in df.iterrows():
cursor.execute(sql, tuple(row))
print(tuple(row))
conn.commit()
5) crime_status 테이블의 데이터 조회
cursor.execute("select * from crime_status")
result = cursor.fetchall()
for row in result:
print(row)
6) 조회한 결과를 Pandas 로 변환해서 확인
df = pd.DataFrame(result)
df.head()
1) AWS RDS(database-1) zerobase에 접속
import mysql.connector
conn = mysql.connector.connect(
host = "엔드포인트",
port = 3306,
user = "zero",
password = "*******",
database = "zerobase"
)
2) cctv 파일을 Pandas로 읽어오기
import pandas as pd
df = pd.read_csv('Seoul_CCTV.csv', encoding = 'utf-8')
df.head(2)
desc cctv;
3) cctv Table 생성
sql = "create table cctv (기관명 varchar(8), 소계 int, 2013년도이전 int, 2014년 int, 2015년 int, 2016년 int)"
cursor = conn.cursor(buffered = True)
cursor.execute(sql)
4) 데이터를 cctv 테이블에 INSERT
sql = "insert into cctv values (%s, %s, %s, %s, %s, %s)"
cursor = conn.cursor(buffered = True)
for i, row in df.iterrows():
cursor.execute(sql, tuple(row))
print(tuple(row))
conn.commit()
5) cctv 테이블의 데이터를 조회하여 확인
cursor.execute("select * from cctv")
result = cursor.fetchall()
for row in result:
print(row)
6) 조회된 데이터를 Pandas로 변환하여 출력
df = pd.DataFrame(result)
df.head()