コード例 #1
0
ファイル: GetDataToMysql.py プロジェクト: sufezl/QFactorModel
class GetDataToMysql:
    def __init__(self):
        self.conn = MysqlCon().getMysqlCon(flag='connect')
        self.logger = mylog.logger

    def GetMain(self, dataDf, tableName):
        # 插入数据语句
        tableList = dataDf.columns.tolist()
        strFormat = '%s,' * len(tableList)
        sqlStr = "replace into %s(%s)" % (
            tableName, ','.join(tableList)) + "VALUES(%s)" % strFormat[:-1]

        # dataDf.fillna('None',inplace=True)
        dataDf = dataDf.astype(object).where(pd.notnull(dataDf), None)
        # dataDf.where(dataDf.notnull(), None)
        # dataDf.where(dataDf.notnull(), None)
        cursor = self.conn.cursor()

        try:
            for r in range(0, len(dataDf)):
                values = tuple(dataDf.iloc[r][tableList].tolist())
                cursor.execute(sqlStr, values)
        except:
            self.logger.error("插入数据到数据库错误,请检查!")

        cursor.close()
        self.conn.commit()
コード例 #2
0
class GetDataToMysql:
    def __init__(self):
        self.conn = MysqlCon().getMysqlCon(flag='connect')
        self.logger = mylog.set_log()

    def GetMain(
        self,
        dataDf,
        tableName,
    ):
        # 插入数据语句
        tableList = dataDf.columns.tolist()
        strFormat = '%s,' * len(tableList)
        sqlStr = "replace into %s(%s)" % (
            tableName, ','.join(tableList)) + "VALUES(%s)" % strFormat[:-1]

        dataDf = dataDf.astype(object).where(pd.notnull(dataDf), None)
        cursor = self.conn.cursor()

        for r in range(0, len(dataDf)):
            values = tuple(dataDf.iloc[r][tableList].tolist())
            cursor.execute(sqlStr, values)

        cursor.close()
        self.conn.commit()
        self.logger.info("数据存入mysql成功!")