This commit is contained in:
yumoqing 2023-04-11 15:12:10 +08:00
parent 300e0745b0
commit 07c2848da3
3 changed files with 23 additions and 22 deletions

View File

@ -1,21 +1,18 @@
import openpyxl as xlsx
import asyncio
from sqlor.dbpools import DBPools
class CBObject:
def __init__(self,name):
def __init__(self,db, name):
self.db = db
self.tbl = name
self.sql = ''
async def handle(self,dic):
names = ','.join(dic.keys())
vs = ["'%s'"%v if type(v)==type('') else str(v) for v in dic.values() ]
values = ','.join(vs)
self.sql = "insert into " + self.tbl + \
" (" + names + ") values (" + \
values + ");"
print(self.sql)
async def handle(self,ws):
db = DBPools()
meta = sor.I()
async with db.sqlorContext(self.db) as sor:
for rec in getRecord(ws):
sor.C(self.tbl, rec)
typesconv = {
"int":int,
@ -23,7 +20,7 @@ typesconv = {
"str":str,
}
async def loadData(ws,callback):
async def getRecord(ws):
names = []
types = []
for i,r in enumerate(ws.rows):
@ -40,21 +37,23 @@ async def loadData(ws,callback):
for j,c in enumerate(r):
tf = typesconv.get(types[j],None)
v = c.value
if tf is not None:
v = tf(v)
dic[names[j]] = v
await callback(dic)
yield rec
async def excel2db(xlsxfile):
wb = xlsx.load_workbook(xlsxfile)
dbname = [ i[2:-3] for i in wb.sheetnames if i.startswith('__')[0]
for name in wb.sheetnames:
if name.startswith('__'):
continue
ws = wb[name]
cbobj = CBObject(name)
await loadData(ws,cbobj.handle)
cbobj = CBObject(dbname, name)
await cbobj.handle(ws)
if __name__ == '__main__':
import sys
config = getConfig()
DBPools(config.databases)
if len(sys.argv) < 2:
print('%s xlsxfile' % sys.argv[0])
sys.exit(1)

View File

@ -147,7 +147,7 @@ limit $[from_line]$,$[rows]$"""
return sqlcmd
def pkSQL(self,tablename=None):
sqlcmd = """SELECT column_name as name FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` WHERE table_name='%s' AND constraint_name='PRIMARY'
sqlcmd = """SELECT distinct column_name as name FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` WHERE table_name='%s' AND constraint_name='PRIMARY'
""" % tablename.lower()
return sqlcmd

View File

@ -94,6 +94,7 @@ class SQLor(object):
schemas = []
for t in tabs:
primary = await self.primary(t.name)
# print('primary=', primary)
indexes = concat_idx_info(await self.indexes(t.name))
fields = await self.fields(t.name)
primary_fields = [f.field_name for f in primary]
@ -513,6 +514,7 @@ class SQLor(object):
sqlstring = self.pkSQL(tablename)
recs = []
await self.execute(sqlstring,{},lambda x:recs.append(x))
# print('sql=', sqlstring, 'recs=', recs)
return recs
async def fkeys(self,tablename):
@ -621,7 +623,7 @@ class SQLor(object):
newData = [ i for i in ns.keys() if i not in condi and i in fields]
c = [ '%s = ${%s}$' % (i,i) for i in condi ]
u = [ '%s = ${%s}$' % (i,i) for i in newData ]
c_str = ','.join(c)
c_str = ' and '.join(c)
u_str = ','.join(u)
sql = 'update %s.%s set %s where %s' % (self.dbname, tablename,
u_str,c_str)
@ -633,7 +635,7 @@ class SQLor(object):
fields = [ i['name'] for i in desc['fields']]
condi = [ i for i in desc['summary'][0]['primary']]
c = [ '%s = ${%s}$' % (i,i) for i in condi ]
c_str = ','.join(c)
c_str = ' and '.join(c)
sql = 'delete from %s.%s where %s' % (self.dbname, tablename,c_str)
await self.runSQL({'sql_string':sql},ns,None)