pymysql

SQLAlchemyの增删改查

我与影子孤独终老i 提交于 2019-12-31 16:07:55
用a*my写原味sql from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String from sqlalchemy import create_engine Base = declarative_base() # 创建单表 class Users(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(32)) extra = Column(String(16)) # 数据库连接相关 engine = create_engine("mysql+pymysql://root:123456@127.0.0.1:3306/day128?charset=utf8") def init_db(): # 创建表 Base.metadata.create_all(engine) def drop_db(): # 删除表 # Base.metadata.drop_all(engine) pass if __name__ == '__main__': # drop_db() init_db()

使用Python对MySQL数据库操作

Deadly 提交于 2019-12-31 11:43:12
本文介绍Python3使用PyMySQL连接数据库,并实现简单的增删改查。 什么是PyMySQL? PyMySQL是Python3.x版本中用于连接MySQL服务器的一个库,Python2.x中则使用mysqldb。 PyMySQL安装 在使用PyMySQL之前,我们需要确保PyMySQL已经安装。 PyMySQL下载地址: https://github.com/PyMySQL/PyMySQL 。 如果还未安装,我们可以使用以下命令安装最新版的PyMySQL: $ pip install PyMySQL 如果你的系统不支持pip命令,可以使用以下方式安装: 1、使用git命令下载安装包安装(你也可以手动下载): $ git clone https://github.com/PyMySQL/PyMySQL $ cd PyMySQL $ python3 setup.py install 2、如果需要制定版本号,可以使用curl命令来安装: $ # X.X 为PyMySQL 的版本号 $ curl -L https://github.com/PyMySQL/PyMySQL/tarball/pymysql-X.X | tar xz $ cd PyMySQL* $ python3 setup.py install 注意: 请确保您有root权限来安装上述模块。 3、数据库连接 连接数据库前

Centos7 使用 Binlog2sql 恢复Mysql8.0 数据

对着背影说爱祢 提交于 2019-12-30 15:14:10
0.环境 CentOS Linux release 7.6.1810 (Core) mysql Ver 8.0.16 python 3.8.1 (下面步骤安装) pymysql 0.9.3 (下面步骤安装) 1. Binlog2sql 安装 1.1 安装python 1.依赖包安装 yum install -y wget zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc libffi-devel 2.安装python3.8.1(20191230最新稳定版) wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz tar -xzvf Python-3.8.1.tgz cd Python-3.8.1 ./configure --prefix=/etc/python3.8 --enable-optimizations make && make install [root@mysql1 python3.8]# ll /usr/bin/python* lrwxrwxrwx. 1 root root 7 Jun 3 2019 /usr/bin/python -> python2

Mock a MySQL database in Python

血红的双手。 提交于 2019-12-30 08:09:13
问题 I use Python 3.4 from the Anaconda distribution. Within this distribution, I found the pymysql library to connect to an existing MySQL database, which is located on another computer. import pymysql config = { 'user': 'my_user', 'passwd': 'my_passwd', 'host': 'my_host', 'port': my_port } try: cnx = pymysql.connect(**config) except pymysql.err.OperationalError : sys.exit("Invalid Input: Wrong username/database or password") I now want to write test code for my application, in which I want to

Using SQLAlchemy and pymysql, how can I set the connection to utilize utf8mb4?

余生长醉 提交于 2019-12-30 02:53:06
问题 I discovered (the hard way) that MySQL's UTF8 character set is only 3 bytes. A bit of research shows I can fix this by changing the tables to utilize the utf8mb4 collation and get the full 4 bytes UTF should be. I've done so. My database, tables and columns have all been ALTER ed to utilize this charset. However, I still receive this message if I have data that has unicode code points larger than U+FFFF: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

Django开发基础----操作数据库

