0
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()

My goal was to put the 'stock.db' file in the Streamlit app. However, the result was 'FileUploaderEncodingWarning'. I actually don't even know what's this error means, either. I know some people think that I didn't put effort, but I searched a lot. So the basis of my question is how can I remove this error?

1 Answer 1

1

FileUploaderEncodingWarning is a warning that in the future, the uploader will not attempt to automatically decode uploaded text files but instead will expect that an encoding parameter is provided specifying the encoding to be used.

However you are uploading a binary file - a sqlite database - so you can ignore the warning.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.