简体   繁体   中英

Creating/Inserting/Selecting BLOB (binary data) in sqlite3 through Python

This is the current error i'm getting and I just can't find the right syntax.

line 58, in cur.execute("INSERT INTO Results VALUES(?,?,?)', ('scan1','ip1',[lite.Binary(nmap)]") sqlite3.OperationalError: near "', ('": syntax error

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

lite.Binary() has 'nmap' (the filename) as the input parameter.
Shouldn't the input parameter be 'nmapblob' (the actual data)?

Also there are too many quotation marks. There should only be one quotation mark before INSERT and none after 'nmap' at the end of the line.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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