如何解决树莓中的这些错误?

时间:2019-02-22 07:26:40

标签: python python-3.x raspberry-pi

def insertorupdate(Id,Name,Email):
    conn=sqlite3.connect("faces.db")
    cmd="SELECT * FROM Employee WHERE Id="+str(Id)
    c = conn.execute(cmd)
    isRecordExist=0
    for row in c:
        isRecordExist=1

        if(isRecordExist==1):
            cmd="UPDATE Employee Set Name="+str(Name)+ "Email= "+ str(Email) + "WHERE Id="+str(Id)
        else:
            cmd="INSERT INTO Employee(Id,Name,Email) Values(" +str(Id)+" ,"+str(Name)+ " ,"+ str(Email)+")"

        print(cmd)
        conn.execute(cmd)
        conn.commit()
        conn.close()

id = input('\n enter user id end press <return> ==>  ')
name = input('\n enter user name end press <return> ==>  ')
email = input ('\n enter user email end press <return> ==>  ')
insertorupdate(id,name,email)

跟踪:

  File "/home/pi/Desktop/project2/facerecognition.py", line 35, in <module>
      insertorupdate(id,name,email)
  File "/home/pi/Desktop/project2/facerecognition.py", line 28, in insertorupdate
      cursor = conn.execute(cmd)
      sqlite3.OperationalError: near "Email": syntax error

1 个答案:

答案 0 :(得分:0)

,查询中传递email之前,必须先UPDATE固定缩进,查询格式。

def insertorupdate(id,name,email):
    conn=sqlite3.connect("faces.db")
    cmd="SELECT * FROM Employee WHERE Id=" +str(id)
    c = conn.execute(cmd)
    isRecordExist=0
    for row in c:
        isRecordExist=1
        if(isRecordExist==1):
            cmd="UPDATE Employee SET Name=" +str(name)+ " Email= " +str(email)+ "WHERE Id=" + str(id)
        else:
            cmd="INSERT INTO Employee(Id,Name,Email) Values(" +str(id)+" ,"+str(name)+ " ,"+ str(email)+")"


print(cmd)
conn.execute(cmd)
conn.commit()
conn.close()



id = input('\n enter user id end press <return> ==>  ')
name = input('\n enter user name end press <return> ==>  ')
email = input ('\n enter user email end press <return> ==>  ')

insertorupdate(id,name,email)

编辑:

考虑使用

data = c.fetchall()
   if len(data) > 1:

代替

isRecordExist=0
    for row in c:
        isRecordExist=1
        if(isRecordExist==1):
相关问题