diff --git a/.gitignore b/.gitignore index a93cdb8..b020c1a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.vscode/* *build/* */dist/* -*.egg-info/* \ No newline at end of file +*.egg-info/* +.pypirc diff --git a/.pypirc b/.pypirc index 7e8fe1b..067c8d4 100644 --- a/.pypirc +++ b/.pypirc @@ -1,7 +1,8 @@ [testpypi] username = __token__ - password = pypi-AgENdGVzdC5weXBpLm9yZwIkNjFiYzZkYWItMzMwYy00N2ZjLTliYTQtMDA2NWI4MjA0NThiAAIleyJwZXJtaXNzaW9ucyI6ICJ1c2VyIiwgInZlcnNpb24iOiAxfQAABiBwOUBIFc8NI00O1yGyw9qxoo16hgd4OeSiueYrTgHKMQ + password = pypi-AgENdGVzdC5weXBpLm9yZwIkZGI5YzBkNWQtNDUyYS00YzJjLTkyMWUtZDc2ZDNmM2E3MjQxAAIqWzMsImQxNjUyNjE5LTVkYjQtNDRiMy1iZGQ2LTVmZGE2NmMyMTZkNCJdAAAGIMPxamz6UNGefB0mxRTT1Ujgr8cZgeOh7uQatx4yecpq + [pypi] -username = __token__ -password = pypi-AgEIcHlwaS5vcmcCJDcwMDdkM2VhLTI4MzctNDNlMy04YTFhLWUzYzM3ZDUzMjI5YgACJXsicGVybWlzc2lvbnMiOiAidXNlciIsICJ2ZXJzaW9uIjogMX0AAAYgECSzotZs3nCgsAPvMuT-nVjOdyTklsuusVE1672KstQ \ No newline at end of file + username = __token__ + password = pypi-AgEIcHlwaS5vcmcCJGMwMDFmZjFiLWYzZTItNDJjZC05ZDVjLWUxYWUzMWI4YzhhMQACKlszLCJlYTc2YmQzZC0zYTRlLTQwNzktOTNiZC1mZWUyNzUwZDgxOTkiXQAABiCC3lD_W_bJZK7nTGrV70m4pLazLDqvJkWr5wwhD_y8iw diff --git a/README.md b/README.md index 3dcf94f..cebff41 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This package help to convert your excel files (xlsx,xls,csv) to SQL Server datab # Installation -exceltomysql can be installed as: +exceltosqlserver can be installed as: ```python pip install exceltosqlserver @@ -21,8 +21,7 @@ pip install exceltosqlserver # QuickStart ```python -import exceltosqlserver as es -# generate the class instance +from exceltosqlserver import ExcelToDB, hostname, local_ip # STEP One, prepare your input pareameters @@ -34,11 +33,11 @@ rename_table = "" # Use your filename as tablename onto SQL Server or user defi # get your local host name # this will return your local computer name for your sql server database -host_name = es.hostname +host_name = hostname # get your local ip address # this will return your local ip address (if your sql server can be accessed by DNS) -ip = es.local_ip +ip = local_ip # you need to change your host if needed, dns: local ip address #yourHostORip = "localhost" @@ -47,8 +46,8 @@ yourHostORip = ip # STEP Two add your data to sql server -es.exceltoDBtable(yourFile, yourHostORip, yourUsrID, yourPWD, yourDBname, rename_table) - +es = ExcelToDB(yourFile, yourHostORip, yourUsrID, yourPWD, yourDBname, rename_table) +es.save2db() ``` @@ -61,16 +60,16 @@ Sucessfully saved 'yourtable' to SQL Server... # API Reference -exceltosqlserver.exceltoDBtable(`filePath,hostORip=False,usrID =False,pwd=False,database=False,rename_table`) +exceltosqlserver.ExcelToDB(`filePath,host_ip=False,usrID =False,pwd=False,db_name=False,rename_table`) filePath: str -hostORip: str default: "" +host_ip: str default: "" usrID: str default: "" pwd: str default: "" -database: str default: "" +db_name: str default: "" rename_table: str default: "", will auto save your filename as table name to sql server database. If assignmed value, will change table name from your filename to the assigned value. diff --git a/exceltosqlserver/__init__.py b/exceltosqlserver/__init__.py index 2d23fc3..511fbfd 100644 --- a/exceltosqlserver/__init__.py +++ b/exceltosqlserver/__init__.py @@ -1,4 +1,4 @@ from ._api import * -print("Version: 0.2.1") +print("Version: 0.2.5") diff --git a/exceltosqlserver/__pycache__/__init__.cpython-312.pyc b/exceltosqlserver/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..b483139 Binary files /dev/null and b/exceltosqlserver/__pycache__/__init__.cpython-312.pyc differ diff --git a/exceltosqlserver/__pycache__/_api.cpython-312.pyc b/exceltosqlserver/__pycache__/_api.cpython-312.pyc new file mode 100644 index 0000000..04c7213 Binary files /dev/null and b/exceltosqlserver/__pycache__/_api.cpython-312.pyc differ diff --git a/exceltosqlserver/__pycache__/exceltosqlserver.cpython-312.pyc b/exceltosqlserver/__pycache__/exceltosqlserver.cpython-312.pyc new file mode 100644 index 0000000..3287222 Binary files /dev/null and b/exceltosqlserver/__pycache__/exceltosqlserver.cpython-312.pyc differ diff --git a/exceltosqlserver/_api.py b/exceltosqlserver/_api.py index 9a0f129..5a69ffd 100644 --- a/exceltosqlserver/_api.py +++ b/exceltosqlserver/_api.py @@ -6,6 +6,8 @@ ############################################################## -from .exceltosqlserver import exceltoDBtable +from .exceltosqlserver import ExcelToDB from .exceltosqlserver import hostname from .exceltosqlserver import local_ip + +__all__ = ['ExcelToDB', 'hostname', 'local_ip'] \ No newline at end of file diff --git a/exceltosqlserver/exceltosqlserver.py b/exceltosqlserver/exceltosqlserver.py index 821709b..3099af6 100644 --- a/exceltosqlserver/exceltosqlserver.py +++ b/exceltosqlserver/exceltosqlserver.py @@ -15,29 +15,41 @@ local_ip = socket.gethostbyname(hostname) -class exceltoDBtable: - # Available for sql server and mysql now +class ExcelToDB: def __init__(self, filePath: str, - hostORip: str = "", + host_ip: str = "", usrID: str = "", pwd: str = "", - database: str = "", + db_name: str = "", rename_table: str = ""): + """This class is used to save excel or csv file into sql server database. - if not any([hostORip, database, usrID, pwd]): + Args: + filePath (str): your file path. + host_ip (str, optional): your local machine host or ip address. Defaults to "". + usrID (str, optional): sql server user id . Defaults to "". + pwd (str, optional): sql server password. Defaults to "". + db_name (str, optional): database name in sql server. Defaults to "". + rename_table (str, optional): rename your input table. + if "", will use exact the same table name of your input file. Defaults to "". + + Raises: + Exception: Partially inputs, please check your inputs... + + """ + + if not any([host_ip, db_name, usrID, pwd]): raise Exception("Partially inputs, please check your inputs...") self.filePath = filePath - self.hostORip = hostORip - self.database = database + self.host_ip = host_ip + self.db_name = db_name self.usrID = usrID self.pwd = pwd self.rename_table = rename_table self.dbType = ["sqlserver"] - self.readData() - self.connect2DB() def connect2DB(self) -> None: # sourcery skip: assign-if-exp, extract-method # This will test whether a sql server database or a mysql database @@ -57,21 +69,10 @@ def connect2DB(self) -> None: # sourcery skip: assign-if-exp, extract-method try: # connect to sql server self.engine = create_engine( - f"mssql+pyodbc://{self.usrID}:{self.pwd}@{self.hostORip}/{self.database}?driver={driveString}?") + f"mssql+pyodbc://{self.usrID}:{self.pwd}@{self.host_ip}/{self.db_name}?driver={driveString}?") print("Successfully connected to SQL Server...") - - if self.rename_table: - table_name = self.rename_table - elif "/" in self.filePath: - table_name = self.filePath.split("/")[-1].split(".")[0] - else: - table_name = self.filePath.split(".")[0] - - self.file_data.to_sql(table_name, con=self.engine) - print("Successfully saved %s into SQL Server..." % table_name) return None - except Exception: self.engine = False continue @@ -88,3 +89,32 @@ def readData(self) -> None: print("Successfully load csv data...") else: raise Exception("Unable to load input file...") + + def save2db(self) -> None: + """Save your data into sql server database. + """ + + self.readData() + self.connect2DB() + + # specify the table name + if self.rename_table: + tableName = self.rename_table + elif "/" in self.filePath: + tableName = self.filePath.split("/")[-1].split(".")[0] + else: + tableName = self.filePath.split(".")[0] + + if self.engine: + try: + self.file_data.to_sql(tableName, con=self.engine) + print("Successfully saved %s into SQL Server..." % tableName) + + except Exception as e: + raise Exception( + "Can not save table to sql server, please check your inputs." + ) from e + else: + raise Exception( + "Can not save table to sql server, please check your inputs." + ) \ No newline at end of file diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..eff53b0 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,16 @@ +pandas>=2.2.1 +psycopg2>=2.9.9 +pytest>=8.0.2 +pyufunc>=0.2.1 +setuptools>=68.2.2 +SQLAlchemy>=2.0.27 +tqdm +shapely +numpy +beautifulsoup4 +requests +urllib3 +sphinx +pyodbc +exceltosqlserver + diff --git a/setup.py b/setup.py index 6aa0523..9800811 100644 --- a/setup.py +++ b/setup.py @@ -11,8 +11,8 @@ modules_needed = [] setuptools.setup( - name="exceltosqlserver", # Replace with your own username - version="0.2.1", + name="exceltosqlserver", # Replace with your own username + version="0.2.5", author="Xiangyong Luo", author_email="rochemay@163.com", description="This package help convert your excel files (xlsx,xls,csv) to SQL Server Database.", @@ -25,7 +25,7 @@ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], - python_requires='>=3.6', + python_requires='>=3.10', install_requires=modules_needed, packages=setuptools.find_packages(), diff --git a/tests/__pycache__/test_exceltosqlserver.cpython-312-pytest-8.0.2.pyc b/tests/__pycache__/test_exceltosqlserver.cpython-312-pytest-8.0.2.pyc new file mode 100644 index 0000000..9eb11c1 Binary files /dev/null and b/tests/__pycache__/test_exceltosqlserver.cpython-312-pytest-8.0.2.pyc differ diff --git a/tests/test_exceltosqlserver.py b/tests/test_exceltosqlserver.py new file mode 100644 index 0000000..5bfa4bb --- /dev/null +++ b/tests/test_exceltosqlserver.py @@ -0,0 +1,36 @@ +# -*- coding:utf-8 -*- +############################################################## +# Created Date: Sunday, March 3rd 2024 +# Contact Info: luoxiangyong01@gmail.com +# Author/Copyright: Mr. Xiangyong Luo +############################################################## + + +import pytest +from exceltosqlserver import ExcelToDB + +import sqlalchemy +import pyodbc +import pandas as pd +import sys +from pathlib import Path +sys.path.append(str(Path(__file__).parent.parent.absolute())) + + +def test_db_connection(): + """This function is used to test the database connection.""" + excel2db = ExcelToDB(filePath="test_data/test_data.xlsx", + host_ip="localhost", + usrID="sa", + pwd="123456", + db_name="TestDB", + rename_table="TestTable") + + excel2db.connect2DB() + assert excel2db.dbType == ["sqlserver"] + assert excel2db.host_ip == "localhost" + assert excel2db.db_name == "TestDB" + assert excel2db.usrID == "sa" + assert excel2db.pwd == "123456" + assert excel2db.rename_table == "TestTable" + assert excel2db.engine is not None \ No newline at end of file