python & MySQLdb(Three)
#实现python封装
# encoding=utf8
import MySQLdb
#定义类
class MysqlHelper():
def __init__(self,host,port,db,user,passwd,charset='utf8'):
self.host=host
self.port=port
self.db=db
self.user=user
self.passwd=passwd
self.charset=charset
#初始化设置连接
def connect(self):
self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
self.cursor=self.conn.cursor()
#进行连接
def close(self):
self.cursor.close()
self.conn.close()
#关闭连接
def get_one(self,sql,param=()):
result=None
try:
self.connect()
self.cursor.execute(sql,param)
result=self.cursor.fetchone()
self.close()
except Exception as e:
print(e)
return result
#查找一行
def get_all(self,sql,param=()):
list=()
try:
self.connect()
self.cursor.execute(sql,param)
list=self.cursor.fetchall()
self.close()
except Exception as e:
print(e)
return list
#查找全部数据
def insert(self,sql,param=()):
return self.__edit(sql,param)
#返回插入数据
def update(self,sql,param):
return self.__edit(sql,param)
#返回更改后数据
def delete(self,sql,param):
return self.__edit(sql,param)
#返回删除数据
def __edit(self,sql,param):
count=0
try:
self.connect()
count=self.cursor.execute(sql,param)
self.conn.commit()
self.close()
except Exception as e:
print(e)
return count
实现添加操作:
#encoding=utf8
from fengzhaung import *
sql="insert into stu(stu_name,stu_hometown,gender) values(%s,%s,%s)"
stu_name=input("输入姓名")
stu_hometown=input("请输入家乡:")
gender=input("输入性别,1男,0女:")
param=[stu_name,stu_hometown,bool(gender)]
mysqlh=MysqlHelper('192.168.65.146',3306,'student','root','toor')
count=mysqlh.insert(sql,param)
if count==1:
print ('ok')
else:
print('error')
实现查找:
#encoding=utf8
from fengzhaung import *
#id=input("输入编号")
sql="select * from stu where stu_id=1"
helper=MysqlHelper('192.168.65.146',3306,'student','root','toor')
s=helper.get_one(sql)
print(s)
最后还有一点小问题,怎么自主输入进行查找??试了几种办法,发现不行。
mmp我搞了半天发现是在做字符串的转换,这个是必然报错的,但是我们用连接操作就可以了,利用MySQLhelper的get_one方法去传就可以了,自己真的菜啊看来
#方法一:
#encoding=utf8
from fengzhaung import *
ih=input("输入编号")
print(id(ih))
sql="select * from stu where stu_id="+ih
helper=MysqlHelper('192.168.65.146',3306,'student','root','toor')
s=helper.get_one(sql)
print(s)
#方法二
#encoding=utf8
from fengzhaung import *
ih=input("请输入编号:")
sql="select * from stu where stu_id=%s"
param=[ih]
helper=MysqlHelper('192.168.65.146',3306,'student','root','toor')
s=helper.get_one(sql,param)
print(s)
python & MySQLdb(Three)的更多相关文章
- Python MySQLdb在Linux下的快速安装
在家里windows环境下搞了一次 见 python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...
- #MySQL for Python(MySQLdb) Note
#MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...
- cygwin 下安装python MySQLdb
cygwin 下安装python MySQLdb 1) cygwin 更新 运行 cygwin/setup-x86_64.exe a 输入mysql,选择下面的包安装: libmysqlclient- ...
- Python MySQLdb模块连接操作mysql数据库实例_python
mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...
- python MySQLdb在windows环境下的快速安装
python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...
- windows 环境下安装python MySQLdb
使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...
- python MySQLdb连接mysql失败(转载)
最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...
- 117、python MySQLdb在windows环境下的快速安装、问题解决方式
使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...
- python MySQLdb Windows下安装教程及问题解决方法(python2.7)
使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装http://www.jb51.net/article/6574 ...
- macOS安装Python MySQLdb
macOS安装Python MySQLdb 0. 参考 Mac OS X - EnvironmentError: mysql_config not found 1. 背景 import MySQLdb ...
随机推荐
- 实用的sublime插件集合 – sublime推荐必备插件
Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...
- conda 安装多个版本的python
再添加一个python3.6conda create --name py36 python=3.6然后通过source activate py36来激活环境
- Java测试的题目感想
日期:2018.9.24 星期一 博客期:012 说起来测试真的是来的时候信心满满,考完的时候慌得出神!我感觉自己会用Scanner类做输出和文件操作就可以在有限时间内把它搞出来了!事实证明我错了!我 ...
- Maven设置本地仓库路径
在maven文件下的settings.xml中添加<localRepository>F:\cppdy\repo</localRepository>(本地仓库路径)
- react 使用draft.js富文本编辑器
参照网址:https://www.cnblogs.com/3body/p/6224010.html 参看网址:https://www.cnblogs.com/mosquito18/p/9787816. ...
- cf1121d 尺取
尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...
- 支付宝WAP支付总结
一:支付宝开放文档: 支付宝手机网站支付的开发文档: https://docs.open.alipay.com/203 二:配置应用环境: 开发者调用接口前需要先生成RSA2密钥,RSA2密钥包含应用 ...
- Eclipse编写ExtJS5卡死问题
本篇以eclipse为例,导入后在编译时很容易出现eclipse的卡死现象,这主要是js文件的校验引起的. 我们可通过如下方法进行配置: 打开该项目的.project文件,删除如下配置即可: < ...
- Selenium+PhantomJS使用时报错原因及解决方案
问题 今天在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息: UserWarning: Selenium support for PhantomJS has been dep ...
- 437. 路径总和 III
方法一:48 ms /* sumUp递归子程序求解以root为根节点的子节点之和为sum的路径数目; pathSum递归部分是把根节点逐一考察,如以root->left,以root->ri ...