python_MySQL
原文章连接:http://www.runoob.com/python/python-mysql.html
配置数据库
conn = mysql.connector.connect(user='root', password='数据库密码', database='数据库名')
cursor = conn.cursor()
#如果表存,执行如下操作
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
创建表
sql = """ create table if not exists myUserInfo (
id varchar(99),
author varchar (20),
title varchar (20),
content varchar (9999)
)"""
数据库的插入操作:
#插入一条数据
sql = """insert into myUserInfo(id, author, title, content) values ('2', 'sqz', '文章的标题2', '文章的内容2')""" try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
conn.commit()
except:
# 如果发生错误则回滚
conn.rollback()
#关闭数据库
conn.close()
接口带参数插入数据
@app.route('/register/', methods=['GET', "POST"])
def register():
uid = 0
userName = request.values.get('userName')
passWord = request.values.get('passWord')
tel = request.values.get('tel')
print(uid, userName, passWord)
lin1 = cursor.rowcount
cursor.execute('insert into myUserInfo values ("%d", "%s", "%s", "%s")' % (uid, userName, passWord, tel))
# 执行sql语句
conn.commit()
接口带参数查询数据
@app.route('/login/', methods=['GET', 'POST'])
def login():
_userName = request.values.get('userName')
_passWord = request.values.get('passWord')
sql = "select * from myUserInfo where userName= '%s' and passWord='%s'" % (_userName, _passWord)
cursor.execute(sql)
results = cursor.fetchall()
if results:
return json.dumps({'resCode':''})
else:
return json.dumps({'resCode':'', 'errorCode':'无此用户'})
conn.close()
接口带参更新数据
@app.route('/update_passWord/', methods=['GET', 'POST'])
def modify_password():
_userName = request.values.get('userName')
_passWord = request.values.get('passWord')
_newPassword = request.values.get('newPassword')
sql = "select * from myUserInfo where userName= '%s' and passWord='%s'" % (_userName, _passWord)
cursor.execute(sql)
results = cursor.fetchall()
if results:
#更新此用户的信息
sql = "update myUserInfo set passWord='%s' where userName='%s'" % (_newPassword, _userName)
cursor.execute(sql)
conn.commit()
else:
return json.dumps({'resCode':'', 'errorCode':'请重新输入'})
conn.close()
删除操作我觉得危险,最好不要使用,后面在更新吧
python_MySQL的更多相关文章
- python_MySQL 数据库操作
Python中的mysql操作可以使用MySQLdb模块来完成.它符合Python社区设计的Python Database API SpecificationV2.0标准,所以与其他的数据库操作的AP ...
- Python_MySQL数据库的写入与读取
[需求]1. 在数据库中创建表,且能按时间自动创建新表 2. 数据写入数据库 3. 从数据库读取数据 1. 创建表,并自动更新 def Creat_Table(InitMySQL,tabel_name ...
- python_MySQL数据库
MySQL数据库的特点: 1.是关系型数据库 关系型数据库的特点 1.数据是以行和列的的形式存储的 2.这一系列的行和列称为表 ...
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...
- LightMysql:为方便操作MySQL而封装的Python类
原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...
- SQLAlchemy一对多总结
1.SQLAlchemy之一对多关系 1.1 创建单表 class Test(Base): __tablename__ = 'user' nid = Colume(Integer,primary_ke ...
- Python 第九篇:队列Queue、生产者消费者模型、(IO/异步IP/Select/Poll/Epool)、Mysql操作
Mysql操作: grant select,insert,update,delete on *.* to root@"%" Identified by "123456&q ...
- 通过demo学python
链接 Github项目地址 软件安装包(pycharm.注册码.解析器等) Python 一切皆对象 Python 编码规范 The Python Standard Library The Pytho ...
- MySQL状态变量Aborted_connects与Aborted_clients浅析
关于MySQL的状态变量Aborted_clients & Aborted_connects分别代表的意义,以及哪些情况或因素会导致这些状态变量变化呢?下文通过实验测试来验证一下,首先我们来看 ...
随机推荐
- bash计算上下行数据差值
for i in {1..60000}; do echo "`date +'%F %T'` `df /dev/md0 | grep 'data1'` "; sleep 1; don ...
- SAP Parallel Accounting(平行分类账)业务配置及操作手册
目录 SAP Parallel Accounting(平行分类账业务)配置及操作手册 SAP Parallel Accounting(平行分类账业务)配置及操作手册 Overview 业务说明 为了适 ...
- 第四百一十五节,python常用排序算法学习
第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...
- ubuntu 下无损扩展分区
命令扩展: http://www.cnblogs.com/greatfish/p/7347945.html http://www.cnblogs.com/wangxingggg/articles/68 ...
- idea中git颜色不显示或者文件右键没有git按钮解决方法
VCS--->Enable Version Control Integration,然后选择git就可以了
- hibernate 映射一对多
参考笔记: https://www.cnblogs.com/biehongli/p/6561690.html
- modified: xxx(modified content, untracked content)
当运行git status的时候提示如下: modified: xxx(modified content, untracked content) 我们会很本能的直接执行 add .commit .pu ...
- [破解] IPhone 5S icloud dns bypass
http://ui.iclouddnsbypass.com/deviceservices/buddy/barney_activation_help_en_us.buddyml http://www.j ...
- python __setattr__、__getattr__、__getattribute__全面详解
一.属性引用函数 hasattr(obj,name[,default])getattr(obj,name)setattr(obj,name,value)delattr(obj,name) 二.属性引用 ...
- Water Buying
Water Buying time limit per test 1 second memory limit per test 256 megabytes input standard input o ...