how can I fix the error [closed]

2020-05-06 10:26发布

问题:

I'm novice here and I have my code, there is running good before I create my SQL query. I use sqlite3 as database.

that's my code :

code.py

print """<INPUT  Value="Valider" Type="SUBMIT" >

<head>
   <script type="text/javascript">

   {

   alert('thank you.');

   }

   </script>
   </head>"""

print "</body>"
print "</html>"

conn = sqlite3.connect('database.sqlite')
conn.row_factory = sqlite3.Row 
c = conn.cursor()

sql = "INSERT INTO info_calc (my_argv[2], my_argv[3], my_argv[4], sys.platform, getpass.getuser(), machine ) VALUES (%s, %s, %s, %s, %s, %s)

try:
    c.execute(sql)
    conn.commit()
except:
     conn.rollback()

cursor.close()

when I execute my code, I have this error :

sql = "INSERT INTO info_calc (my_argv[2], my_argv[3], my_argv[4], sys.platform, getpass.getuser(), machine ) VALUES (%s, %s, %s, %s, %s, %s)
                                                                                                                                               ^
SyntaxError: EOL while scanning string literal

回答1:

You forgot the quote at the end of the string.

Also, the INSERT statement wants a list of column names; my_argv[2] is not a column name.

Furthermore, %s is not a database parameter marker; use ? instead.

Additionally, getpass is a module; you cannot call it.

sql = "INSERT INTO info_calc (application,version,path,os,user,ip) "+
      "VALUES (?,?,?,?,?,?)"
args = my_argv[2], my_argv[3], my_argv[4], sys.platform, getpass.getuser(), machine
c.execute(sql, args)


回答2:

you forget to keep " it should be like

sql = "INSERT INTO info_calc (application,version,path,os,user,ip) VALUES (%s, %s, %s, %s, %s, %s)"

one more thing,while executing the query insert you need to give values

it should be

c.execute(sql,(my_argv[2], my_argv[3], my_argv[4], sys.platform, getpass.getuser(), machine ))