python3.6使用mysql
2017-04-30 14:48
218 查看
因为MySQLdb不支持python3,需要导入pymysql
可以选择pip 安装pymysql,或者pycharm安装
[File] >> [settings] >> [Project: python] >> [Project Interpreter] >> "+"
可以选择pip 安装pymysql,或者pycharm安装
[File] >> [settings] >> [Project: python] >> [Project Interpreter] >> "+"
import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='localhost', port=3310, user='root', passwd='root', db='python', charset='utf8' ) # 获取游标 cursor = connect.cursor() # 插入数据 sql = "INSERT INTO trade (name, account, saving) VALUES ( '%s', '%s', %.2f )" data = ('雷军', '13512345678', 10000) cursor.execute(sql % data) connect.commit() print('成功插入', cursor.rowcount, '条数据') # 修改数据 sql = "UPDATE trade SET saving = %.2f WHERE account = '%s' " data = (8888, '13512345678') cursor.execute(sql % data) connect.commit() print('成功修改', cursor.rowcount, '条数据') # 查询数据 sql = "SELECT name,saving FROM trade WHERE account = '%s' " data = ('13512345678',) cursor.execute(sql % data) for row in cursor.fetchall(): print("Name:%s\tSaving:%.2f" % row) print('共查找出', cursor.rowcount, '条数据') # 删除数据 sql = "DELETE FROM trade WHERE account = '%s' LIMIT %d" data = ('13512345678', 1) cursor.execute(sql % data) connect.commit() print('成功删除', cursor.rowcount, '条数据') # 事务处理 sql_1 = "UPDATE trade SET saving = saving + 1000 WHERE account = '18012345678' " sql_2 = "UPDATE trade SET expend = expend + 1000 WHERE account = '18012345678' " sql_3 = "UPDATE trade SET income = income + 2000 WHERE account = '18012345678' " try: cursor.execute(sql_1) # 储蓄增加1000 cursor.execute(sql_2) # 支出增加1000 cursor.execute(sql_3) # 收入增加2000 except Exception as e: connect.rollback() # 事务回滚 print('事务处理失败', e) else: connect.commit() # 事务提交 print('事务处理成功', cursor.rowcount) # 关闭连接 cursor.close() connect.close()
def insert_data(news_list): try: connect = get_connect() cursor = connect.cursor() for news in news_list: sql = "INSERT INTO news(title, tag, source, source_url, keyword, keywords) VALUES ( %s,%s,%s,%s,%s,%s)" #data = {news.title, news.tag, news.source, news.source_url, news.keyword, news.keywords} cursor.execute(sql, (news.title, news.tag, news.source, news.source_url, news.keyword, news.keywords)) connect.commit() except Exception as e: print(e) finally: cursor.close() connect.close()
def select_url(): arrList = [] try: connect = get_connect() cursor = connect.cursor() print("connection") sql = "SELECT id,source_url FROM news" cursor.execute(sql) result = cursor.fetchall() for row in result: # print(row[0]) news = News() news.source_url = row[1] arrList.append(news) except Exception as e: print(e) finally: return arrList
相关文章推荐
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- windows10 下使用Pycharm2016 基于Anaconda3 Python3.6 安装Mysql驱动总结
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- python(二)mac环境(macOS10.12.6)下的python3.6使用mysql
- python中使用MySQLdb连接mysql
- linux环境下的mysql-python使用注意事项
- SQLALchemy中使用中文作为MySQL的列名(Python 2.7)
- Python使用MySQLdb创建、调用MySQL存储过程
- Python中使用MySQL的问题
- python2.7中使用mysql (windows XP)
- python mysql使用持久链接
- 关于MySQLdb的一些新发现(使用python连接mysql的库)
- python中使用MYSQL
- 使用Python定时更新Mysql数据
- Ubuntu下使用Python+flask+MySQL的架构搭建Web框架
- python访问mysql 连接mysql - 使用mysql connector
- 使用python读取csv文件,并将数据更新至mysql
- python2.6使用mysql包出现的一些问题以及解决办法
- python中使用mysql(windows xp下)
- python mysql安装使用