您的位置:首页 > 数据库 > MySQL

操作MySql

2015-07-31 23:32 260 查看
1.使用pyMySQL操作数据库,下载

点击打开链接

2.解压后在跟目录

py setup.py install

重启一下py shell

3.例子

import pymysql.cursors
connection = pymysql.connect(host='localhost',user='root',
password='111111',db='pyDB',
charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)

try:
# with connection.cursor() as cursor:
#     sql = "insert into users(email,password) values(%s,%s)"
#     cursor.execute(sql,('webmaster@python.org','very-secret'))
#     connection.commit()
with connection.cursor() as cursor:
sql = 'SELECT * FROM USERS U WHERE U.ID > 0'
cursor.execute(sql)
result = cursor.fetchone()
print(result)
finally:
connection.close()


参考资料:

Resources

DB-API 2.0: http://www.python.org/dev/peps/pep-0249
MySQL Reference Manuals: http://dev.mysql.com/doc/

MySQL client/server protocol: http://dev.mysql.com/doc/internals/en/client-server-protocol.html
PyMySQL mailing list: https://groups.google.com/forum/#!forum/pymysql-users

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: