2
\$\begingroup\$

I have this python script that uses pandas read an excel sheet then to update a sqlite db it works perfectly just wondering if I can speed it up as I am at about 70k lines

"""
Created on Thu Aug  1 14:11:01 2019

@author: Shane Pitts
"""

# -*- coding: utf-8 -*-
import sqlite3
from sqlite3 import Error
import pandas as pd
import shutil

#Takes DB File path then creates connection to it
def create_connection(db_file):
    """ create a database connection to a SQLite database """
    try:
        conn = sqlite3.connect(db_file)
        return conn
        print(sqlite3.version)
    except Error as e:
        print(e)
    return None
#updates reported table    
def update_dupi_reported(conn, dupi, userID, op):

    with conn:
        cur = conn.cursor()
        cur.execute("UPDATE reported SET dupi = :dupi WHERE userID = :userID AND operator = :op",
                       {'dupi':dupi, 'userID':userID, 'op':op})
#updates blocked table
def update_dupi_blocked(conn, dupi, userID, op):

    with conn:
        cur = conn.cursor()
        cur.execute("UPDATE blocked SET dupi = :dupi WHERE userID = :userID AND operator = :op",
                       {'dupi':dupi, 'userID':userID, 'op':op})
def Count():
    #Creates a dataframe with the Excel sheet information
    df = pd.DataFrame()    
    df = pd.read_excel("/root/Shane_db/Count.xlsx")

    #Assigns a variable to the DataBase
    database = "/root/Shane_db/db.db"

    # create a database connection
    conn = create_connection(database)
    cur = conn.cursor()

    #Runs through the DataFrame once for reported
    for i in df.index:
        userID = df['userID'][i]
        dupi = df['dupi'][i]
        op = df['operator'][i]
        print(i)
        with conn:
            update_dupi_reported(conn, dupi, userID, op)
            #Runs through a Second time for blocked
    for x in df.index:
        userID = df['userID'][x]
        dupi = df['dupi'][x]
        op = df['operator'][x]
        print(x)
        with conn:
            update_dupi_blocked(conn, dupi, userID, op)

    shutil.copy("/root/Shane_db/db.db", "/var/www/html/site/db.db")

if __name__ == '__main__':
    Count()

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ Commented Jun 3, 2020 at 16:48
  • \$\begingroup\$ @SᴀᴍOnᴇᴌᴀ I don't mean this in any rude way at all would this be a better title? \$\endgroup\$ Commented Jun 3, 2020 at 18:26
  • \$\begingroup\$ It is better... though keywords like pandas and excel are not as beneficial since the question has some of those as tags... if you had a description of how the data is used (e.g. for some type of report) that could help add clarity for reviewers (either in the title and/or description) \$\endgroup\$ Commented Jun 3, 2020 at 18:36

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.