언어/python

python + mysql

파아랑새 2017. 12. 28. 17:54
import pymysql.cursors

def mysql_value_setting(cnt_dict, md5_hash):
try:
connection = pymysql.connect(host = "localhost",
user = "root",
password = '1234',
#charset = 'utf8mb4',
db = 'hash_value')

except:
print ("mysql connection error ... !!!")

else:
# case 1) 정상 파일
if cnt_dict['f_cnt'] >= 15:
try:
with connection.cursor() as cursor:
sqlQuery = """INSERT INTO `normalH` VALUES (%s)"""
cursor.execute(sqlQuery,(md5_hash))
connection.commit()
except:
print ("file insert error ... !!!")

else:
print ("normalH insert success")

else:
try:
with connection.cursor() as cursor:
sqlQuery = """INSERT INTO `adnormalH` VALUES (%s)"""
cursor.execute(sqlQuery,(md5_hash))
connection.commit()
except:
print ("file insert error ... !!!")

else:
print ("adnormalH insert success")

connection.close()


def mysql_data_printf(table_choice):
try:
connection = pymysql.connect(host = "localhost",
user = "root",
password = '1234',
#charset = 'utf8mb4',
db = 'hash_value')

except:
print ("mysql connection error ... !!!")

else:
with connection.cursor() as cursor:
sqlQuery = "SELECT `*` from" +"`"+table_choice+"`"
cursor.execute(sqlQuery)
data = cursor.fetchall()
for rw in data:
print (rw)

connection.close()


mysql_data_printf('adnormalh')