PyMySQL不插入D

2024-10-02 22:35:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段代码,它获取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()

Tags: importeventiddatatypeconnectionevecursor
1条回答
网友
1楼 · 发布于 2024-10-02 22:35:54

我可能大错特错。你知道吗

但这是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))

我猜你的cursor超出了with的范围

相关问题 更多 >