python查询mysql数据库

import pymysql
host = '192.168.74.5'
user = 'root'
passwd ='root'
port = 3310
db = 'dingding' #数据库名称
table = 'gl_user' class SelectMySQL(object):
def select_data(self,sql):
result = []
try:
self.conn = pymysql.connect(
host = host,
port = port,
user = user,
passwd = passwd,
db = db,
charset='utf8',
)
self.cur = self.conn.cursor()
self.cur.execute(sql)
alldata = self.cur.fetchall()
for rec in alldata:
print(rec)
result.append(rec)
except Exception as e:
print('Error msg:',e)
return result def closeMysql(self):
self.cur.close()
self.conn.close() if __name__ =='__main__':
sql = 'select * from gl_user'
select = SelectMySQL()
result1 = select.select_data(sql)
select.closeMysql()
print(result1)
import pymysql as MySQLdb

hostname = '192.168.74.5'
user = 'root'
passwd = 'root'
port = 3310
db = 'dingding'
table = 'gl_user' class MYSQLCommand(object):
def __init__(self,host,port,user,passwd,db,table):
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.db = db
self.table = table def connectMysql(self):
try:
self.conn = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd,db=self.db,charset='utf8')
print(self.conn)
self.cursor =self.conn.cursor()
except:
print('connect mysql error') def queryMysql(self):
sql = 'select * from '+self.table
try:
self.cursor.execute(sql)
row = self.cursor.fetchall()
print(row)
except:
print(sql,' execute failed') def closeMysql(self):
self.cursor.close()
self.conn.close() if __name__=='__main__':
mysql = MYSQLCommand(hostname,port,user,passwd,db,table)
mysql.connectMysql()
mysql.queryMysql()
mysql.closeMysql()

python一天一题(2)的更多相关文章

  1. python一天一题(3)

    #--coding=utf8-- from selenium import webdriver import time import logging import os.path ''' 搜索取搜索的 ...

  2. python一天一题(1)

    #有一个文件,文件名为output_1981.10.21.txt . # 下面使用Python: 读取文件名中的日期时间信息,并找出这一天是周几. # 将文件改名为output_YYYY-MM-DD- ...

  3. Android面试一天一题(1Day)

    写在前面 该博客思路源于在简书看到goeasyway博主写的Android面试一天一题系列,无copy之意,仅为让自己总结知识点,成长一点点.先感谢各位大神的无私分享~! 关于题目,大部分则出自And ...

  4. Python面试真题答案或案例

    Python面试真题答案或案例如下: 请等待. #coding=utf-8 #1.一行代码实现1--100之和 print(sum(range(1,101))) #2.如何在一个函数内部修改全局变量 ...

  5. Python基础数据类型题

    Python基础数据类型 题考试时间:三个小时 满分100分(80分以上包含80分及格)1,简述变量命名规范(3分) 1.必须是字母,数字,下划线的任意组合. 2.不能是数字开头 3.不能是pytho ...

  6. 『Python题库 - 填空题』151道Python笔试填空题

    『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...

  7. Python练习100题

    Python练习100题 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filename:001.py cnt = 0#count the sum of res ...

  8. 21天学通Python课后实验题4.6

    21天学通Python课后实验题4.6 1. 编程实现用户输入一门课程的两门子课程成绩,第一门子课程60分以上,则显示“通过”,第一门子课程不及格,则显示“未通过”,第一门子课程及格,而第二门子课程不 ...

  9. Python语言上机题实现方法(持续更新...)

    Python语言上机题实现方法(持续更新...) 1.[字符串循环左移]给定一个字符串S,要求把S的前k个字符移动到S的尾部,如把字符串"abcdef"前面的2个字符'a'.'b' ...

随机推荐

  1. FFmpeg(6)-通过av_find_best_stream()来获取音视流的索引

    也可以通过av_find_best_stream()函数来获取流的索引: 例: audioStream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -, ...

  2. Linux系统Apache服务 - 配置HTTP的默认主页

    1.安装HTTPD和httpd-manual软件包 # yum -y install http httpd-manual 2.创建/var/www/html/index.html 内容是 Hello ...

  3. Asp.Net正则获取链接地址

    string html = “html代码”; Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?< ...

  4. 【转】suspend造成死锁

    备注:我最近的项目就遇到了这个问题.只用了一个CCriticalSection对象,并且全部都有释放.但还是死活没查出死锁的原因.最后才发现原来是suspend导致的.最终用CEvent替代了susp ...

  5. Python(九)之网络编程

    一般的网络通信: Python中利用socket模块实现网络主机间通讯: socket.socket(family,type) family:套接字类型 AF_INET AF_UNIX type: S ...

  6. 设计模式之原型模式(深入理解OC中的NSCopying协议以及浅拷贝、深拷贝)

    原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.原型模式其实就是从一个对象再创建另一个可定制的对象,而且不需知道任何创建的细节. 比如说,有一个Person类,有firstN ...

  7. RGB与CMYK以及加色与减色

    转载自:http://blog.csdn.net/tohmin/article/details/4761930 黑白色配色与RGB.CMYK 1. 光学三原色与印刷三原色 光学与印刷三原色, 相信大家 ...

  8. log4e下载地址

    Log4e的插件的下载地址:log4e.jayefem.de/content/view/3/1/

  9. spring security 注解@EnableGlobalMethodSecurity详解

     1.Spring Security默认是禁用注解的,要想开启注解,需要在继承WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解 ...

  10. Linux 增量系统备份和部分还原策略

    . . . . . 完全用 Linux 已经有快半年的时间了,一直想要全盘备份一下数据,但是却一直没有做,为什么呢? 一方面是东西比较多,备份一次要很长的时间:另一方面是一直在纠结用哪种方式备份比较好 ...