在 Streamlit 中放置 sqlite3 db 文件的问题
Posted
技术标签:
【中文标题】在 Streamlit 中放置 sqlite3 db 文件的问题【英文标题】:Problem with putting on sqlite3 db file in Streamlit 【发布时间】:2021-01-06 04:57:42 【问题描述】:import sqlite3
from sqlite3 import Error
import csv
import numpy as np
import tempfile
import streamlit as st
conn = None
db = st.file_uploader("stock.db", type="db")
if db:
with tempfile.NamedTemporaryFile() as fp:
fp.write(db.getvalue())
conn = sqlite3.connect(fp.name)
if conn:
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def create_project(conn, project):
"""
Create a new project into the projects table
:param conn:
:param project:
:return: project id
"""
sql = ''' INSERT INTO projects(compamy_name,begin_price,end_price)
Values(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, project)
conn.commit()
return cur.lastrowid
def create_task(conn, task):
"""
Create a new task
:param conn:
:param task:
:return:
"""
sql = ''' INSERT INTO tasks(name,priority,status_id,project_id,begin_date,end_date)
VALUES(?,?,?,?,?,?) '''
cur = conn.cursor()
cur.execute(sql, task)
conn.commit()
return cur.lastrowid
def main():
database = r"./stock.db"
conn = create_connection(database)
with conn:
try:
curs = conn.cursor()
curs.execute('''CREATE TABLE IF NOT EXISTS stock (
id INT PRIMARY KEY,
company_name VARCHAR(200),
now_price INT
)''')
curs.close()
except:
import traceback
print(traceback.format_exc())
from csv import DictReader
with open('data.csv', encoding='utf-8-sig') as file:
data = DictReader(file)
curs = conn.cursor()
for row in data:
print(row)
ins = 'INSERT INTO stock (id, company_name, now_price) VALUES (?, ?, ?)'
curs.execute(ins, (
row['id'],
row['company_name'],
int(row['now_price'])
))
if __name__ == '__main__':
main()
我的目标是将“stock.db”文件放入 Streamlit 应用程序中。但是,结果是“FileUploaderEncodingWarning”。我其实也不知道这个错误是什么意思。我知道有些人认为我没有付出努力,但我搜索了很多。所以我的问题的基础是如何消除这个错误?
【问题讨论】:
【参考方案1】:FileUploaderEncodingWarning
是一个警告,在未来,上传者will not attempt to automatically decode uploaded text files 将提供expect that an encoding parameter,指定要使用的编码。
但是,您正在上传二进制文件 - sqlite 数据库 - 所以您可以忽略警告。
【讨论】:
以上是关于在 Streamlit 中放置 sqlite3 db 文件的问题的主要内容,如果未能解决你的问题,请参考以下文章
如何在虚拟 conda 环境中设置 streamlit 的路径
我如何在XmlDocument的XmlElement中放置字符串
“ __conform__() 不是有效的 Streamlit 命令。”