• <bdo id='Kgmko'></bdo><ul id='Kgmko'></ul>
    1. <legend id='Kgmko'><style id='Kgmko'><dir id='Kgmko'><q id='Kgmko'></q></dir></style></legend>

      <small id='Kgmko'></small><noframes id='Kgmko'>

      <i id='Kgmko'><tr id='Kgmko'><dt id='Kgmko'><q id='Kgmko'><span id='Kgmko'><b id='Kgmko'><form id='Kgmko'><ins id='Kgmko'></ins><ul id='Kgmko'></ul><sub id='Kgmko'></sub></form><legend id='Kgmko'></legend><bdo id='Kgmko'><pre id='Kgmko'><center id='Kgmko'></center></pre></bdo></b><th id='Kgmko'></th></span></q></dt></tr></i><div id='Kgmko'><tfoot id='Kgmko'></tfoot><dl id='Kgmko'><fieldset id='Kgmko'></fieldset></dl></div>

        <tfoot id='Kgmko'></tfoot>

        使用 to_sql 和 sqlalchemy 到 mariadb 数据库的 pandas 数据框

        pandas dataframe to mariadb database with to_sql and sqlalchemy(使用 to_sql 和 sqlalchemy 到 mariadb 数据库的 pandas 数据框)
          <bdo id='CDCrY'></bdo><ul id='CDCrY'></ul>
          <tfoot id='CDCrY'></tfoot>

            <tbody id='CDCrY'></tbody>

            <i id='CDCrY'><tr id='CDCrY'><dt id='CDCrY'><q id='CDCrY'><span id='CDCrY'><b id='CDCrY'><form id='CDCrY'><ins id='CDCrY'></ins><ul id='CDCrY'></ul><sub id='CDCrY'></sub></form><legend id='CDCrY'></legend><bdo id='CDCrY'><pre id='CDCrY'><center id='CDCrY'></center></pre></bdo></b><th id='CDCrY'></th></span></q></dt></tr></i><div id='CDCrY'><tfoot id='CDCrY'></tfoot><dl id='CDCrY'><fieldset id='CDCrY'></fieldset></dl></div>
              • <small id='CDCrY'></small><noframes id='CDCrY'>

                  <legend id='CDCrY'><style id='CDCrY'><dir id='CDCrY'><q id='CDCrY'></q></dir></style></legend>
                1. 本文介绍了使用 to_sql 和 sqlalchemy 到 mariadb 数据库的 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 pandas 函数 to_sql 将数据框写入 MariaDB 数据库.我在 PyCharm 中的 Python 代码如下所示:

                  I want to use the pandas function to_sql to write a dataframe into a MariaDB database. My Python code inside PyCharm looks as follows:

                  import pandas as pd
                  import mysql.connector
                  from sqlalchemy import create_engine
                  
                  myd = pd.read_csv('/[path]/[filename].csv')
                  
                  engine = create_engine('mysql+mysqlconnector://[user]:[pw]@127.0.0.1/[dbname]')
                  
                  myd.to_sql(name='[tablename]', con=engine, if_exists='replace', index=False)
                  

                  在控制台中执行最后一行时,出现以下错误:

                  When executing the last line in the console I get the following error:

                  Error on sql SELECT name FROM sqlite_master WHERE type='table' AND name='[tablename]'; 
                  Traceback (most recent call last):   
                     File "/usr/lib/python3.4/code.py", line 90, in runcode
                       exec(code, self.locals)   
                     File "<input>", line 1, in <module>   
                     File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 1261, in to_sql
                       self, name, con, flavor=flavor, if_exists=if_exists, **kwargs)   
                     File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 207, in write_frame
                       exists = table_exists(name, con, flavor)   
                     File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 275, in table_exists
                       return len(tquery(query, con)) > 0   
                     File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 90, in tquery
                       cur = execute(sql, con, cur=cur)   
                     File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 44, in execute
                       cur = con.cursor() 
                  AttributeError: 'Engine' object has no attribute 'cursor'
                  

                  这里有人有同样的情况有一点错误.然而,在有人解决问题之前它就消失了.你知道怎么回事吗?

                  Here someone had the same error at one point. However, it had disappeared before someone solved the problem. Do you know what is wrong?

                  推荐答案

                  仅从 pandas 0.14.0 开始支持传递 sqlalchemy 引擎

                  Passing sqlalchemy engines is only supported starting from pandas 0.14.0

                  要将 to_sql 与旧版 pandas 一起使用,您需要传递原始连接 (engine.raw_connection()) 和 flavor='mysql'to_sql:

                  To use to_sql with older pandas version, you need to pass the raw connection (engine.raw_connection()) and flavor='mysql' to to_sql:

                  myd.to_sql(name='[tablename]', con=engine.raw_connection(), flavor='mysql', if_exists='replace', index=False)
                  

                  但是,我建议升级您的 pandas 版本(不推荐使用传递原始连接,并且在较新的 pandas 版本中将不再支持,然后将仅支持 sqlalchemy 引擎/连接)

                  However, I recommend to upgrade your pandas version (passing raw connections is deprecated and will not be supported anymore in newer pandas versions, then only sqlalchemy engines/connections will be supported)

                  这篇关于使用 to_sql 和 sqlalchemy 到 mariadb 数据库的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  <i id='iWEMd'><tr id='iWEMd'><dt id='iWEMd'><q id='iWEMd'><span id='iWEMd'><b id='iWEMd'><form id='iWEMd'><ins id='iWEMd'></ins><ul id='iWEMd'></ul><sub id='iWEMd'></sub></form><legend id='iWEMd'></legend><bdo id='iWEMd'><pre id='iWEMd'><center id='iWEMd'></center></pre></bdo></b><th id='iWEMd'></th></span></q></dt></tr></i><div id='iWEMd'><tfoot id='iWEMd'></tfoot><dl id='iWEMd'><fieldset id='iWEMd'></fieldset></dl></div>

                    • <legend id='iWEMd'><style id='iWEMd'><dir id='iWEMd'><q id='iWEMd'></q></dir></style></legend>

                        <tbody id='iWEMd'></tbody>

                        <tfoot id='iWEMd'></tfoot>

                        <small id='iWEMd'></small><noframes id='iWEMd'>

                        • <bdo id='iWEMd'></bdo><ul id='iWEMd'></ul>