Python sqlalchemy.types 模块,NVARCHAR 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用sqlalchemy.types.NVARCHAR

项目:stock    作者:pythonstock    | 项目源码 | 文件源码
def insert_db(data, table_name, write_index, primary_keys):
    # ??engine
    engine_mysql = engine()
    # ?? http://docs.sqlalchemy.org/en/latest/core/reflection.html
    # ????????????????
    insp = inspect(engine_mysql)
    col_name_list = data.columns.tolist()
    # ????????????varchar???
    if write_index:
        # ?????????
        col_name_list.insert(0, data.index.name)
    print(col_name_list)
    data.to_sql(name=table_name, con=engine_mysql, schema=MYSQL_DB, if_exists='append',
                dtype={col_name: NVARCHAR(length=255) for col_name in col_name_list}, index=write_index)
    # ????????
    if insp.get_primary_keys(table_name) == []:
        with engine_mysql.connect() as con:
            # ??????????
            try:
                con.execute('ALTER TABLE `%s` ADD PRIMARY KEY (%s);' % (table_name, primary_keys))
            except  Exception as e:
                print("################## ADD PRIMARY KEY ERROR :", e)


# ?????