进行了代码优化,欢迎评审
#!/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. 微信小程序使用原生WebSokcet实现断线重连及数据拼接

    以前做小程序为了应急找了个插件去链接WebSokcet,文章传送门. 回过头在新项目中再次使用时出现了些许问题,不一一赘述.遂决定好好用一下原生的WebSokcet. 一.说明 1.小程序原生的Web ...

  2. ng 构建

    1.ng 构建和部署 构建:编译和合并ng build 部署:复制dist里面的文件到服务器 2.多环境的支持 配置环境package.json "scripts": { &quo ...

  3. eclipse中的debug按钮组突然找不到了,找回方法

  4. wget、yum、rpm、apt-get区别

    wget 类似于迅雷,是一种下载工具, 通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理 名字是World Wide Web”与“get”的结合. yum: 是 ...

  5. 在Ubuntu下安装IntelliJ IDEA

    1)在IDEA官网下载Linux版本安装包 2)将安装包shell到/usr/local目录下 3)切到安装目录下,验证文件校验和,官网上显示的校验和: 3d77ee82094dab51e345f16 ...

  6. finally return 执行顺序问题

    网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下fina ...

  7. 【洛谷 SP2878】Knights of the Round Table(双联通分量)

    先放这吧,没时间写,明天再补 "明天到了" 题目链接 题意:求不在任何奇环内的点的数量. Tarjan求点双联通分量,然后再染色判断是不是二分图就好了. 只是不懂为什么Tarjan ...

  8. 河南省第十届省赛 Binary to Prime

    题目描述: To facilitate the analysis of  a DNA sequence,  a DNA sequence is represented by a binary  num ...

  9. Windows XP SP1 Privilege Escalation

    MS05-018 MS05-018 Works for Windows 2K SP3/4 | Windows XP SP1/2 Download ms05-018.exe: https://githu ...

  10. django2.0的reverse

    导入: 官方文档地址:https://yiyibooks.cn/xx/Django_1.11.6/topics/http/urls.html from django.urls import rever ...