cursor=cnn.cursor() try: sql_query='select id,name from student where age > %s' cursor.execute(sql_query,(21,)) forid,name in cursor: print('%s\'s age is older than 21,and her/his id is %d'%(name,id)) except mysql.connector.Error as e: print('query error!{}'.format(e)) finally: cursor.close() cnn.close()
6. 删除操作
1 2 3 4 5 6 7 8 9 10
cursor=cnn.cursor() try: sql_delete='delete from student where name = %(name)s and age < %(age)s' data={'name':'orange','age':24} cursor.execute(sql_delete,data) except mysql.connector.Error as e: print('delete error!{}'.format(e)) finally: cursor.close() cnn.close()