#实现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. Confluence 6 后台中的默认空间模板设置

    Confluence 6 后台中的默认空间模板设置界面的布局. https://www.cwiki.us/display/CONFLUENCEWIKI/Customizing+Default+Spac ...

  2. Vux使用经验

    Vux使用心得 参考链接 布局 简单平分:水平布局和垂直布局 <template> <divider>Horizontal</divider> <flexbo ...

  3. 四.awk、sde深度讲解

    ###sed### 查询 1创建测试文件 cat>person.txt<<EOF> 101,oldboy,CEO> 102,zhangyao,CTO> 103,Al ...

  4. vue——vue-resource

    get请求 getSearch () { return this.$http.get('https://xxx.xxx.com/xxx.json', {params: {name: this.sear ...

  5. C#动态系统托盘图标

    C#动态系统托盘图标 利用timer组件定时执行变化. using System; using System.Windows.Forms; namespace DynamicStockIcon { p ...

  6. [转] meta标签的作用及整理

    meta的标签的使用是我在前端学习中曾经困惑过一段时间的问题.一方面不是很了解meta标签的用途,另一方面是对于meta标签里的属性和值不是懂,也不知道从哪里冒出来的,所以这篇文章专门整理下meta标 ...

  7. [转]教你十分钟下载并破解IntelliJ IDEA(2017)

    来源:http://www.itwendao.com/article/detail/400687.html 温馨提示:IntelliJ IDEA(2017)需要安装JDK8以上才能运行 如果你是JDK ...

  8. C# 之 HttpRequest 类

          Request对象派生自HttpRequest类,使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值,从客户端获取信息,浏览器的种类,用户输入表单的数据,Cooki ...

  9. 【AtCoder】ARC077

    C - pushpush 如果是按下标说的话 如果是偶数个 那么是 \(N,N - 2,N - 4...1,3,5...N - 1\) 如果是奇数个 \(N,N - 2,N - 4...2,4,6.. ...

  10. python全栈开发day99-DRF序列化组件

    1.解释器组件源码分析 https://www.processon.com/view/link/5ba0a8e7e4b0534c9be0c968 2.基于CBV的接口设计 1).django循环que ...