#首先這個代碼沒有提取不夠簡練
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pymysql
import datetime
import time
import re,string
host = "19.206.25.24"
mysql_user_name = "root"
now = datetime.datetime.now()
now_timestamp = int(time.time())
def update_create_time_type():
row_list = find_create_time_row()
db = pymysql.connect(host=host, user=mysql_user_name, password="123456789", database="waresic")
cursor = db.cursor()
for row in row_list:
msg = str(row)
msg = msg.replace(',', '')
newmsg = msg.replace('\'', '')
newmsg = newmsg[1:]
newmsg=newmsg.strip(')')
print (newmsg+'\n')
cursor.execute(newmsg)
db.commit()
db.close()
return
# `create_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '創建時間',
# `update_at` COMMENT '更新時間',
def find_create_time_row():
sql = "select CONCAT('ALTER TABLE ',TABLE_NAME,' MODIFY ', COLUMN_NAME, ' datetime(3) NOT NULL DEFAULT current_timestamp(3);') from information_schema.COLUMNS where TABLE_SCHEMA = 'waresic' and COLUMN_NAME = 'create_time';"
db = pymysql.connect(host=host, user=mysql_user_name, password="123456789", database="information_schema")
cursor = db.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
db.commit()
db.close()
return rows
def update_time_exe():
zone_list = find_update_time_rows()
db = pymysql.connect(host=host, user=mysql_user_name, password="123456789", database="waresic")
cursor = db.cursor()
for row in zone_list:
msg = str(row)
msg = msg.replace(',', '')
newmsg = msg.replace('\'', '')
newmsg = newmsg[1:]
newmsg=newmsg.strip(')')
print (newmsg+'\n')
cursor.execute(newmsg)
db.commit()
db.close()
return
def find_update_time_rows():
sql = "select CONCAT('ALTER TABLE ',TABLE_NAME,' MODIFY ', COLUMN_NAME, ' datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) ;') from information_schema.COLUMNS where TABLE_SCHEMA = 'waresic' and COLUMN_NAME = 'update_time';"
db = pymysql.connect(host=host, user=mysql_user_name, password="123456789", database="information_schema")
cursor = db.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
db.commit()
db.close()
return rows
def main():
update_time_exe()
update_create_time_type()
return
if __name__ == '__main__':
main()
一分鐘發個博