创建它并将数据流式传输到 db 文件后,在 sqlite 中找不到我的 table
Can't find my table in sqlite after creating it and streaming data to the db file
这是我第一次使用sqlite,之前只用过MySQL。我有一个程序,我可以在其中流式传输实时推特推文并将它们存储在数据库中。该程序创建一个数据库,然后启动 运行ning tweepy 从 twitter 获取数据。我在尝试从我的数据库文件 twitter.db
中打印数据以进行数据探索时遇到问题。然而,我确实在我的控制台上看到了实时的推文流,我似乎无法从数据库中调用数据。
下面是我的数据库。
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
def create_table():
try:
c.execute("CREATE TABLE IF NOT EXISTS sentiment(unix REAL, tweet TEXT, sentiment REAL)")
c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
conn.commit()
except Exception as e:
print(str(e))
create_table()
在我 运行 程序一次后,我将 def create_table()
函数标记出来,以允许数据流在没有程序 运行 另一个 create_table( ).下面是我如何将数据流式传输到我的数据库。
def on_data(self, data):
try:
data = json.loads(data)
tweet = unidecode(data['text'])
time_ms = data['timestamp_ms']
analysis = TextBlob(tweet)
sentiment = analysis.sentiment.polarity
print(time_ms, tweet, sentiment)
c.execute("INSERT INTO sentiment (unix, tweet, sentiment) VALUES (?, ?, ?)",
(time_ms, tweet, sentiment))
conn.commit()
except KeyError as e:
print(str(e))
return(True)
来自 twitter API 的流式传输似乎运行良好,但是当我想打印出我的行以进行数据探索并检查数据是否正在存储时,我收到此错误:OperationalError: no such table: sentiment
.下面的代码会产生上述错误:
import sqlite3
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
c.execute("SELECT * FROM sentiment")
print(c.fetchall())
当我 运行 c.execute("SELECT * FROM sqlite_master")
...我在屏幕上打印了 []
。我假设并且知道有些事情是非常错误的。上面的代码有什么问题?
谢谢。
您是否从同一目录执行脚本?
如果不确定我建议用两种脚本都写
import os
print("I am in following directory: ", os.getcwd())
conn = sqlite3.connect('twitter.db')
而不是
conn = sqlite3.connect('twitter.db')
并检查两者是否真的在同一目录中查找 twitter.db
如果他们这样做,则转到命令行更改到此目录并键入
sqlite3 twitter.db
然后输入
.tables
看看会列出什么 table。
然后您甚至可以键入查询(如果 table 存在)以查看更多详细信息
SELECT * FROM sentiment;
我认为 gelonida 物有所值,命令行 sqlite3 是你的朋友。
我稍微修改了你的代码,当数据库位于同一位置时它工作正常:
import os
import sqlite3
os.remove('twitter.db')
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
def create_table():
try:
c.execute("CREATE TABLE IF NOT EXISTS sentiment(unix REAL, tweet TEXT, sentiment REAL)")
c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
conn.commit()
except Exception as e:
print(str(e))
create_table()
def on_data():
try:
tweet = 'text'
time_ms = 123.2
sentiment = 1.234
print(time_ms, tweet, sentiment)
c.execute("INSERT INTO sentiment (unix, tweet, sentiment) VALUES (?, ?, ?)",
(time_ms, tweet, sentiment))
conn.commit()
except KeyError as e:
print(str(e))
return(True)
on_data()
conn.close()
# Now check the result
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
c.execute("SELECT * FROM sentiment")
print(c.fetchall())
运行 它打印:
123.2 text 1.234
[(123.2, 'text', 1.234)]
这是我第一次使用sqlite,之前只用过MySQL。我有一个程序,我可以在其中流式传输实时推特推文并将它们存储在数据库中。该程序创建一个数据库,然后启动 运行ning tweepy 从 twitter 获取数据。我在尝试从我的数据库文件 twitter.db
中打印数据以进行数据探索时遇到问题。然而,我确实在我的控制台上看到了实时的推文流,我似乎无法从数据库中调用数据。
下面是我的数据库。
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
def create_table():
try:
c.execute("CREATE TABLE IF NOT EXISTS sentiment(unix REAL, tweet TEXT, sentiment REAL)")
c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
conn.commit()
except Exception as e:
print(str(e))
create_table()
在我 运行 程序一次后,我将 def create_table()
函数标记出来,以允许数据流在没有程序 运行 另一个 create_table( ).下面是我如何将数据流式传输到我的数据库。
def on_data(self, data):
try:
data = json.loads(data)
tweet = unidecode(data['text'])
time_ms = data['timestamp_ms']
analysis = TextBlob(tweet)
sentiment = analysis.sentiment.polarity
print(time_ms, tweet, sentiment)
c.execute("INSERT INTO sentiment (unix, tweet, sentiment) VALUES (?, ?, ?)",
(time_ms, tweet, sentiment))
conn.commit()
except KeyError as e:
print(str(e))
return(True)
来自 twitter API 的流式传输似乎运行良好,但是当我想打印出我的行以进行数据探索并检查数据是否正在存储时,我收到此错误:OperationalError: no such table: sentiment
.下面的代码会产生上述错误:
import sqlite3
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
c.execute("SELECT * FROM sentiment")
print(c.fetchall())
当我 运行 c.execute("SELECT * FROM sqlite_master")
...我在屏幕上打印了 []
。我假设并且知道有些事情是非常错误的。上面的代码有什么问题?
谢谢。
您是否从同一目录执行脚本?
如果不确定我建议用两种脚本都写
import os
print("I am in following directory: ", os.getcwd())
conn = sqlite3.connect('twitter.db')
而不是
conn = sqlite3.connect('twitter.db')
并检查两者是否真的在同一目录中查找 twitter.db
如果他们这样做,则转到命令行更改到此目录并键入
sqlite3 twitter.db
然后输入
.tables
看看会列出什么 table。 然后您甚至可以键入查询(如果 table 存在)以查看更多详细信息
SELECT * FROM sentiment;
我认为 gelonida 物有所值,命令行 sqlite3 是你的朋友。
我稍微修改了你的代码,当数据库位于同一位置时它工作正常:
import os
import sqlite3
os.remove('twitter.db')
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
def create_table():
try:
c.execute("CREATE TABLE IF NOT EXISTS sentiment(unix REAL, tweet TEXT, sentiment REAL)")
c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
conn.commit()
except Exception as e:
print(str(e))
create_table()
def on_data():
try:
tweet = 'text'
time_ms = 123.2
sentiment = 1.234
print(time_ms, tweet, sentiment)
c.execute("INSERT INTO sentiment (unix, tweet, sentiment) VALUES (?, ?, ?)",
(time_ms, tweet, sentiment))
conn.commit()
except KeyError as e:
print(str(e))
return(True)
on_data()
conn.close()
# Now check the result
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
c.execute("SELECT * FROM sentiment")
print(c.fetchall())
运行 它打印:
123.2 text 1.234
[(123.2, 'text', 1.234)]