相关疑难解决方法(0)

使用sqlalchemy将csv文件加载到数据库中

我正在尝试学习用Python编程.我想把csv文件放到数据库中.使用它是个好主意

python database sqlalchemy

27
推荐指数
4
解决办法
3万
查看次数

如何使用SQLAlchemy将批量条目提交到SQL数据库,并且有错误空间?

我可以使用SQLAlchemy将条目提交到具有容错容错的SQL数据库吗?将大批条目一起提交效率要高得多,但如果其中一个条目中存在错误,例如整数列中的文本,则整个批处理无法保存到数据库中.我下面的解决方法单独提交条目,但是这种方法可能会创建太多与mysql服务器的连接,特别是在并行运行时.是否有更有效的方式将条目作为批处理提交,但有错误的余地?

def commitentry(database, enginetext, verbose = False):
    """
    Takes a database object and text string that defines the SQL
    engine and adds all entries in the database list to the SQL
    database.
    """

    engine = create_engine(enginetext)
    Session = sessionmaker()
    Session.configure(bind=engine)
    session = Session()
    counter = 0
    for entry in database:
        try:
            session.add(entry)
            session.commit()

        except Exception, e:
            print("Commit Error")
            session.rollback()


            if verbose:
                print(e)

        finally:
            counter += 1
            if verbose:
                print(counter, counter/float(len(database)))

    if verbose:
        print("Entries saved!")
    session.close()
Run Code Online (Sandbox Code Playgroud)

python mysql sql sqlalchemy

2
推荐指数
1
解决办法
643
查看次数

标签 统计

python ×2

sqlalchemy ×2

database ×1

mysql ×1

sql ×1