简体   繁体   中英

How to do an insert in the database with Python

I have a Python script that does the Moodle version check. After checking the Moodle version I would like to insert into the database but I am getting the following error:

line 337
mycursor = db.cursor()
TabError: inconsistent use of tabs and spaces in indentation

This is the code that using and with problems:

import import mysql.connector

...

def printversion(version):
if version != 0:
    print ("\nVersion found via " + version.split(';')[2] + " : Moodle " +  version.split(';')[0])
    vuln = version.split(';')[0]
    db = mysql.connector.connect(host="localhost", user="admin", passwd="", database="test")
    mycursor = db.cursor() 
    insertQuery = """INSERT INTO moodle (id,payload) VALUES (%s,%s)"""
    mycursor.execute(insertQuery, ('',vuln))
    db.commit()  
    db.close()
    return version.split(';')[0].replace("v","")
    
print ("\nVersion not found")
return False

The error reported is about code indentation, but I don't know how to solve this problem to insert versioning results in the database.

You have a slight misunderstanding here, in any function in python you need to provide an indentation(tab) before you start writing any code,

ie structure is

def your_functionname(parameters)
    < you code here >

So, in a correct way, your code should look like this

...

def printversion(version):
    if version != 0:
        print ("\nVersion found via " + version.split(';')[2] + " : Moodle " +  
        version.split(';')[0])
        vuln = version.split(';')[0]
        db = mysql.connector.connect(host="localhost", user="admin", passwd="", 
        database="test")
        mycursor = db.cursor() 
        insertQuery = """INSERT INTO moodle (id,payload) VALUES (%s,%s)"""
        mycursor.execute(insertQuery, ('',vuln))
        db.commit()  
        db.close()
        return version.split(';')[0].replace("v","")
        
    print ("\nVersion not found")
    return False

Your code requires the section after the function definition to be indented.

So instead of this,

def printversion(version):
if version != 0:
    print ("\nVersion found via " + version.split(';')[2] + " : Moodle " +  version.split(';')[0])
    vuln = version.split(';')[0]
    db = mysql.connector.connect(host="localhost", user="admin", passwd="", database="test")
    mycursor = db.cursor() 
    insertQuery = """INSERT INTO moodle (id,payload) VALUES (%s,%s)"""
    mycursor.execute(insertQuery, ('',vuln))
    db.commit()  
    db.close()
    return version.split(';')[0].replace("v","")

You should have this,

def printversion(version):
    if version != 0:
        print ("\nVersion found via " + version.split(';')[2] + " : Moodle " +  version.split(';')[0])
        vuln = version.split(';')[0]
        db = mysql.connector.connect(host="localhost", user="admin", passwd="", database="test")
        mycursor = db.cursor() 
        insertQuery = """INSERT INTO moodle (id,payload) VALUES (%s,%s)"""
        mycursor.execute(insertQuery, ('',vuln))
        db.commit()  
        db.close()
        return version.split(';')[0].replace("v","")

I don't know the intention of your code, but looks like you might want to include an else statement as well,

    else:
        print ("\nVersion not found")
        return False

Just tab the line if version:= 0:

Ident only this line

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