【问题标题】:how to connect to sqlite from sqlalchemy如何从 sqlalchemy 连接到 sqlite
【发布时间】:2020-03-12 18:45:41
【问题描述】:

我的主目录中有一个 sqlite 数据库。

stephen@stephen-AO725:~$ pwd
/home/stephen
stephen@stephen-AO725:~$ sqlite db1
SQLite version 2.8.17
Enter ".help" for instructions
sqlite> select * from test
   ...> ;
3|4
5|6
sqlite> .quit

当我尝试使用 sqlalchemy 和 pandas 从 jupiter notebook 连接时,某事不起作用。

db=sqla.create_engine('sqlite:////home/stephen/db1')
pd.read_sql('select * from db1.test',db)

~/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context) 578 第579章 --> 580 cursor.execute(语句,参数) 581 582 def do_execute_no_params(self, cursor, statement, context=None)

DatabaseError: (sqlite3.DatabaseError) 文件不是数据库 [SQL:从 db1.test 中选择 *] (此错误的背景:http://sqlalche.me/e/4xp6

我也试过了:

db=sqla.create_engine('sqlite:///~/db1')

同样的结果

【问题讨论】:

  • URL 应该有 3 个斜杠,而不是 4 个。
  • @klaus 4 是正确的,如果使用绝对路径。 sqlite://,后面是不存在的主机,然后是分隔符/,最后是路径:docs.sqlalchemy.org/en/13/dialects/sqlite.html#connect-strings
  • 查看控制台输出 SQLite version 2.8.17 伸出。我认为 Python 驱动程序适用于 SQLite 3.x,因此可能无法打开您的数据库文件。
  • 根据帮助页面,url 应该有 4 个斜杠。

标签: python sqlite sqlalchemy


【解决方案1】:

如 Everila 所述,问题不在于向后兼容性。 anaconda安装了自己的sqlite,即sqlite3.x,并且sqlite无法加载sqlite 2.x创建的数据库 使用 sqlite 3 创建数据库后,代码工作正常

db=sqla.create_engine('sqlite:////home/stephen/db1')
pd.read_sql('select * from test',db)

确认需要 4 个斜线。

【讨论】:

    【解决方案2】:

    个人而言,只是为了完成@Stephen的代码以及所需的模块:

    # 1.-Load module
    import sqlalchemy
    import pandas as pd
    #2.-Turn on database engine
    dbEngine=sqlalchemy.create_engine('sqlite:////home/stephen/db1.db') # ensure this is the correct path for the sqlite file. 
    
    #3.- Read data with pandas
    pd.read_sql('select * from test',dbEngine)
    
    #4.- I also want to add a new table from a dataframe in sqlite (a small one) 
    
    df_todb.to_sql(name = 'newTable',con= dbEngine, index=False, if_exists='replace') 
    
    

    另一种阅读方式是使用 sqlite3 库,可能更直接:

    #1. - Load libraries
    import sqlite3
    import pandas as pd
    
    # 2.- Create your connection.
    cnx = sqlite3.connect('sqlite:////home/stephen/db1.db')
    cursor = cnx.cursor()
    
    # 3.- Query and print all the tables in the database engine
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
    print(cursor.fetchall())
    
    # 4.-  READ TABLE OF SQLITE CALLED test
    dfN_check = pd.read_sql_query("SELECT * FROM test", cnx) # we need real name of table
    
    #  5.- Now I want to delete all rows of this table
    cnx.execute("DELETE FROM test;")
    
    # 6. -COMMIT CHANGES! (mandatory if you want to save these changes in the database)
    cnx.commit()
    
    
    # 7.- Close the connection with the database
    cnx.close()
    

    如果这有帮助,请告诉我!

    【讨论】:

      猜你喜欢
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      相关资源
      最近更新 更多