将smallint插入时间pyodbc

时间:2017-07-19 17:48:53

标签: python mysql pyodbc

我正在用pyodbc做一个python脚本,所以现在我遇到了这个麻烦,脚本从一个表中获取一些数据,其中一个数据是一个smaillint类型,它的支持是一个时间所以这个脚本插入这个信息到另一个表但是当它插入这个字段时错误:

类似的东西:

选择日期(SMALLINT):

顺便说一句,我只使用30分钟的间隔:

0
30
130
200
230
1530
1600

当它插入时间类型表时:

00:00:00
00:00:30
00:01:30
00:02:00
00:02:30
00:15:30
00:16:00

它的支持是

    00:00:00
    00:30:00
    01:30:00
    02:00:00
    02:30:00
    15:30:00
    16:00:00

这是我的代码:

        # ------------------------------------
        # Creando Registro de Inicio ****hagent****
        # ------------------------------------

        finicio = datetime.datetime.now()
        valores = (finicio_script, fichero, finicio, ip, ipDestino[cont], tablas[0])
        #print valores #Prueba de valores a insertar
        #Se comienza con la creacion de registro en BDD local
        print "Realizando creacion de Log de Registro"
        cadenaSQL = (
                "INSERT INTO log_scripts_CMS(id_proceso, fichero, fecha_hora_inicio, srv_origen, "
                "srv_destino, tipo_script) VALUES (?, ?, ?, ?, ?, ?)"
                )
        #print cadenaSQL #Prueba de query
        registro_cursor.execute(cadenaSQL, valores)
        #Se empieza con la insercion de datos a las bdd
        print "Insertando datos a", ipDestino[cont]+ " tabla: " + tablas [0]

        cadenaSQL = (
        "INSERT IGNORE INTO "+tablas[0]+"(row_date, **starttime_utc**, intrvl, acd, skill, extension, "
        "logid, loc_id, rsv_level, i_stafftime, ti_stafftime, i_availtime, ti_availtime, i_acdtime, i_acwtime, "
        "i_acwouttime, i_acwintime, ti_auxtime, i_auxouttime, i_auxintime, i_othertime, acwincalls, acwintime, "
        "auxincalls, auxintime, acwoutcalls, acwouttime, acwoutoffcalls, acwoutofftime, acwoutadjcalls, "
        "auxoutcalls, auxouttime, auxoutoffcalls, auxoutofftime, auxoutadjcalls, event1, event2, event3, "
        "event4, event5, event6, event7, event8, event9, assists, acdcalls, acdtime, acwtime, o_acdcalls, "
        "o_acdtime, o_acwtime, da_acdcalls, da_anstime, da_abncalls, da_abntime, holdcalls, holdtime, holdabncalls, "
        "transferred, conference, abncalls, abntime, i_ringtime, i_da_acdtime, i_da_acwtime, da_acdtime, da_acwtime, "
        "da_othercalls, da_othertime, ringcalls, ringtime, ansringtime, ti_othertime, da_acwincalls, da_acwintime, "
        "da_acwocalls, da_acwotime, da_acwoadjcalls, da_acwooffcalls, da_acwoofftime, noansredir, incomplete, "
        "acdauxoutcalls, i_acdaux_outtime, i_acdauxintime, i_acdothertime, phantomabns, i_auxtime, holdacdtime, "
        "da_release, acd_release, ti_auxtime0, ti_auxtime1, ti_auxtime2, ti_auxtime3, ti_auxtime4, ti_auxtime5, "
        "ti_auxtime6, ti_auxtime7, ti_auxtime8, ti_auxtime9, acdcalls_r1, acdcalls_r2, i_otherstbytime, i_auxstbytime"
        ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?"
        ",?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
        "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
        )
        #print  (cadenaSQL) #Comprobaacion de query
        #Se insertan datos en bdd local
        if cuenta_hagent != 0:
            crsr.executemany(cadenaSQL, hagent)
            print "Listo!"
        else:
            print "No hay datos por insertar"

修改

这是我从另一个表中获取数据以将它们保存在另一个数据库中的方法:

