简体   繁体   English

在MySQL中查询大量数据

[英]Querying Huge Data in MySQL

I am retrieving all incoming user IP adress from my server, I have retrieved the adresses and wrote them to a file. 我正在从服务器中检索所有传入的用户IP地址,我已经检索了地址并将其写入文件。 I have also filtered the ip adresses from the file and retrieved them to a variable, ip. 我还从文件中过滤了IP地址,并将它们检索到变量ip。 However, when I am unable to upload the ip adresses to the mysql database. 但是,当我无法将ip地址上传到mysql数据库时。

So far I have this but it does not work. 到目前为止,我有这个功能,但是没有用。

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)

I have the tables set to the following: 我将表设置为以下内容:

user = longtext
ip = blob

Error Given: 错误提示:

    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")

Sample IP Adresses Look at Just the IP and the USer: IP地址示例仅考察IP和用户:

 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    

the , from after VALUES, is invalid ,位于VALUES,之后VALUES,无效

When you receive a syntax error you only need to look at where the error points to. 收到语法错误时,您只需查看错误指向的位置。 This error can be confusing because it doesn't give you the line in your script but instead inside mysql at the first statmeent you execute which is your insert statement. 这个错误可能会令人困惑,因为它没有给您脚本中的代码,而是在您执行的第一个状态(即插入语句)的mysql内部。

For your second issue in your comment: 对于您评论中的第二个问题:

you should be using parameterization so something like: 您应该使用参数化,例如:

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

should work 应该管用

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')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM