Python으로 MySQL 접속 후 사용하는 방법
pip install mysql-connector-python
import mysql.connector
# MySQL 연결
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
)
# MySQL 연결
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "********",
)
# AWS RDS 연결
mydb = mysql.connector.connect(
host = "database-1.ca5oct2z6zq8.us-east-2.rds.amazonaws.com",
port = 3306,
user = "admin",
password = "********",
)
close()
구문 사용mydb.close()
# MySQL 연결
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
database = "<database>"
)
import mysql.connector
# MySQL 연결
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "********",
database = "zerobase"
)
# MySQL 연결
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
database = "<database>"
)
mycursor = mydb.cursor()
mycursor.execute(<query>)
cur = mydb.cursor()
cur.execute("CREATE TABLE sql_file (id int, filename varchar(16))")
cur = mydb.cursor()
cur.execute("DROP TABLE sql_file")
# MySQL 연결
mydb = mysql.connector.connect(
host = "<hostname>",
user = "<username>",
password = "<password>",
database = "<database>"
)
mycursor.mydb.cursor()
sql = open("<filename>.sql").read()
mycursor.execute(sql)
# MySQL 연결
remote = mysql.connector.connect(
host = "localhost",
user = "root",
password = "********",
database = "zerobase"
)
cur = remote.cursor()
sql = open('test03.sql').read()
cur.execute(sql)
remote.close()
# MySQL 연결
remote = mysql.connector.connect(
host = "localhost",
user = "root",
password = "********",
database = "zerobase"
)
cur = remote.cursor()
sql = open('test03.sql').read()
cur.execute(sql, multi = True)
remote.close()
INSERT INTO sql_file VALUES (1, 'test01.sql');
INSERT INTO sql_file VALUES (1, 'test02.sql');
INSERT INTO sql_file VALUES (1, 'test03.sql');
INSERT INTO sql_file VALUES (1, 'test04.sql');
# MySQL 연결
remote = mysql.connector.connect(
host = "localhost",
user = "root",
password = "********",
database = "zerobase"
)
cur = remote.cursor()
sql = open('test04.sql').read()
#cur.execute(sql, multi = True)
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()
mycursor.execute(<query>)
result = mycursor.fetchall()
for data in result:
print(data)
remote = mysql.connector.connect(
host = 'localhost',
port = 3306,
user = 'root',
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()
remote = mysql.connector.connect(
host = 'localhost',
user = 'root',
password = '********',
database = 'zerobase'
)
cur = remote.cursor(buffered = True)
cur.execute('SELECT * FROM celeb')
result = cur.fetchall()
for result_iterator in result:
print(result_iterator)
remote.close()