背景:工作需要大量链接数据库进行一些操作查询,但是也会有出现异常情况
1.添加字段
1 ALTER TABLE app01_student ADD COLUMN Relation VARCHAR(255) DEFAULT'无';
2 、D:\Users\zhang.ning\PycharmProjects\APP>python manage.py startapp app01
2.查询操作
import pymysql
sql = "SELECT * FROM userinfo "
def Sql_Query():
try:
connection = pymysql.connect(host='127.0.0.1',
user='root',
password='123456',
db='test',
charset='utf8',
port=3306
)
except Exception as e:
print(e)
exit()
try:
with connection.cursor() as cursor:
cursor.execute(sql)
result = cursor.fetchall()
except Exception as e:
print(e)
return False, None
else:
return True, result
finally:
connection.close()
if __name__ == '__main__':
flag, rs = Sql_Query()
if flag:
print(rs)
else:
print("NO")