# -*- coding:utf-8 -*-
import pymysql
import sys
__all__ = ['MySQL']
class MySQL(object):
'''
MySQL
'''
conn = ''
cursor = ''
def __init__(self, host='localhost', user='root', passwd='root', db='weibo_history', charset='utf8'):
"""MySQL Database initialization """
try:
self.conn = pymysql.connect(host, user, passwd, db)
except Exception:
sys.exit()
self.cursor = self.conn.cursor()
def query(self, sql):
""" Execute SQL statement """
return self.cursor.execute(sql)
def commit(self):
""" Return the results after executing SQL statement """
return self.conn.commit()
def show(self):
""" Return the results after executing SQL statement """
return self.cursor.fetchall()
def __del__(self):
""" Terminate the connection """
self.conn.close()
self.cursor.close()
# test
if __name__ == '__main__':
mysql = MySQL()
mysql.query("select user_id from user where has_relation = '0' limit 1")
result = mysql.show()
for row in result:
userid = row[0]
print(userid[0])
sql = "UPDATE user SET has_relation = '1' WHERE user_id = '%s'" %userid
print(sql)
mysql.query(sql)