DB : connect - helloMinji/chatbot_spotify GitHub Wiki
- ํ์ํ ํจํค์ง ๋ถ๋ฌ์ค๊ธฐ
import pymysql
- DB ์ฐ๊ฒฐ์ ์ํ ์ธํ
: AWS - RDS์์ ํด๋น ๊ฐ ํ์ธ
host =
port =
username = # AWS RDS ์์ฑ ์ ์ค์ ํ ๊ฐ
database = # AWS RDS ์์ฑ ์ ์ค์ ํ ๊ฐ (db๋ช
)
password = # AWS RDS ์์ฑ ์ ์ค์ ํ ๊ฐ
Connect DB
def main():
try:
db connect
conn = pymysql.connect(host, user=username, passwd=password, db=database, port=port, use_unicode=True, charset='utf8')
:bulb: host โ host=host : ์ค๋ฅ๊ฐ ์๊ฒจ์ ์ด๋ ๊ฒ ๋ณ๊ฒฝํ๋ ์ฑ๊ณต!
์ฐ๊ฒฐ๋ db์์ ์ํธ์์ฉ์ ์ํด cursor setting
cursor = conn.cursor()
when db connect failed
except:
logging.error("could not connect to rds")
sys.exit(1)
cursor๊ฐ ์์ด์ผ query๋ฌธ ์คํ์ด ๊ฐ๋ฅํ ๊ฒ!
cursor.execute("SHOW TABLES")
print(cursor.fetchall())
print("success")
### ๊ธฐ์กด ์ฝ๋ ( header ~~~ )
cursor method
- execute : ์คํ
- fetchall : ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํ๊บผ๋ฒ์ ๊ฐ์ ธ์ฌ ๋
- fetchone : ํ ๋ฒ ํธ์ถ์ ํ๋์ ํ๋ง ๊ฐ์ ธ์ฌ ๋
- fetchmany(n) : n๊ฐ๋งํผ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๋