Python PyMySQL不插入数据

Python PyMySQL不插入数据,python,mysql,syntax,pymysql,Python,Mysql,Syntax,Pymysql,我有一段代码,它获取Windows日志并将各种信息插入mySQL数据库。代码运行正常,没有错误,但实际上并没有将数据输入到表中。这张表还是空白的。我从一个示例中提取了我的mySQL语法,并做了一些修改,所以我不完全确定到底出了什么问题。我觉得这要么与数据类型有关,要么与我对语法所做的一些更改有关 import sys import pymysql import pymysql.cursors import win32evtlog # requires pywin32 pre-installed

我有一段代码,它获取Windows日志并将各种信息插入mySQL数据库。代码运行正常,没有错误,但实际上并没有将数据输入到表中。这张表还是空白的。我从一个示例中提取了我的mySQL语法,并做了一些修改,所以我不完全确定到底出了什么问题。我觉得这要么与数据类型有关,要么与我对语法所做的一些更改有关

import sys
import pymysql
import pymysql.cursors
import win32evtlog # requires pywin32 pre-installed
import win32evtlogutil 
import time


server = 'localhost' # name of the target computer to get event logs
logtype = 'System' # 'Application' # 'Security'
hand = win32evtlog.OpenEventLog(server,logtype)
flags = 
win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
events = win32evtlog.ReadEventLog(hand, flags,0)

while True:
for event in events:
    evt_tp = event.EventType
    if evt_tp != (1 or 2 or 8):
            eve_cat = str(('Event Category:', event.EventCategory))
            eve_timegen = str(('Time Generated:', event.TimeGenerated))
            eve_srcnm =  str(('Source Name:', event.SourceName))
            eve_id = str(('Event ID:', event.EventID))
            eve_typ =  str(('Event Type:', event.EventType))
            data = event.StringInserts
            if data:
                print ('Event Data:')
                for msg in data:
                    print(msg)

            print(type(eve_cat))
            print(type(eve_timegen))
            print(type(eve_srcnm))
            print(type(eve_id))
            print(type(eve_typ))
            print(type(data))
            time.sleep(10)

    else:
        eve_cat = ('Event Category:', event.EventCategory)
        eve_timegen = ('Time Generated:', event.TimeGenerated)
        eve_srcnm =  ('Source Name:', event.SourceName)
        eve_id = ('Event ID:', event.EventID)
        eve_typ =  ('Event Type:', event.EventType)
        data = event.StringInserts
        print('There were no errors found')

        print(eve_cat)
        print(eve_timegen)
        print(eve_srcnm)
        print(eve_id)
        print(eve_typ)
        print(data)
        time.sleep(10)


# Connect to the database
connection = pymysql.connect(host='localhost',
                         user='root',
                         password='',
                         db='ptest',
                         charset='utf8mb4',
                         cursorclass=pymysql.cursors.DictCursor)



try:
    with connection.cursor() as cursor:
    # Create a new record
        sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName', 
'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
    cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))

# connection is not autocommit by default. So you must commit to save
# your changes.

connection.commit()

with connection.cursor() as cursor:
    # Read a single record
    sql = "SELECT `id`, `Type` FROM `win_logs` WHERE `Category`=%s"
    cursor.execute(sql, ('webmaster@python.org',))
    result = cursor.fetchone()
    print(result)
finally:
connection.close()
我可能大错特错了

但这是python

这很重要

试试看:

try:
    with connection.cursor() as cursor:
    # Create a new record
        sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName`, 'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
        cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))
我猜你的
光标
超出了
的范围
我可能会大错特错

但这是python

这很重要

试试看:

try:
    with connection.cursor() as cursor:
    # Create a new record
        sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName`, 'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
        cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))

我猜你的
光标
超出了
的范围
缺少提交?可能是循环无限运行而没有点击connection.commit()?@Travis我相信就是这样。我在没有运行测试数据库的情况下运行了代码,仍然没有收到任何错误。我将打破循环,看看我的数据是否插入。@Benjaminphris尝试迭代一次,看看它是如何运行的,一次评级正是需要发生的事情。现在我遇到了一个很好的mySQL错误块,但这肯定是一个进步。我们必须解决这些问题。谢谢你帮我解决了这个愚蠢的错误@Travismissing commit?可能是循环无限运行而没有点击connection.commit()?@Travis我相信就是这样。我在没有运行测试数据库的情况下运行了代码,仍然没有收到任何错误。我将打破循环,看看我的数据是否插入。@Benjaminphris尝试迭代一次,看看它是如何运行的,一次评级正是需要发生的事情。现在我遇到了一个很好的mySQL错误块,但这肯定是一个进步。我们必须解决这些问题。谢谢你帮我解决了我的愚蠢错误@Travis