print ('Files in Drive:')
!ls drive/AI驱动器中的文件:
database.sqlite
Reviews.csv
Untitled0.ipynb
fine_food_reviews.ipynb
Titanic.csv当我在Google中运行上面的代码时,很明显,我的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 Reviews != 3‘上执行失败:没有这样的表:评审
发布于 2020-01-14 19:49:05
下面将找到解决db setup on the Colab VM、table creation、data insertion和data querying的代码。在单个笔记本单元中执行所有代码片段。
但是,请注意,此示例仅说明如何在非持久性Colab上执行代码。如果要将数据库保存到GDrive,则必须先挂载Gdrive (来源):
from google.colab import drive
drive.mount('/content/gdrive')并将导航转到适当的文件目录之后。
步骤1:创建DB
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是否已成功创建:
!ls输出:
sample_data SQLite_Python.db
步骤2:将数据插入DB
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
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)
发布于 2018-05-22 10:46:49
试试这个吧。看看那里有几张桌子。
"SELECT name FROM sqlite_master WHERE type='table'"发布于 2018-09-19 16:08:01
向数据库文件提供类似的可共享id,就像对Reviews.csv一样
database_file=drive.CreateFile({'id':'your_sharable_id表示sqlite‘}) database_file.GetContentFile('database.sqlite')
https://stackoverflow.com/questions/50461696
复制相似问题