pymysql不插入数据

jv4diomz  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(596)

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

5cg8jx4n1#

我可能大错特错。
但这是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 范围

相关问题