pymysql

No module named MySQLdb

匿名 (未验证) 提交于 2019-12-03 01:55:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. I am using it in Windows Vista. 回答1: You need to use one of the following commands. Which one depends on what OS and software you have and use. easy_install mysql-python (mix os) pip install mysql-python (mix os) apt-get install python-mysqldb (Linux Ubuntu, ...) cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD) yum install MySQL-python (Linux Fedora, CentOS ...) For Windows, see this answer: Install

Flask-SQLAlchemy PyMySQL - Access Denied Error

匿名 (未验证) 提交于 2019-12-03 01:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: OS: RHEL Python Version: 3.6 SQLAlchemy Version: 1.1.10 PyMySQL Version: 0.7.11 MySQL Distrib: 5.7.17-13 import pymysql conn = pymysql.connect(host='localhost', port=3306, user='testuser', passwd='###########', db='mysql') cur = conn.cursor() cur.execute("SELECT Host,User FROM user") print(cur.description) print() for row in cur: print(row) cur.close() conn.close() When I try to execute the above code I'm getting the following error message, but I'm still able to connect to mysql using the same account and password via command line. pymysql

Getting “”Duplicate column name\" error in Django for no apparent reason

匿名 (未验证) 提交于 2019-12-03 00:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've looked here and through Google, and found nothing that seems to describe what I'm seeing. I'm using Django 1.7 with Python 3.4. ETA: I'm using MySQL 5.6.17 I have the following model (unrelated fields left out): class Location(models.Model): location_type = models.CharField(max_length=5, choices=constants.LocationTypes.LOCATION_CHOICES ) parent = models.ForeignKey("Location", blank=True, null=True, related_name='location_parent') room = models.ForeignKey("Location", blank=True, null=True, related_name='location_room') There are no other

报错django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")的解决办法

匿名 (未验证) 提交于 2019-12-03 00:27:02
阅读数:3786 参考 解决方案 Traceback (most recent call last): File "C: \Users \rHotD \AppData \Local \Programs \Python \Python 35 \lib \site -packages \django \db \backends \utils .py", line 64, in execute return self.cursor.execute(sql, params) File "C: \Users \rHotD \AppData \Local \Programs \Python \Python 35 \lib \site -packages \django \db \backends \mysql \base .py", line 110, in execute return self.cursor.execute(query, args) File "C: \Users \rHotD \AppData \Roaming \Python \Python 35 \site -packages \pymysql \cursors .py", line 166, in execute result = self._query(query) File "C: \Users \rHotD

Flask--数据库连接池

匿名 (未验证) 提交于 2019-12-03 00:16:01
Ŀ¼ import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s8day127db') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # cursor.execute("select id,name from users where name=%s and pwd=%s",['lqz','123',]) cursor.execute("select id,name from users where name=%(user)s and pwd=%(pwd)s",{'user':'lqz','pwd':'123'}) obj = cursor.fetchone() conn.commit() cursor.close() conn.close() print(obj) 为每个线程创建一个连接,线程即使调用了close方法,也不会关闭,只是把连接重新放到连接池,供自己线程再次使用。当线程终止时,连接自动关闭 from DBUtils.PersistentDB import PersistentDB import pymysql POOL = PersistentDB(

python操作mysql数据库

Deadly 提交于 2019-12-03 00:06:53
import pymysql 连接数据库方法1: ip ="192.168.xx.xx" user = 'jxz' password="123456" db='jxz' port=3306 charset='utf8' conn = pymysql.connect(host=ip,user=user,password=password,db=db,port=port,charset=charset,autocommit=True)  #建立连接 cur = conn.cursor(pymysql.cursors.DictCursor)    # 游标(输出的是个字典形式) sql = 'select * from app_myuser limit 5;' cur.execute(sql)    # 执行sql语句,insert 、update 、delete all = cur.fetchall()    # 打印全部 one = cur.fetchone()    # 打印一行 many = cur.fetchmany(2)   # 输入几行打印几行 cur.close() # 关闭游标 conn.close() # 关闭链接 print(one) print(many) print(all) 连接数据库方法2: def op_mysql(sql):   db_info = {

