在colab中读取sqlite文件

时间:2018-05-22 07:05:52

标签: pandas sqlite google-colaboratory

print ('Files in Drive:')

!ls drive/AI

云端硬盘中的文件:

database.sqlite
Reviews.csv
Untitled0.ipynb
fine_food_reviews.ipynb
Titanic.csv

当我在Google Colab中运行上述代码时,显然我的sqlite文件存在于我的驱动器中。但每当我对这个文件运行一些查询时,就会说

# using the SQLite Table to read data.
con = sqlite3.connect('database.sqlite') 

#filtering only positive and negative reviews i.e. 
# not taking into consideration those reviews with Score=3
filtered_data = pd.read_sql_query("SELECT * FROM Reviews WHERE Score !=3",con)
  

DatabaseError:sql上的执行失败'SELECT * FROM评论WHERE   得分!= 3':没有这样的表:评论

5 个答案:

答案 0 :(得分:2)

在下面,您将找到解决db setup on the Colab VMtable creationdata insertiondata querying的代码。在单个笔记本单元中执行所有代码段。

但是请注意,此示例仅显示了如何在非持久Colab VM上执行代码。如果要将数据库保存到GDrive,则必须首先安装Gdrive(source):

from google.colab import drive
drive.mount('/content/gdrive')

navigate之后的相应文件目录。


第1步:创建数据库

import sqlite3

conn = sqlite3.connect('SQLite_Python.db')  # You can create a new database by changing the name within the quotes
c = conn.cursor() # The database will be saved in the location where your 'py' file is saved

# Create table - CLIENTS
c.execute('''CREATE TABLE SqliteDb_developers
             ([id] INTEGER PRIMARY KEY, [name] text, [email] text, [joining_date] date, [salary] integer)''')

conn.commit()

测试数据库是否成功创建:

!ls

输出:

sample_data SQLite_Python.db


第2步:将数据插入数据库

import sqlite3

try:
    sqliteConnection = sqlite3.connect('SQLite_Python.db')
    cursor = sqliteConnection.cursor()
    print("Successfully Connected to SQLite")

    sqlite_insert_query = """INSERT INTO SqliteDb_developers
                          (id, name, email, joining_date, salary) 
                           VALUES (1,'Python','MakesYou@Fly.com','2020-01-01',1000)"""


    count = cursor.execute(sqlite_insert_query)
    sqliteConnection.commit()
    print("Record inserted successfully into SqliteDb_developers table ", cursor.rowcount)
    cursor.close()

except sqlite3.Error as error:
    print("Failed to insert data into sqlite table", error)
finally:
    if (sqliteConnection):
        sqliteConnection.close()
        print("The SQLite connection is closed")

输出:

已成功连接到SQLite

记录已成功插入SqliteDb_developers表1

SQLite连接已关闭


第3步:查询数据库

import sqlite3

conn = sqlite3.connect("SQLite_Python.db")

cur = conn.cursor()
cur.execute("SELECT * FROM SqliteDb_developers")

rows = cur.fetchall()

for row in rows:
  print(row)

conn.close()

输出:

(1,'Python','MakesYou@Fly.com','2020-01-01',1000)

答案 1 :(得分:0)

试试这个。看看那里有什么表。

"SELECT name FROM sqlite_master WHERE type='table'"

答案 2 :(得分:0)

为您的数据库文件提供类似的可共享ID,就像使用Reviews.csv一样

database_file = drive.CreateFile({'id':'sqlite文件的your_sharable_id'}) database_file.GetContentFile('database.sqlite')

答案 3 :(得分:0)

如果您尝试从Google驱动器访问文件,则需要先安装驱动器:

from google.colab import drive
drive.mount('/content/drive')

执行完此操作后,右键单击要在colab会话中读取的文件,然后选择“复制路径”并将其粘贴到连接字符串中。

con = sqlite3.connect('/content/database.sqlite')

您现在可以读取文件了。

答案 4 :(得分:0)

con = sqlite3.connect('database.sqlite')
filtered_data = pd.read_sql_query("SELECT * FROM Reviews WHERE Score !=3",con)

如果执行两次,则肯定会出现此类错误。请执行一次,没有任何错误。

如果遇到任何错误,请删除

  

database.sqlite

此文件并再次提取。这次再次执行,没有任何失败/错误。这对我有用。