pymysql

用pymysql和Flask搭建后端,响应前端POST和GET请求,实现登录和注册功能

大兔子大兔子 提交于 2020-08-10 21:21:57
前言 这次作业不仅需要我 建立一个数据库(详情请点击这里) ,还需要我基于这个数据库写后端接口(注册和登录)供前端访问,接收前端的POST和GET请求,并将登录、注册是否成功传给前端。 本文介绍如何用Flask搭建后端,其中使用了pymysql操作mysql数据库,也会做这个部分的介绍。 正文 需要为前端提供的接口有两个:注册和登录,为此我定义了四个函数,分别是 select_user(userid, password) insert_user(userid, password, phone, email, company) on_register() on_login() 前两个函数是操作数据库,被后两个函数调用;后两个函数是给前端的接口。 后端说明 整个后端的代码如下: from flask import Flask, request import json import pymysql from flask_cors import CORS # 定义app app = Flask(__name__) # 设置跨域 CORS(app, supports_credentials=True) # 连接数据库,账号是root,密码是000,数据库名称是shopdata db = pymysql.connect("localhost", "root", "000", "shopdata

How to insert real-time data to database always from third-party API (max 1000) with pymysql/Python?

孤人 提交于 2020-08-10 19:19:47
问题 I am trying to request data from a third-party API, we got the API link like below: GET /api/v4/dblines Supposed we have to insert data from 2020-04-12 07:07:00, and we have original data before that DateTime, the API max/limit records are 1000, how to keep the script running and insert real-time data one by one all the time? Below is the sample JSON data and my sample code: from sqlalchemy import create_engine import pandas as pd import requests # getting json using requests # data =

How to insert real-time data to database always from third-party API (max 1000) with pymysql/Python?

℡╲_俬逩灬. 提交于 2020-08-10 19:19:34
问题 I am trying to request data from a third-party API, we got the API link like below: GET /api/v4/dblines Supposed we have to insert data from 2020-04-12 07:07:00, and we have original data before that DateTime, the API max/limit records are 1000, how to keep the script running and insert real-time data one by one all the time? Below is the sample JSON data and my sample code: from sqlalchemy import create_engine import pandas as pd import requests # getting json using requests # data =

python入门:操作数据库项目实例分享

∥☆過路亽.° 提交于 2020-08-10 07:11:03
本文根据一个项目实例,记录分享一下python将数据库的内容提取显示到程序界面的过程及相关设置,探索python操作数据库的用法。 主要分享内容: 1、显示数据库内容。 2、修改数据库内容。 3、表格控件指定滑块位置。 4、自定义右键菜单。 使用环境: python3.7 + Mysql5 +PyQt5 针对人群: 初学者或有资料查阅需求者,资深人士勿喷,敬请提出宝贵意见,本人虚心接受。 前期准备 1、创建软件界面 上图为使用PyQt5相关模块创建的工程造价系统界面,使用的模块及功能: 1、QMainWindow模块:绘制带菜单、工具栏、状态栏的整体框架。 2、QTreeWidget模块:绘制左侧导航栏。 3、QTableWidgetItem模块:绘制中间用于显示数据的表格。 4、QComboBox, QPushButton模块:绘制下拉框、按钮控件。 5、QSplitter模块:设置各种控件的摆放组合方式,可鼠标拖动调整窗口大小。 6、QMessageBox模块:设置交互弹框。 具体界面绘制过程不是本文重点,暂不详述。 2、与数据库建立连接 要建立与数据库的连接,首先得有具体的数据库,本案例在本机用mysql建立自己的数据库,然后再与软件项目进行连接。 2.1、自建数据库 下载安装好Mysql后,建议再装一个可视化工具,我用的Navicat for Mysql,感觉不错

SQLAlchemy

痴心易碎 提交于 2020-08-10 02:09:10
一、SQLAlchemy介绍 QLAlchemy是一个基于Python的ORM框架。该框架是建立在DB-API之上,使用关系对象映射进行数据库操作。 简而言之就是,将类和对象转换成SQL,然后使用数据API执行SQL并获取执行结果。 什么是DB-API? DB-API是Python的数据库接口规范。 在没有DB-API之前,各数据库之间的应用接口非常混乱,实现各不相同, 项目需要更换数据库的时候,需要做大量的修改,非常不方便,DB-API就是为了解决这样的问题。 pip install sqlalchemy 组成部分:   -- engine,框架的引擎   -- connection pooling 数据库连接池   -- Dialect 选择链接数据库的DB-API种类(实际选择哪个模块链接数据库)   -- Schema/Types 架构和类型   -- SQL Expression Language SQL表达式语言 二、连接数据库 SQLAlchemy 本身无法操作数据库,其必须依赖遵循DB-API规范的三方模块, Dialect 用于和数据API进行交互,根据配置的不同调用不同数据库API,从而实现数据库的操作。 下面是不同数据库的API:   # MySQL-PYthon   mysql+mysqldb://<user>:<password>@<host>[:

