Collecting Data into Database Automatically

Import Necessary Libraries

import pandas as pd
import sqlite3
import os

Get Data From Excel File

def GetData(filename):
# excel settings
try:
df = pd.read_excel(filename)
return df
except:


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Anuoluwapo Balogun

Import Necessary Libraries

import pandas as pd
import sqlite3
import os

Get Data From Excel File

def GetData(filename):
    # excel settings
    try:
        df = pd.read_excel(filename)
        return df
    except:
        print('File Not Readed Filename: ' , filename)
        pass
    # csv settings
    try:
        df = pd.read_csv(filename, delimiter=',')
        return df
    except:
        print('File Not Readed Filename: ' , filename)
        pass
    else:
        df = pd.read_csv(filename, delimiter=';')
        return df
    finally:
        print('CSV File Added!: ', filename)

Write Database Code

def RecordSql(dataframe, filename):
    filename = str(filename).lower().strip().replace(' ', '_')
    conn = sqlite3.connect('collector_database.db')
    try:
        dataframe.to_sql(filename, conn)
        print('Table Added: ', filename)
    except:
        pass
    conn.close()
    return

Run Code

if __name__ == '__main__':
    currdir = os.getcwd()
    for filepath in os.listdir(currdir):
        filename, ext = os.path.splitext(filepath)
        if ext in ['.xlsx', '.xls', '.csv']:
            print(filepath)
            data = GetData(filepath)
            RecordSql(data, filename)


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Anuoluwapo Balogun


Print Share Comment Cite Upload Translate Updates
APA

Anuoluwapo Balogun | Sciencx (2022-09-27T11:40:50+00:00) Collecting Data into Database Automatically. Retrieved from https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/

MLA
" » Collecting Data into Database Automatically." Anuoluwapo Balogun | Sciencx - Tuesday September 27, 2022, https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/
HARVARD
Anuoluwapo Balogun | Sciencx Tuesday September 27, 2022 » Collecting Data into Database Automatically., viewed ,<https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/>
VANCOUVER
Anuoluwapo Balogun | Sciencx - » Collecting Data into Database Automatically. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/
CHICAGO
" » Collecting Data into Database Automatically." Anuoluwapo Balogun | Sciencx - Accessed . https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/
IEEE
" » Collecting Data into Database Automatically." Anuoluwapo Balogun | Sciencx [Online]. Available: https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/. [Accessed: ]
rf:citation
» Collecting Data into Database Automatically | Anuoluwapo Balogun | Sciencx | https://www.scien.cx/2022/09/27/collecting-data-into-database-automatically/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.