Python易错点总结

  1. Python没有代码块作用域。
    在这里插入图片描述
    C/C++语言有代码块作用域。
    在这里插入图片描述
    Java也没有代码块作用域。
    在这里插入图片描述

  2. 字符串的format方法最终结果为字符串。但是假如是使用Python对MySQL插入数据,而且输入的数据包括字符串,则SQL命令应该是string of string。解决方案如下所示:

if create_date[0] != '\'':
    create_date = '\'' + create_date

if create_date[-1] != '\'':
    create_date = create_date  + '\'' 

sql = "INSERT INTO  mysql_table (id,  age, create_date) VALUES ({}, {}, {});"
sql.format(id, age, create_date)
cursor.execute(sql%data)
connect.commit()

猜你喜欢

转载自blog.csdn.net/herosunly/article/details/105766631