title: python3 how to write to a sqlite3 database in a thread
body: <p>I have a fastapi server that can have several clients connecting to it. It seems that every time you visit the site will it kick off a different thread on the server computer. I am trying to find a way to write to a sqlite3 database that will only require me to open the database once. If there is a better solution than what I said please let me know.</p>
  <p>main.py</p>
  <pre><code>from fastapi import FastAPI
  from pydantic import BaseModel
  import Database
  import uvicorn
  
  app = FastAPI()
  
  Database_obj = Database.Database_Sqlite3()
  
  
  
  # This class is to allow me to control the input for the data_from_client function. This will be inputed into the computer table in the database
  class Computer(BaseModel):
      uuid: str
      computer_name: str
      public_ip: str
      date: str
  
  @app.post(&quot;/test&quot;)
  def data_from_client(computer: Computer):
      try:
          # This does not seem very efficent and I am not sure that this will work when several clients start connecting. This requires the database to be opened and closed
          # every time you need to write and fastapi will allow the clients to connect in different threads. 
          Database_obj.safe_start()
          Database_obj.write_computer_table(uuid=computer.uuid, computer_name=computer.computer_name, public_ip=computer.public_ip, date=computer.date)
      except Exception as e:
          print(e)
  
  if __name__ == &quot;__main__&quot;:
      uvicorn.run(&quot;main:app&quot;, host=&quot;0.0.0.0&quot;, port=8000, reload=True)
  </code></pre>
  <p>Database.py</p>
  <pre><code>import sqlite3
  import os
  import mysql.connector
  
  
  class Database_Sqlite3:
  
      def __init__(self):
          self.connection = None
          self.cursor = None
          self.database = r&quot;database.db&quot;
          # This setting will not always do what I want. This will create the Data directly from the location of your terminal not the location of the application. I will need to come up with a better way for this.
          self.path = &quot;./Data&quot;
  
      def make_local_directory(self):
          is_dir = os.path.isdir(self.path)
          if not is_dir:
              try:
                  os.mkdir(self.path)
                  return 0
              except Exception as e:
                  print(e)
                  return 1
          else:
              return 0
  
      def start_database(self):
          try:
              full_database_path = os.path.join(self.path, self.database)
              self.connection = sqlite3.connect(full_database_path)
              self.cursor = self.connection.cursor()
              return 0
          except Exception as e:
              print(e)
              return 1
      
      def safe_start(self):
          try:
              self.make_local_directory()
              self.start_database()
              return 0
          except Exception as e:
              print(e)
              return 1
  
      def create_computer_table(self):
          try:
              self.cursor.execute('''CREATE TABLE computer (uuid text, computer_name text, public_ip text, date text)''')
              return 0
          except Exception as e:
              print(e)
              return 1
  
      def write_computer_table(self, uuid, computer_name, public_ip, date):
          data = (uuid, computer_name, public_ip, date)
          self.cursor.execute('INSERT INTO computer VALUES(?, ?, ?, ?)', data)
          self.connection.commit()
      
  </code></pre>