创建Django项目的过程

匿名 (未验证) 提交于 2019-12-02 23:55:01
1.创建Django项目根目录    a.命令式创建法: Django-admin startproject 项目名称    b.pycharm创建法: 如下图 2.配置setting环境    a.配置静态文件     STATICFILES_DIRS = [      -->静态文件夹真正的位置       os.path.join(BASE_DIR, "static"),     ]    b.注释csrf语句 (47行左右) 2.1 创建APP(已经又了Django项目的情况下添加APP)    首先: 需要,通过cmd命令或者pycharm上面的terminal命令,进入到项目所在的目录    接着(命令): python manage.py startapp APP名称    再接着: 3.使用ORM链接数据库    a. ORM不能创建数据库,所以需要手动创建数据库     create database 数据库名 charset=utf;    b. 再setting文件上配置连接数据库的条件       DATABASES = {   'default': {    'ENGINE': 'django.db.backends.mysql', # 连接的数据库类型    'HOST': '127.0.0.1', # 连接数据库的地址    'PORT': 3306,

django项目之数据库搭建

匿名 (未验证) 提交于 2019-12-02 23:49:02
""" 1.管理员连接数据库 2.创建数据库 >: create database luffy default charset=utf8; 3. 登录数据库 >: mysql -uroot -proot 4.设置权限账号密码 grant 权限(create,update..) on 库.表 to '账号'@'host' identified by '密码' # 3.设置权限账号密码 # 拥有公网或局域网,其他主机连mysql >: grant all privileges on luffy.* to 'luffy'@'%' identified by 'luffy'; # 要是本机连mysql连不上,就账号删除用localhost替换%,本机就可以登录了 >: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'luffy'; # 设置完有权限限制的账号后一定要刷新权限 >: flush privileges; # 5.退出管理员,用账号 luffy 密码 luffy 登入开发 查看已有用户 >: select user,host,password from mysql.user; # 6.luffyapi/settings/dev.py配置 DATABASES = { " default ":

ORM

匿名 (未验证) 提交于 2019-12-02 23:48:02
简单介绍ORM:   对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换 [1]。从效果上说,它其实是创建了一个可在编程语言里使用的--“虚拟对象数据库”。 简单分析ORM的优缺点:   优点:1.简单,不用自己写SQL语句      2.开发效率高   缺点:1.你需要记住ORM的特殊语法      2.因为不是专业的SQL语句,相对于某些大佬的SQL语句,执行效率肯定有差距 ORM中的对应关系:   类――数据表   对象――数据行   属性――字段 ORM能做的事:   1.操作数据表――>创建表、删除表、修改表、操作models.py里面的类   2.操作数据行――>数据的增删改查   但是ORM不能创建数据库,需要事先自己手动创建数据库。 使用Django的ORM详细步骤:   1.自己手动创建数据库     create database 数据库名;   2.在Django项目中设置连接数据库的相关配置(告诉Django连接哪一个数据库) 1 # 与数据库相关的配置 2 DATABASES = { 3 'default' : { 4 # 连接数据库的类型 5 'ENGINE' : 'django.db.backends

django 下操作数据库中数据

匿名 (未验证) 提交于 2019-12-02 23:47:01
Django 下在客服端下操作数据库表中数据 数据的操作无非就是对得到的数据进行增删改查,取出表中的数据用的是pymysql模块,在业务逻辑中为了防止重复代码首先要对pymysql进行封装。这里我选择在外部拼接出sql语句作为参数传入: class my_pymysql: def __init__(self):    #通过__init__来生成链接对象和游标对象,并指定取出的数据是以字典的形式取出,取出多条数据,以列表套字典的形式给出 self.conn = pymysql.connect(host='127.0.0.1',user='root',password='123',database='db3',charset='utf8') self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)   #将pymyaql增删改查的方法封装成函数,方便对数据进=进行操作 def get_one(self,sql,id): self.cursor.execute(sql,id) self.conn.commit() return self.cursor.fetchone() def get_all(self,sql): self.cursor.execute(sql) self.conn.commit()