# ------------------------------------
    # Obtencion de datos de la tabla hagent
    # ------------------------------------

    tablas_informix = ["hagent", "hsplit", "intradaytramo", "loginout"]
    #Se obtienen los datos.
    print "Recuperando datos de la tabla ", tablas_informix[0]
    #Se obtienen datos de la tabla hagent
    cadenaSQL = (
    "SELECT hagent.row_date AS FECHA, hagent.starttime AS starttime, hagent.intrvl AS intrvl, hagent.acd AS acd, hagent.split AS skill, "
    "hagent.extension, hagent.logid, hagent.loc_id, hagent.rsv_level, hagent.i_stafftime, hagent.ti_stafftime, hagent.i_availtime, "
    "hagent.ti_availtime, hagent.i_acdtime, hagent.i_acwtime, hagent.i_acwouttime, hagent.i_acwintime, hagent.ti_auxtime, hagent.i_auxouttime, "
    "hagent.i_auxintime, hagent.i_othertime, hagent.acwincalls, hagent.acwintime, hagent.auxincalls, hagent.auxintime, hagent.acwoutcalls, "
    "hagent.acwouttime, hagent.acwoutoffcalls, hagent.acwoutofftime, hagent.acwoutadjcalls, hagent.auxoutcalls, hagent.auxouttime, "
    "hagent.auxoutoffcalls, hagent.auxoutofftime, hagent.auxoutadjcalls, hagent.event1, hagent.event2, hagent.event3, hagent.event4, "
    "hagent.event5, hagent.event6, hagent.event7, hagent.event8, hagent.event9, hagent.assists, hagent.acdcalls, hagent.acdtime, "
    "hagent.acwtime, hagent.o_acdcalls, hagent.o_acdtime, hagent.o_acwtime, hagent.da_acdcalls, hagent.da_anstime, hagent.da_abncalls, "
    "hagent.da_abntime, hagent.holdcalls, hagent.holdtime, hagent.holdabncalls, hagent.transferred, hagent.conference, hagent.abncalls, "
    "hagent.abntime, hagent.i_ringtime, hagent.i_da_acdtime, hagent.i_da_acwtime, hagent.da_acdtime, hagent.da_acwtime, hagent.da_othercalls, "
    "hagent.da_othertime, hagent.ringcalls, hagent.ringtime, hagent.ansringtime, hagent.ti_othertime, hagent.da_acwincalls, hagent.da_acwintime, "
    "hagent.da_acwocalls, hagent.da_acwotime, hagent.da_acwoadjcalls, hagent.da_acwooffcalls, hagent.da_acwoofftime, hagent.noansredir, "
    "hagent.incomplete, hagent.acdauxoutcalls, hagent.i_acdaux_outtime, hagent.i_acdauxintime, hagent.i_acdothertime, hagent.phantomabns, "
    "hagent.i_auxtime, hagent.holdacdtime, hagent.da_release, hagent.acd_release, hagent.ti_auxtime0, hagent.ti_auxtime1, hagent.ti_auxtime2, "
    "hagent.ti_auxtime3, hagent.ti_auxtime4, hagent.ti_auxtime5, hagent.ti_auxtime6, hagent.ti_auxtime7, hagent.ti_auxtime8, hagent.ti_auxtime9, "
    "hagent.acdcalls_r1, hagent.acdcalls_r2, hagent.i_otherstbytime, hagent.i_auxstbytime FROM root.hagent "
    "WHERE(hagent.row_date = '"+fecha_consulta+"') AND hagent.acd = '"+str(acd[0])+"' AND hagent.split IN ("+condicionIn+") AND starttime = "+hora_query+" "
    "ORDER BY starttime;"
    )
    #Se inicializa cursor.
    cursor = cnxn.cursor()
    cursor.execute(cadenaSQL)
    hagent = []
    hagent = cursor.fetchall()
    print "Listo!"
    #print cadenaSQL
    cnxn.commit()

1 个答案:

答案 0 :(得分:1)

只需将SMALLINT值乘以100.这对我有用:

x = 130
crsr.execute("INSERT INTO table1 (timecol) VALUES (?)", x * 100)
# value inserted is 01:30:00