Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Mar 3, 2024
1 parent 9d88b55 commit 6873185
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build/lib/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")
4 changes: 3 additions & 1 deletion build/lib/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 build/lib/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."
)
Binary file added dist/exceltosqlserver-0.2.5-py3-none-any.whl
Binary file not shown.
Binary file added dist/exceltosqlserver-0.2.5.tar.gz
Binary file not shown.
23 changes: 11 additions & 12 deletions exceltosqlserver.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Metadata-Version: 2.1
Name: exceltosqlserver
Version: 0.2.1
Version: 0.2.5
Summary: This package help convert your excel files (xlsx,xls,csv) to SQL Server Database.
Home-page: https://github.com/Xiangyongluo/exceltosqlserver
Author: Xiangyong Luo
Author-email: [email protected]
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE

Expand All @@ -18,7 +18,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 @@ -35,8 +35,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 @@ -48,11 +47,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 @@ -61,8 +60,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 @@ -75,16 +74,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.
3 changes: 2 additions & 1 deletion exceltosqlserver.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ exceltosqlserver.egg-info/SOURCES.txt
exceltosqlserver.egg-info/dependency_links.txt
exceltosqlserver.egg-info/top_level.txt
exceltosqlserver/test_data/__init__.py
exceltosqlserver/test_data/test01.xls
exceltosqlserver/test_data/test01.xls
tests/test_exceltosqlserver.py

0 comments on commit 6873185

Please sign in to comment.