南笙酒味 提交于 2019-12-29 11:33:51
Django中对数据库的操作是由Models来完成的 Models是什么? 通常, 一个Model 对应数据库的 一张数据表 Django中Models以 类 的形式出现 它包含了一些 基本字段 以及数据的 一些行为 所以,在Django中所有对数据库的操作,就是对Models中的 类以及类的对象 的操作,不需要写任何SQL语句来和数据库进行直接的交互。 编写Models步骤: 1、在应用根目录下创建models.py,并引入models模块 2、创建类,继承models.Model,该类即是一张数据表 3、在类中创建字段 字段创建: 1、数据表里的字段其实就是models.py中类的 属性 (变量) 例如: name = models.CharField(max_length=100) # 发布会标题 Django中主要的数据类型: 编写完Models,怎么把类同步到数据库,生成数据表呢? 1、命令行中进入manage.py的同级目录 2、执行 python3 manage.py makemigrations app名(sign)(可选,如果不指定应用名,默认是该项目下的所有应用) 3、再执行 python3 manage.py migrate,完成 数据迁移 完成数据迁移后,Django会自动在 sign/migrations 目录下生成移植文件 执行 python3

Python开发个人专属表情包网站

ⅰ亾dé卋堺 提交于 2019-12-29 10:20:59
“表情包”是一种利用图片来表示感情的一种方式。表情包是在社交软件活跃之后,形成的一种流行文化,表情包流行于互联网上面,基本人人都会发表情。 曾经你是否也有过找不到表情包去应对别人的时候。 今天小编分享如何用Python开发个人专属的表情包网站,想用什么表情包搜一下就有了! 本篇分为两部分 1、爬取表情包存入数据库 2、搭建个人个人专属表情网站 爬取包情包存入数据库 环境:Windows+Python3.6 IDE:个人喜好 模块 1 import requests 2 import re 3 import pymysq 完整代码 6 import requests 7 import re 8 import pymysql 9 # 连接数据库 10 db = pymysql.connect(host = '127.0.0.1',port = 3306,db = 'db',user = 'root',passwd = 'root',charset = 'utf8') 11 # 创建游标 12 cursor = db.cursor() 13 # cursor.execute('select * from images') 14 # print(cursor.fetchall()) 15 # 小驼峰 16 # 注释 获取图片列表 17 def getImagesList(page): 18

Python中的MySQL接口:PyMySQL & MySQLdb

[亡魂溺海] 提交于 2019-12-28 11:31:42
MySQLdb模块只支持MySQL-3.23到5.5之间的版本,只支持Python-2.4到2.7之间的版本 PyMySQL支持 Python3.0以后的版本 PyMySQL https://pypi.python.org/pypi/PyMySQL https://github.com/PyMySQL/PyMySQL/blob/master/README.rst Python -- one of the following: CPython >= 2.6 or >= 3.3 PyPy >= 4.0 IronPython 2.7 MySQL Server -- one of the following: MySQL >= 4.1 (tested with only 5.5~) MariaDB >= 5.1 MySQLdb https://pypi.python.org/pypi/MySQL-python/1.2.5 MySQL-python 1.2.5 Downloads ↓ Python interface to MySQL MySQLdb is an interface to the popular MySQL database server for Python. The design goals are: Compliance with Python database API

pymysql和MySQLdb

有些话、适合烂在心里 提交于 2019-12-28 11:31:23
MySQLdb创建mysql数据库表 其实mysqldb创建表是有限制的,要求是5.1-5.5版本 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。 1.下载 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-python/或者点击链接下载http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz?use_mirror=nchc 2.解压安装 解压:tar zxvf MySQL-python* 进入文件目录,运行以下命令: python setup.py install 3. 用法 ''' # 打开数据库连接 db = MySQLdb.connect("192.168.72.131","root","123456","test" ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 如果数据表已经存在使用 execute() 方法删除表。 cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # 创建数据表SQL语句 sql = \"\"\"CREATE

MySQLdb和pymysql区别

断了今生、忘了曾经 提交于 2019-12-28 11:31:05
MySQLdb要快点,原因是这个是C写的,速度快 MySQLdb 只支持Python2.x,还不支持3.x 可以用PyMySQL代替。安装方法:pip install PyMySQL 然后在需要的项目中,把 __ init __.py中添加两行: import pymysql pymysql.install_as_MySQLdb() 来源: https://www.cnblogs.com/allenhu320/p/11416726.html