import MySQLdb, sys, configparser, time
from main_config import main_Config

def re_pre(project):
#    config=configparser.ConfigParser()
#    config.read('main.ini')
#    mysql_id=config['settings']["id"]
#    mysql_pass=config['settings']["pass"]
    mysql_id, mysql_pass, projects, root_path, using_os = main_Config()
    time.sleep(0.1)

    count=count_table(project, mysql_id, mysql_pass)

    if count > 0:
        truncate_table(project, mysql_id, mysql_pass)
    else:
        pass

def truncate_table(project, mysql_id, mysql_pass):
    try:
        conn=MySQLdb.connect(db=project, user=mysql_id, passwd=mysql_pass, charset="utf8mb4")
        cursor=conn.cursor()

        sql="truncate table text_table"
        cursor.execute(sql)

        conn.commit()
        cursor.close()
        conn.close()

    except Exception as e:
        print(e)

def count_table(project, mysql_id, mysql_pass):
    try:
        conn=MySQLdb.connect(db=project, user=mysql_id, passwd=mysql_pass, charset="utf8mb4")
        cursor=conn.cursor()

        sql="select count(*) from text_table"
        cursor.execute(sql)
        count = cursor.fetchone()
        count = count[0]

        conn.commit()
        cursor.close()
        conn.close()

        return count

    except Exception as e:
        print(e)


if __name__=="__main__":
    project = "only_text"
    re_pre(project)
