text
stringlengths
0
828
- ``dbConn`` -- mysql database connection
- ``log`` -- logger
**Return:**
- None
**Usage:**
.. todo::
add usage info
create a sublime snippet for usage
.. code-block:: python
usage code
""""""
log.debug('starting the ``_insert_single_batch_into_database`` function')
global totalCount
global globalDbConn
global sharedList
batch = sharedList[batchIndex]
reDate = reDatetime
if isinstance(globalDbConn, dict):
# SETUP ALL DATABASE CONNECTIONS
dbConn = database(
log=log,
dbSettings=globalDbConn,
autocommit=False
).connect()
else:
dbConn = globalDbConn
count = batch[1]
if count > totalCount:
count = totalCount
ltotalCount = totalCount
inserted = False
while inserted == False:
if not replace:
insertVerb = ""INSERT IGNORE""
else:
insertVerb = ""INSERT IGNORE""
uniKeys = set().union(*(d.keys() for d in batch[0]))
tmp = []
tmp[:] = [m.replace("" "", ""_"").replace(
""-"", ""_"") for m in uniKeys]
uniKeys = tmp
myKeys = '`,`'.join(uniKeys)
vals = [tuple([None if d[k] in [""None"", None] else str(d[k])
for k in uniKeys]) for d in batch[0]]
valueString = (""%s, "" * len(vals[0]))[:-2]
insertCommand = insertVerb + """""" INTO `"""""" + dbTableName + \
""""""` (`"""""" + myKeys + """"""`, dateCreated) VALUES ("""""" + \
valueString + """""", NOW())""""""
if not dateCreated:
insertCommand = insertCommand.replace(
"", dateCreated)"", "")"").replace("", NOW())"", "")"")
dup = """"
if replace:
dup = "" ON DUPLICATE KEY UPDATE ""
for k in uniKeys:
dup = """"""%(dup)s %(k)s=values(%(k)s),"""""" % locals()
dup = """"""%(dup)s updated=1, dateLastModified=NOW()"""""" % locals()
insertCommand = insertCommand + dup
insertCommand = insertCommand.replace('\\""""', '\\"" ""')
insertCommand = insertCommand.replace('""""', ""null"")
insertCommand = insertCommand.replace('""None""', 'null')
message = """"
# log.debug('adding new data to the %s table; query: %s' %
# (dbTableName, addValue))
try:
message = writequery(
log=log,
sqlQuery=insertCommand,
dbConn=dbConn,
Force=True,
manyValueList=vals
)
except:
theseInserts = []
for aDict in batch[0]:
insertCommand, valueTuple = convert_dictionary_to_mysql_table(
dbConn=dbConn,
log=log,
dictionary=aDict,