查询MySQL中的大量数据

时间:2012-07-17 20:19:01

标签: python mysql

我正从我的服务器检索所有传入的用户IP地址,我已检索到地址并将其写入文件。我还从文件中过滤了ip地址,并将它们检索到变量ip。但是,当我无法将ip地址上传到mysql数据库时。

到目前为止,我有这个,但它不起作用。

with open("netstat.txt", 'rb') as f:
    for row in f.readlines()[1:]:
        columns = row.split()
        if len(columns) > 2:
            ip = row.split()[1]
            user = row.split()[2]
            ip = ip.strip('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
            con = mdb.connect('server', 'user', 'pass', 'db');

            with con:
                cur = con.cursor()
                cur.executemany("INSERT INTO Juliet_Data(ip,user) VALUES,(%s,%s)" % ip,user)

我将表格设置为以下内容:

user = longtext
ip = blob

错误:

    Traceback (most recent call last):
   File "C:\Documents and Settings\finance\Desktop\Server 
Monitoring\System Load Tracker\User Tracking\netstat.py", line 32, in 
<module>
     cur.execute("INSERT INTO Juliet_Data(ip) VALUES,(%s)" % ip)
   File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in 
execute
     self.errorhandler(self, exc, value)
   File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, 
in defaulterrorhandler
     raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your 
SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near '()' at line 1")

示例IP地址仅查看IP和USer:

 0.0.0.0:22             newjulietleft:0        LISTENING
   TCP    0.0.0.0:135            newjulietleft:0        LISTENING
   TCP    0.0.0.0:445            newjulietleft:0        LISTENING
   TCP    0.0.0.0:1311           newjulietleft:0        LISTENING
   TCP    0.0.0.0:3306           newjulietleft:0        LISTENING
   TCP    0.0.0.0:3389           newjulietleft:0        LISTENING
   TCP    0.0.0.0:5800           newjulietleft:0        LISTENING
   TCP    0.0.0.0:5900           newjulietleft:0        LISTENING
   TCP    0.0.0.0:47001          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49152          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49153          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49154          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49158          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49170          newjulietleft:0        LISTENING
   TCP    10.22.22.22:139        newjulietleft:0        LISTENING
   TCP    10.22.22.22:3389       gregk:17415            ESTABLISHED
   TCP    10.22.22.22:49156      superman:microsoft-ds  ESTABLISHED
   TCP    10.22.22.22:49160      juliet:microsoft-ds    ESTABLISHED
   TCP    10.22.22.22:49164      pong:microsoft-ds      ESTABLISHED
   TCP    10.22.22.22:49165      ftpserve:microsoft-ds  ESTABLISHED
   TCP    10.22.22.22:50376      dbmlog:3306            ESTABLISHED
   TCP    10.22.22.22:50377      hulk:microsoft-ds      ESTABLISHED
   TCP    10.22.22.22:50383      julietz:netbios-ssn    TIME_WAIT
   TCP    10.22.22.22:50384    

2 个答案:

答案 0 :(得分:3)

,之后的VALUES,无效

当您收到语法错误时,您只需要查看错误指向的位置。这个错误可能令人困惑,因为它不会在你的脚本中提供行,而是在你执行的第一个statmeent中的mysql中,这是你的insert语句。

您的评论中的第二个问题:

你应该使用参数化,如:

VALUES (?, ?), (ip, user)

应该有效

答案 1 :(得分:0)

cur.executemany("INSERT INTO Julient_Data (ip,user) VALUES (%s,%s)", ((ip, user),)) 

cur.executemany("INSERT INTO Julient_Data (ip,user) VALUES (%s,%s)", ((ip, user),('192.168.1.1', 'test1')))