#实现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)的更多相关文章

  1. Python MySQLdb在Linux下的快速安装

    在家里windows环境下搞了一次 见   python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...

  2. #MySQL for Python(MySQLdb) Note

    #MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...

  3. cygwin 下安装python MySQLdb

    cygwin 下安装python MySQLdb 1) cygwin 更新 运行 cygwin/setup-x86_64.exe a 输入mysql,选择下面的包安装: libmysqlclient- ...

  4. Python MySQLdb模块连接操作mysql数据库实例_python

    mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...

  5. python MySQLdb在windows环境下的快速安装

    python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...

  6. windows 环境下安装python MySQLdb

    使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...

  7. python MySQLdb连接mysql失败(转载)

    最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...

  8. 117、python MySQLdb在windows环境下的快速安装、问题解决方式

    使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...

  9. python MySQLdb Windows下安装教程及问题解决方法(python2.7)

    使用python访问mysql,需要一系列安装 linux下MySQLdb安装见  Python MySQLdb在Linux下的快速安装http://www.jb51.net/article/6574 ...

  10. macOS安装Python MySQLdb

    macOS安装Python MySQLdb 0. 参考 Mac OS X - EnvironmentError: mysql_config not found 1. 背景 import MySQLdb ...

随机推荐

  1. vue阿里上传图片报400错误

    首先我用vue上传阿里图片用的是分片上传,分片上传由于一片是以100kb为起始的,所以当图片大小小于100kb的时候不分片,可以正常上传,当大于100kb的时候,会报400错误如下 One or mo ...

  2. selenium之 chromedriver与chrome版本映射表(更新至v2.33)

    看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 v2.33 v60-62 v2. ...

  3. python使用 HTMLTestRunner.py生成测试报告

    HTMLTestRunner.py python 2版本 下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 使用时,先建立一个”PyDe ...

  4. 断路器Ribbon

    断路器:就是对服务访问不到的情况做出自己的错误,也就是故障转移(将当前出现故障的请求重新返回特定消息) 改造消费者项目(RibbonDemo) 1.在pom.xml中引入hystrix的jar包 &l ...

  5. 【转载】中文输入法下onKeyPress不能触发的问题

    onKeypress---->oninput https://segmentfault.com/a/1190000008820968

  6. JS中----this的指向和如何修改this的指向

    this this是js中的一个关键字,函数运行时自动生成的一个内部对象,只能在函数内部使用.我们要讨论的是 this 的指向. this就是函数运行时自动生成的一个内部对象 下面介绍一下几种情况下, ...

  7. php模拟数据请求

    php:模拟后台接受数据的步骤<?php> 1.连接数据库 $host="localhost"; $uname="root"; $upwd=&quo ...

  8. C++ 内链接 外链接

    编译的时候(假如编译器是VS),是以源文件cpp文件为单位,编译成一个个的obj文件,然后再通过链接器把不同的obj文件链接起来.如果一些变量或函数的定义是内连接的话,链接器链接的时候就不会拿它们去与 ...

  9. JavaScript数组去重的6个方法

    方法一无需思考,我们可以得到 O(n^2) 复杂度的解法.定义一个变量数组 res 保存结果,遍历需要去重的数组,如果该元素已经存在在 res 中了,则说明是重复的元素,如果没有,则放入 res 中. ...

  10. Web前端开发:Sublime Text 常用插件

    在安装这些插件之前,确保你已经安装了Package Control.   安装Package Control方法:   通过菜单栏View->Show Console 或者快捷键Ctrl+` 打 ...