簡體   English   中英

PyMySQL沒有插入數據

[英]PyMySQL not Inserting Data

我有一段代碼可以獲取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))

我猜你的cursor超出with范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM