Skip to content

Commit

Permalink
v0.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Mar 3, 2024
1 parent 4ed6848 commit de381f8
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.vscode/*
*build/*
*/dist/*
*.egg-info/*
*.egg-info/*
.pypirc
7 changes: 4 additions & 3 deletions .pypirc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[testpypi]
username = __token__
password = pypi-AgENdGVzdC5weXBpLm9yZwIkNjFiYzZkYWItMzMwYy00N2ZjLTliYTQtMDA2NWI4MjA0NThiAAIleyJwZXJtaXNzaW9ucyI6ICJ1c2VyIiwgInZlcnNpb24iOiAxfQAABiBwOUBIFc8NI00O1yGyw9qxoo16hgd4OeSiueYrTgHKMQ
password = pypi-AgENdGVzdC5weXBpLm9yZwIkZGI5YzBkNWQtNDUyYS00YzJjLTkyMWUtZDc2ZDNmM2E3MjQxAAIqWzMsImQxNjUyNjE5LTVkYjQtNDRiMy1iZGQ2LTVmZGE2NmMyMTZkNCJdAAAGIMPxamz6UNGefB0mxRTT1Ujgr8cZgeOh7uQatx4yecpq


[pypi]
username = __token__
password = pypi-AgEIcHlwaS5vcmcCJDcwMDdkM2VhLTI4MzctNDNlMy04YTFhLWUzYzM3ZDUzMjI5YgACJXsicGVybWlzc2lvbnMiOiAidXNlciIsICJ2ZXJzaW9uIjogMX0AAAYgECSzotZs3nCgsAPvMuT-nVjOdyTklsuusVE1672KstQ
username = __token__
password = pypi-AgEIcHlwaS5vcmcCJGMwMDFmZjFiLWYzZTItNDJjZC05ZDVjLWUxYWUzMWI4YzhhMQACKlszLCJlYTc2YmQzZC0zYTRlLTQwNzktOTNiZC1mZWUyNzUwZDgxOTkiXQAABiCC3lD_W_bJZK7nTGrV70m4pLazLDqvJkWr5wwhD_y8iw
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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"
Expand All @@ -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()

```

Expand All @@ -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.
2 changes: 1 addition & 1 deletion exceltosqlserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

from ._api import *

print("Version: 0.2.1")
print("Version: 0.2.5")
Binary file not shown.
Binary file added exceltosqlserver/__pycache__/_api.cpython-312.pyc
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion exceltosqlserver/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
72 changes: 51 additions & 21 deletions exceltosqlserver/exceltosqlserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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."
)
16 changes: 16 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -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

6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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(),
Expand Down
Binary file not shown.
36 changes: 36 additions & 0 deletions tests/test_exceltosqlserver.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de381f8

Please sign in to comment.