【问题标题】:Creating/Inserting/Selecting BLOB (binary data) in sqlite3 through Python通过 Python 在 sqlite3 中创建/插入/选择 BLOB(二进制数据)
【发布时间】:2015-01-16 11:46:38
【问题描述】:

这是我遇到的当前错误,我只是找不到正确的语法。

第 58 行,在 cur.execute("INSERT INTO 结果值(?,?,?)', ('scan1','ip1',[lite.Binary(nmap)]") sqlite3.OperationalError: 附近 "', ('": 语法错误

CODE BELOW


    #!usr/bin/python
    import xml.etree.ElementTree as ETree
    import sqlite3 as lite
    import time

    #
    localtime = time.localtime()
    print  ############################################################################
    print  ###                                                                      ###
    print  ###     Current Time: "localtime"                                        ###
    print  ###     IS 501 Project by Ross Hickey                                    ###
    print  ###     PyCode (IS 501) is designed to take exports from Nessus,         ###
    print  ###     Retina, and Nmap then import the xml outputs into SQLite3        ###
    print  ###     using Python code. From there the code will take the database    ###
    print  ###     files and display them in a readable HTML format.                ###
    print  ###                                                                      ###
    print  ############################################################################

################################################################################
#
#   Creating database
#
################################################################################


con = lite.connect('HickeyIS501.db');
cur = con.cursor()

################################################################################
#
#   Table Creation
#
################################################################################


with con:
    cur.execute('''CREATE TABLE Results (scanner TEXT, ipaddr TEXT, file BLOB)''')



################################################################################
#
#   Parsing the XML
#   BLOB data using
#   ElementTree
#
################################################################################

nmap = "nmap-results.xml"
nessus = "nessus-results.nessus"
retina = "retina-results.xml"

#NMAP
with open(nmap, "rb") as nmapdata:
    nmapblob = nmapdata.read()
    timestamp = time.time()

cur.execute('INSERT INTO Results VALUES(?,?,?)',('scan1','ip1',lite.Binary(nmapblob))
cur.execute('SELECT * FROM Results')
#print cur.fetchall()

#NESSUS
with open(nessus, "rb") as nessusdata:
    nessusblob = nessusdata.read()
    timestamp = time.time()

cur.execute('INSERT INTO Results VALUES(?,?,?)',('scan1','ip1',lite.Binary(nessusblob))
cur.execute('SELECT * FROM Results')
#print cur.fetchall()

#RETINA
with open(retina, "rb") as retinadata:
    retinablob = retinadata.read()
    timestamp = time.time()

cur.execute('INSERT INTO Results VALUES(?,?,?)',('scan1','ip1',lite.Binary(retinablob))
cur.execute('SELECT * FROM Results')
#print cur.fetchall()



    ############################################################################
    #
    #   Building the HTML tables
    #
    ############################################################################

    with open('Results.html','wb') as logitnow:
        logitnow.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
        logitnow.write("<html xmlns=\"http://www.HickeyIS501.html\">\n")
        logitnow.write("<head>\n")
        logitnow.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n")
        logitnow.write("<title>Scan Results</title>\n")
        logitnow.write("<style type=\"text/css\">\n")
        logitnow.write("    ul.menu { \n")
        logitnow.write("        list-style:none; /* remove bullets */ \n")
        logitnow.write("        background:#999; \n")
        logitnow.write("        overflow:auto; /* span the width of the container */ \n")
        logitnow.write("        padding:0; margin:0; /* reset browser defaults */ \n")
        logitnow.write("        height:100%; /* ie6 fix */ \n")
        logitnow.write("        } \n")
        logitnow.write("    ul.menu li { \n")
        logitnow.write("        display:inline; /* line-up the list items horizontally */ \n")
        logitnow.write("        padding:0; margin:0; /* again, reset browser defaults */ \n")
        logitnow.write("        } \n")
        logitnow.write("    ul.menu li a { \n")
        logitnow.write("        padding:10px 20px; \n")
        logitnow.write("        background:#999; \n")
        logitnow.write("        color:#fff; \n")
        logitnow.write("        text-decoration:none; \n")
        logitnow.write("        display:block; /* block it baby! */ \n")
        logitnow.write("        float:left; /* float each list item to the left */ \n")
        logitnow.write("        } \n")
        logitnow.write("    ul.menu li a:hover { \n")
        logitnow.write("        background:#777; \n")
        logitnow.write("        } \n")
        logitnow.write("</style>\n")
        logitnow.write("</head>\n")
        logitnow.write("<body>\n")
        logitnow.write("   <ul class=\"menu\"> \n")
        logitnow.write("        <li><a href=\"./Nmap_Results.html\">Nmap</a></li> \n")
        logitnow.write("        <li><a href=\"./Nessus_Results.html\">Nessus</a></li> \n")
        logitnow.write("        <li><a href=\"./Retina_Results.html\">Retina</a></li> \n")
        logitnow.write("    </ul>\n")

    with open("output.html", "wb") as out:
        out.write("<DOCTYPE>\n")
        out.write("<html>\n")
        out.write("<p>Hello World</p>\n")
        out.write("</html>\n")

【问题讨论】:

    标签: python sqlite binary blob


    【解决方案1】:

    lite.Binary() 将“nmap”(文件名)作为输入参数。
    输入参数不应该是'nmapblob'(实际数据)吗?

    还有太多的引号。在 INSERT 之前应该只有一个引号,在行尾的 'nmap' 之后应该没有。

    cur.execute('INSERT INTO Results VALUES (?,?,?)', ('scan1','ip1',lite.Binary(nmapblob)))
    

    【讨论】:

    • 我已经更新了代码,但现在仍然失败,出现语法错误 cur.execute('SELECT * FROM Results') ^ SyntaxError: invalid syntax
    • nmapblob 之后应该有 3 个右括号——这会导致语法错误。我的编辑也有语法问题。我希望这个版本更适合你。
    猜你喜欢
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2014-04-15
    • 1970-01-01
    • 2019-11-29
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多