import pymysql
db_conn = pymysql.connect(
host='localhost',
port=3306,
user='jewon119',
passwd='내 비밀번호',
db='blog_db',
charset='utf8'
)
import pymysql
db_conn = pymysql.connect(
host='localhost',
port=3306,
user='jewon119',
passwd='내 비밀번호',
db='blog_db',
charset='utf8'
)
db = db_conn.cursor()
sql = 'SHOW TABLES;'
db.execute(sql)
sql = """
CREATE TABLE user_info(
USER_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
USER_EMAIL VARCHAR(100) NOT NULL,
BLOG_ID CHAR(4),
PRIMARY KEY(USER_ID)
);
"""
db.execute(sql)
db_conn.commit()
sql = 'DROP TABLE user_info;'
db.execute(sql)
db_conn.commit()
user_email = 'test@test.com'
blog_id = 'A'
sql = "INSERT INTO user_info (USER_EMAIL, BLOG_ID) VALUES ('%s', '%s')" % (str(user_email), str(blog_id))
db.execute(sql)
db_conn.commit()
sql = "SELECT * FROM user_info"
db.execute(sql)
results = db.fetchall()
for result in results:
print(result, type(result))
- DB 연결 해제 : 🔍 db_conn.close()
- DB 및 Table 연결
- connection이 잘 잡혀있지 않아 오류가 발생하면 아래 2개로 해결 시도
- 🔍 connection.admin.command('ismaster')
- 🔍 connection.server_info() 👈 위에 명령 시도해도 해결안될 때 체크
import pymongo
username = ''
password = ''
ip_address = 'localhost'
connection = pymongo.MongoClient()
connection = pymongo.MongoClient('mongodb://%s' % (ip_address))
blog_session_db = connection.blog_session_db
blog_ab = blog_session_db.blog_ab
blog_ab.insert_one({'email':'def@def.com'})
blog_ab.find_one({'email':'def@def.com'})
results = blog_ab.find()
for result in results:
print(result)