๐งท ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
pip install mysql-connector-python
import mysql.connector
๐งท MySQL ์ ์ ์ฝ๋
- Local Database ์ฐ๊ฒฐ
local = mysql.connector.Connect( host = 'hostname', user = 'username', password = 'password' # ํน์ Database ์ ์ ์ํ๊ธฐ ์ํ ์ฝ๋ , database = 'dbname' ) local.close()
- AWS RDS (database-1) ์ฐ๊ฒฐ
remote = mysql.connector.Connect( host = 'hostname', port = <port> , user = 'username', password = 'password' # ํน์ Database ์ ์ ์ํ๊ธฐ ์ํ ์ฝ๋ , database = 'dbname' ) remote.close()
๐ ์ฐ๊ฒฐ ์ ํน์ database์ ์ ์ํ๊ณ ์ ํ๋ค๋ฉด,
database = ์ต์ ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
๐ ์ฌ์ฉ ํ์๋ ๊ผญ close()๋ก ๋ซ์์ฃผ๋๋ก ํ๋ค.
local = mysql.connector.Connect(
host = 'localhost',
user = 'root',
password = 'pw'
# ํน์ Database ์ ์ ์ํ๊ธฐ ์ํ ์ฝ๋
, database = 'zerobase'
)
local.close()
remote = mysql.connector.Connect(
host = 'database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com',
port = 3306,
user = 'admin',
password = 'pw'
# ํน์ Database ์ ์ ์ํ๊ธฐ ์ํ ์ฝ๋
, database = 'zerobase'
)
remote.close()
๐งท
- ์ ์์ฝ๋ ๋ณ์์ cursor()๋ฅผ ์ฐ๊ฒฐํด์ฃผ๊ณ ,
execute()์ ๊ดํธ์์ ์ฟผ๋ฆฌ๋ฌธ ์ฝ์
remote = mysql.connector.Connect(
host = 'database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com',
port = 3306,
user = 'admin',
password = '๋น๋ฒ์
๋ ฅ',
database = 'zerobase'
)
cur = remote.cursor()
cur.execute('CREATE TABLE sql_file (id int, filename varchar(16))')
remote.close()
# ์์ฑ์
mysql> use zerobase
Database changed
mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| celeb |
| crime_status |
| police_station |
| snl_show |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.15 sec)
#์์ฑํ
mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| celeb |
| crime_status |
| police_station |
| snl_show |
| sql_file |
| test1 |
| test2 |
+--------------------+
7 rows in set (0.15 sec)
mysql> desc sql_file;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| filename | varchar(16) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.15 sec)
remote = mysql.connector.Connect(
host = 'database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com',
port = 3306,
user = 'admin',
password = '๋น๋ฒ์
๋ ฅ',
database = 'zerobase'
)
cur = remote.cursor()
cur.execute('DROP TABLE sql_file')
remote.close()
mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| celeb |
| crime_status |
| police_station |
| snl_show |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.15 sec)
๐งท
- execute()์์ ์ฝ์ด์จ sqlํ์ผ๋ณ์ ๋ฃ๊ธฐ
TEST03.sql์ ํ
์ด๋ธ์์ฑ ์ฟผ๋ฆฌ ์์ฑ ํ ์ ์ฅ
์คํ์ฝ๋์์ฑ
remote = mysql.connector.connect(
host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com",
port = 3306,
user = 'admin',
password = 'pw',
database = 'zerobase'
)
cur = remote.cursor()
sql = open('TEST03.sql').read()
cur.execute(sql)
remote.close()
mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| celeb |
| crime_status |
| police_station |
| snl_show |
| sql_file |
| test1 |
| test2 |
+--------------------+
7 rows in set (0.15 sec)
mysql> desc sql_file;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| filename | varchar(16) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.16 sec)
remote = mysql.connector.connect(
host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com",
port = 3306,
user = 'admin',
password = 'pw',
database = 'zerobase'
)
cur = remote.cursor()
sql = open('TEST04.sql').read()
for i in cur.execute(sql,multi=True):
if i.with_rows:
print(i.fetchall())
else :
print(i.statement)
remote.commit()
remote.close()
โ error | Commands out of sync; you can't run this command now
remote = mysql.connector.connect( host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com", port = 3306, user = 'admin', password = 'pw', database = 'zerobase' ) cur = remote.cursor() sql = open('TEST04.sql').read() cur.execute(sql, multi=True) remote.commit() remote.close()
for๋ฌธ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์์ฐฝ์ ์ฐ๊ธฐ์ํด ์ฝ๋ฉํ์ค์๊ณ
์์๊ฐ์ด ์ฝ๋๋ฅผ ์ง์ ์คํํด๋ณด์๋๋ฐ, error๊ฐ ๋ด๋ค.
์ด ์ค๋ฅ๋ ์ด์ ์ฟผ๋ฆฌ์ ๊ฒฐ๊ณผ ์งํฉ์ ๋ชจ๋ ์๋ชจํ์ง ์์ ์ํ์์ ๋ค์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๋ ค๊ณ ํ ๋ ๋ฐ์ํ๋ค๊ณ ํ๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก for๋ฌธ๊น์ง ๋ชจ๋ ์คํ์ ํด์ผ๋ง ์ ์์ ์ผ๋ก ๋ค์ค์ฟผ๋ฆฌ๋ฅผ ์คํํ ์ ์๋ค.
mysql> select * from sql_file;
+------+------------+
| id | filename |
+------+------------+
| 1 | test01.sql |
| 2 | test02.sql |
| 3 | test03.sql |
| 4 | test04.sql |
+------+------------+
4 rows in set (0.15 sec)
๐งท select ์ฟผ๋ฆฌ๋ฌธ์ฒ๋ผ ๊ฒฐ๊ณผ ๊ฐ์ด row๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฉด Fetch All ํด์ ์ถ๋ ฅ
(์ฝ์ด์ฌ ๋ฐ์ดํฐ ์์ด ๋ง์ ๊ฒฝ์ฐ buffered=True)
remote = mysql.connector.connect(
host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com",
port = 3306,
user = 'admin',
password = 'pw',
database = 'zerobase'
)
cur = remote.cursor(buffered=True)
cur.execute("select * from sql_file")
result = cur.fetchall()
for result_iteraotr in result :
print(result_iteraotr)
remote.close()
import pandas as pd
df = pd.DataFrame(result)
df
csv ํ์ผ ๋ถ๋ฌ์จ ํ ๋ฐ์ดํฐ ํ์ธ
AWS mysql ์ฐ๊ฒฐ
import mysql.connector
conn = mysql.connector.connect(
host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com",
port = 3306,
user = 'admin',
password = 'pw',
database = 'zerobase'
)
cursor = conn.cursor(buffered=True)
sql = "INSERT INTO police_station VALUE (%s,%s)"
for i, row in df.iterrows() :
cursor.execute(sql, tuple(row))
print(tuple(row))
conn.commit()
๐งท tuple ()
์ df value๊ฐ์ ํด๋นํ๋ ์ด ๋ฐ์ดํฐ๊ฐ ํํ๋ก ๋ฐํ
๐งท (%s,%s)
cursor.execute(sql, tuple(row)) ๋ฌธ์์ %s,%s ์ tuple(row)๊ฐ์ด ๋ค์ด๊ฐ
๋ฐ์ดํฐ ํ์ธ
๋ฐ์ดํฐํ๋ ์์ผ๋ก ํ์ธ
crime_status ํ ์ด๋ธ์ 2020_crime.csv ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์
2020_crime.csv ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
AWS RDS(database-1) zerobase ์ ์ ์
import mysql.connector
conn = mysql.connector.connect(
host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com",
port = 3306,
user = 'admin',
password = 'pw',
database = 'zerobase'
)
cursor = conn.cursor(buffered=True)
sql = "INSERT INTO crime_status VALUES ('2020',%s,%s,%s,%s)"
import mysql.connector
conn = mysql.connector.connect(
host = "database-1.cpc8kqyoiq1s.ap-southeast-2.rds.amazonaws.com",
port = 3306,
user = 'admin',
password = 'pw',
database = 'zerobase'
)
sql = "CREATE TABLE cctv (๊ธฐ๊ด๋ช
varchar(8), ์๊ณ int, 2013๋
๋์ด์ int, 2014๋
int, 2015๋
int, 2016๋
int)"
cursor = conn.cursor(buffered=True)
cursor.execute(sql)
cctv CSV ๋ฐ์ดํฐ๋ฅผ Pandas ๋ก ์ฝ์ด์ต๋๋ค
๋ฐ์ดํฐ๋ฅผ cctv ํ
์ด๋ธ์ INSERT ํฉ๋๋ค.