首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Google中查询SQLite数据库文件

在Google中查询SQLite数据库文件
EN

Stack Overflow用户
提问于 2018-05-22 07:05:51
回答 5查看 8K关注 0票数 0
代码语言:javascript
复制
print ('Files in Drive:')

!ls drive/AI

驱动器中的文件:

代码语言:javascript
复制
database.sqlite
Reviews.csv
Untitled0.ipynb
fine_food_reviews.ipynb
Titanic.csv

当我在Google中运行上面的代码时,很明显,我的sqlite文件在我的驱动器中。但是,每当我对这个文件执行一些查询时,它就会显示

代码语言:javascript
复制
# 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 Reviews != 3‘上执行失败:没有这样的表:评审

EN

回答 5

Stack Overflow用户

发布于 2020-01-14 19:49:05

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

但是,请注意,此示例仅说明如何在非持久性Colab上执行代码。如果要将数据库保存到GDrive,则必须先挂载Gdrive (来源):

代码语言:javascript
复制
from google.colab import drive
drive.mount('/content/gdrive')

并将导航转到适当的文件目录之后。

步骤1:创建DB

代码语言:javascript
复制
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()

测试DB是否已成功创建:

代码语言:javascript
复制
!ls

输出:

sample_data SQLite_Python.db

步骤2:将数据插入DB

代码语言:javascript
复制
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:查询DB

代码语言:javascript
复制
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)

票数 3
EN

Stack Overflow用户

发布于 2018-05-22 10:46:49

试试这个吧。看看那里有几张桌子。

代码语言:javascript
复制
"SELECT name FROM sqlite_master WHERE type='table'"
票数 1
EN

Stack Overflow用户

发布于 2018-09-19 16:08:01

向数据库文件提供类似的可共享id,就像对Reviews.csv一样

database_file=drive.CreateFile({'id':'your_sharable_id表示sqlite‘}) database_file.GetContentFile('database.sqlite')

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50461696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档