进行了代码优化,欢迎评审
#!/usr/bin/python
# -*- coding:utf-8 -*-
import logging
logging.basicConfig(level=logging.INFO)
 
class baselib(json.JSONEncoder):
def __init__(self):
self.conn = mysql.connector.connect(host='192.1.1.1',port=3306,user='root',passwd='123456',db='user',charset="utf8")
 
def query(self,sql):
try:
self.cursor = self.conn.cursor(dictionary=True) #返回数据为dict
data = self.cursor.execute(sql)
data = self.cursor.fetchone()
data = json.dumps(data,cls=CJsonEncoder).decode("unicode-escape")
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
print 'Connect wemedia closed in finally'
return data
def GetBitradeSqlAll(self,sql):
try:
self.cursor = self.conn.cursor()
data = self.cursor.execute(sql)
data = self.cursor.fetchall()
data = json.dumps(data,cls=CJsonEncoder).decode("unicode-escape")
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
print 'Connect wemedia closed in finally'
return data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
import mysql.connector
class mysqltest(object):
def __init__(self):
pass
 
def selectmysql(self,sql):
global conn,cursor
conn = mysql.connector.connect(host='127.0.0.1',port=3306,user='test',passwd='root@test',db='testuser', use_unicode=True)
try:
cursor = conn.cursor()
# 插入一行记录,注意MySQL的占位符是%s:
data = cursor.execute(sql)
# 查询单数据时用
#"%s" '% index
wmid = cursor.fetchone()
# wmid = cursor.fetchall() 查询多数据时使用
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
print 'Connect wemedia closed in finally'
return wmid
if __name__ == '__main__':
w= mysqltest()
sql = '''select wm_id from wm_owner_info where wm_owner_info.id =59129'''
data = w.selectmysql(sql)
print data
conn.commit()
cursor.close
conn.close
 
以下为历史版本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import mysql.connector
 
class connectMysql(object):  
 
  def ExecNonQuery(sql):
    conn = MySQLdb.connect(host='xxxx',user='xxxx',passwd='xxxx',db='xxxx')
    cursor = conn.cursor()
    cursor.execute(sql)
    res = cursor.fetchone()
    print res
    cursor.close
    conn.close
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from connectMysql import connectSQl
class diaoyong(object):
 
  def mysql_query(self,i):
    sad = connectSQl()
    sad.ExecNonQuery('select * from user where id = "%s" ' % i )
 
if __name__ == '__main__':
  w = diaoyong()
  w.mysql_query('5')

python sql语句封装连接mysql的更多相关文章

  1. Python将JSON格式数据转换为SQL语句以便导入MySQL数据库

    前文中我们把网络爬虫爬取的数据保存为JSON格式,但为了能够更方便地处理数据.我们希望把这些数据导入到MySQL数据库中.phpMyadmin能够把MySQL数据库中的数据导出为JSON格式文件,但却 ...

  2. asp.net将sql语句封装在类库中

    将sql语句封装在cs中,通过类库的引用使用他的select.update.insert 源代码(cs): using System; using System.Collections.Generic ...

  3. 常用sql语句整理:mysql

    ## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`(  ...  )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...

  4. Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)

    Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项) A.cat B.concat C.join D.+ 解答:B

  5. python使用MySQLdb模块连接MySQL

    1.安装驱动 目前有两个MySQL的驱动,我们可以选择其中一个进行安装: MySQL-python:是封装了MySQL C驱动的Python驱动:mysql-connector-python:是MyS ...

  6. mysql操作命令梳理(5)-执行sql语句查询即mysql状态说明

    在日常mysql运维中,经常要查询当前mysql下正在执行的sql语句及其他在跑的mysql相关线程,这就用到mysql processlist这个命令了.mysql> show process ...

  7. Python - Django - 使用 Pycharm 连接 MySQL 数据库

    在 Pycharm 的右上方找到 Database 点击 依次点击,选择 MySQL 数据库 点击 Download 下载驱动文件 下载完成后对数据库的相关信息进行填写 填写完成后点击“Test Co ...

  8. Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库

    最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...

  9. Python 2.7_初试连接Mysql查询数据导出到excel_20161216

    由于每天到公司都需要先执行一遍检测操作,观察数据是否导入完整,今天想到能否自动连接Mysql执行SQL并导出数据,每天到公司直接查看excel文件即可 时间紧,代码初次试验,边摸索边学习吧. xlsx ...

随机推荐

  1. Java的Properties使用及格式定义

    java.util.Properties extends Hashtable<Object,Object> 方便读取 键值对 格式的文本资源工具 常用方法一览 初始化对象 new Prop ...

  2. python单例与数据库连接池

    单例:专业用来处理连接多的问题(比如连接redis,zookeeper等),全局只有一个对象 单例代码def singleton(cls): instances = {} def _singleton ...

  3. Step-By-Step: Setting up Active Directory in Windows Server 2016

    There are interesting new features now made available in Windows Server 2016 such as time based grou ...

  4. kubernetes 参考资料

    kubernetes 参考资料 非常建议先花20分钟,完成这个官方的交互式指南:https://kubernetes.io/docs/tutorials/kubernetes-basics/ 这个教程 ...

  5. 前端面试:css预处理

    css预处理定义: 定义了一种新的语言,其基本思想是用一种专门编程语言,为css增加了一些编程的特性,将css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作. 几种预处理语言 sass l ...

  6. 栈的图文解析 和 对应3种语言的实现(C/C++/Java)【转】

    概要 本章会先对栈的原理进行介绍,然后分别通过C/C++/Java三种语言来演示栈的实现示例.注意:本文所说的栈是数据结构中的栈,而不是内存模型中栈.内容包括:1. 栈的介绍2. 栈的C实现3. 栈的 ...

  7. HDFS之FileSystem

    package cn.hx.test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; impo ...

  8. Web.xml过滤器配置及执行顺序概念

    第一个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain ch ...

  9. 分布式缓存Memcache

    Memcached是分布式的,也就是说它不是本地的.它基于网络连接(当然它也可以使用localhost)方式完成服务,本身它是一个独立于应用的程序或守护进程(Daemon方式). Memcached使 ...

  10. bzoj 2115 路径xor和最大值

    首先我们可以将这张图转化为一棵树,其中可能有好多环,肯定是从1走到N,其中可能经过好多环, 因为我们不走N在的一支,最后还是会走回来,因为a xor b xor b=a,所以相当于没走,就是走了个环, ...