通过Python在sqlite3中创建/插入/选择BLOB(二进制数据)

时间:2014-11-19 03:58:20

标签: python sqlite binary blob

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

第58行,in     cur.execute(“INSERT INTO Results VALUES(?,?,?)',('scan1','ip1',[lite.Binary(nmap)]”) sqlite3.OperationalError:near“',('”:语法错误

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

1 个答案:

答案 0 :(得分:1)

lite.Binary()有&#39; nmap&#39; (文件名)作为输入参数 输入参数不应该是&#39; nmapblob&#39; (实际数据)?

也有太多的引号。在INSERT之前只应该有一个引号,而在&nbsp;&nbsp;&nbsp;在最后一行。

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