Python 爬取51cto博客数据存入MySQL

落爺英雄遲暮 提交于 2020-08-09 05:56:10
实验环境 1.安装Python 3.7 2.安装requests, bs4,pymysql 模块 实验步骤 1.安装环境及模块 可参考博客https://blog.51cto.com/13760351/2500048 2.编写代码 # 51cto 博客页面数据插入mysql数据库 # 导入模块 import re import bs4 import pymysql import requests # 连接数据库账号密码 db = pymysql.connect(host='172.171.13.229', user='root', passwd='abc123', db='test', port=3306, charset='utf8') # 获取游标 cursor = db.cursor() def open_url(url): # 连接模拟网页访问 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/57.0.2987.98 Safari/537.36'} res = requests.get(url, headers=headers) return res # 爬取网页内容 def find_text

python3连接mysql 稍微进阶 + 日期处理

浪子不回头ぞ 提交于 2020-08-08 23:49:50
1.踩了个操作中文的坑,结果发现之前的文章中有强调了,在连接处加:charset="utf8" conn = pymysql.connect(host = ' 127.0.0.1 ' , unix_socket = ' /tmp/mysql.sock ' , user = ' root ' , password = ' ****** ' , db = ' mysql ' , charset = ' utf8s ' ) 2. 发现报错,因为爬虫返回的list有2个数据,比如作者字段['账号已注销','生成外链接'],大概就是这么个样子, 然后调整了下抓取字段,将extract 改成 extract_first; 3. 连接SQL不报错了,但是根本写不进去内容啊,特意写了个最简单的insert语句,发现无反应,最后google出原因, 应该加conn.commit(); 贴上第一个表的字段: CREATE TABLE `songList` ( `id` mediumint( 8 ) AUTO_INCREMENT NOT NULL , `title` varchar ( 255 ) DEFAULT NULL , `author` varchar ( 255 ) DEFAULT NULL , `createTime` varchar ( 255 ) DEFAULT NULL ,

install pip3 for python 3.x

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-08 15:40:29
前言:   我目前使用的服务器为centos6.x 系统自带的python的版本为2.6.x,但是目前无论是学习还是使用python,python3都是首选,那么问题来了。---如何安装python3环境,又如何给python3安装对应的pip3呢? 更关键的是我们原来的系统中还有一些自带的工具需要用到python2.6版本,所以要求的是python3 and python2 共存,pip2 and pip3共存。下面文章就是我的亲自实践。(给 python3 安装 pip3)   写这篇的目的就是为了帮助也遇到同样问题的同志(目前网上的资料不好找,大都没有经过实践,下面是我的经历,百分百能够成功!! go !!go!!) 一。先安装python3 安装python3.x 这里不多赘述,so easzy!! 1. 先到官方网站下载python3的安装包 https://www.python.org/downloads/source/ ---我下载的是Python-3.5.2.tar.xz 2. 上传包到服务器 3.解压 tar -xf Python-3.5.2.tar.xz 4.编译安装 !!!!注意 注意 ⚠️ 在编译之前需要安装一些必须的依赖,否则当报错的时候还得重新编译 ---(我就是吃了这个亏,千万要注意奥。。。) 安装必要依赖(至少需要如下两个,我个人就遇到如下两个)

Using PyMySQL, I can't connect to RDS

时光怂恿深爱的人放手 提交于 2020-08-08 08:19:08
问题 Using PyMySQL , I get pymysql.err.OperationalError: (1045, u"Access denied for user 'my_user'@'<MY_IP_ADDRESS>' (using password: YES)") , however, I can login from the command line or MySQL Workbench using the same credentials on this machine. And, I can connect to localhost using PyMySQL. Here's my sample test code: import pymysql prod_conn = pymysql.connect( host='correct-host-name.us-west-2.rds.amazonaws.com', user='my_user', password='correct_password', port=3306, database='my_db')

Using PyMySQL, I can't connect to RDS

怎甘沉沦 提交于 2020-08-08 08:19:08
问题 Using PyMySQL , I get pymysql.err.OperationalError: (1045, u"Access denied for user 'my_user'@'<MY_IP_ADDRESS>' (using password: YES)") , however, I can login from the command line or MySQL Workbench using the same credentials on this machine. And, I can connect to localhost using PyMySQL. Here's my sample test code: import pymysql prod_conn = pymysql.connect( host='correct-host-name.us-west-2.rds.amazonaws.com', user='my_user', password='correct_password', port=3306, database='my_db')