#实现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 数据中心的 SAML 单点登录设置你的身份提供者

    如果你希望 Confluence 提供 SSO,将需要将 Confluence 添加到你的 IdP 中.一些后续的步骤将会与你的 IdP 有关,但是你通常需要: 在你的 IdP 中定义一个 'appl ...

  2. mongo 的导入和导出

    1.导出工具:mongoexport     1.概念:         mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指 ...

  3. MySQL数据查询子查询语句

  4. Nginx详解十二:Nginx场景实践篇之跨站访问相关

    跨站访问 浏览器请求一个页面的时候,发送了两个域名的请求 此情况不安全,容易出现CSRF攻击,所以浏览器禁止跨域访问 Nginx设置打开跨站访问 配置语法:add_header name value ...

  5. Fiddler抓包1-抓firefox上https请求

    前言 fiddler是一个很好的抓包工具,默认是抓http请求的,对于pc上的https请求,会提示网页不安全,这时候需要在浏览器上安装证书. 一.网页不安全 1.用fiddler抓包时候,打开百度网 ...

  6. vue 中动态绑定class 和 style的方法

    先列举一些例子 :class="['content',{'radioModel':checkType}]" :class="['siteAppListDirNode',{ ...

  7. 使用Calendar获取上一月,下一月,上一年,下一年的当天日期

    Calendar的add(int field,int amount)方法 field 表示月或年,天等字段 amount 代表增量或减量 例如: 上月的当天日期  Calendar cal = Cal ...

  8. MyEclipes相关配置

    0. MyEclipes10 相关下载资源(私人珍藏版) 链接:http://pan.baidu.com/s/1eSIdObS密码:0cjy 1. myEclipes连接Tomcat http://w ...

  9. WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn) java.net.ConnectException: Connection refused

    1.启动kafka的脚本程序报如下所示的错误: [hadoop@slaver1 script_hadoop]$ kafka-start.sh start kafkaServer... [-- ::,] ...

  10. How to trigger an Animation when TextBlock’s Text is changed during a DataBinding

    原文:http://michaelscherf.wordpress.com/2009/02/23/how-to-trigger-an-animation-when-textblocks-text-is ...