python3查询mysql数据
python3不支持MySQLdb,代替的是import pymysql
连接数据库查表:
import pymysql
conn= pymysql.connect(
host='xx.xx.xx.xx',
port = 3306,
user='xxx',
passwd='xxx',
db ='transit',
)
cur = conn.cursor()
cur.execute("SELECT * FROM xxx")
for r in cur.fetchall():
print(r)
#cur.close()
conn.close()
或者
#1. mysql connetct
host = "xxx.xxx.xxx.xxx"
user = "xxx"
passwd = "xxx"
port = 3306
db = "transit"
conn = MySQLdb.connect(host=host,port=port,user=user,passwd=passwd,db=db,charset='utf8', )
#2. create cursour
cursor = conn.cursor()
#3. query
sql = "select * from xxx"
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
id = row[0]
stream_name = row[1]
download_url = row[2]
size = row[3]
status = row[4]
createAt = row[5]
print(id,stream_name,download_url,size,status,createAt)
#print ("id=%s,stream_name=%s,download_url=%s,size=%s,status=%d,createAt=%s"%(id, stream_name, download_url, size, status,createAt))
except:
print ("Error: unable to fetch data")
# close mysql
conn.close()
python3查询mysql数据的更多相关文章
- python查询mysql数据(3)
python查询mysql数据(3) """数据查询""" import pymysql import datetime from pymy ...
- 笔记:PHP查询mysql数据后中文字符乱码
新建表Clubs CREATE TABLE `Clubs` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET utf8 NOT NULL ...
- php 查询mysql数据批量转为PDF文件二(批量使用wkhtmltopdf html导出PDF)
上节讲到配置wkhtmltopdf,这节讲下如何批量操作 首先讲下wkhtmltopdf如何使用 直接命令行输入: wkhtmltopdf http://www.baidu.com/ baidu.p ...
- php 查询mysql数据批量转为PDF文件一(mac使用配置wkhtmltopdf html导出PDF)
数据转标准PDF查文档,查资料先转HTML标准格式再html转PDF 转PDF wkhtmltopdf工具是最佳选择 首先下载wkhtmltopdf https://wkhtmltopdf.org/d ...
- 查询MYSQl数据表中的最后一条记录
mysql: select * from table order by id DESC limit 1 oracle: select * from emp where id in (select ma ...
- 查询MySQL数据表的字段名和表结构
查询表的字段: -- 查询表的字段名 SELECT COLUMN_NAME -- GROUP_CONCAT('a.', COLUMN_NAME SEPARATOR ',') AS COLUMN_NAM ...
- python查询mysql数据
>>>cur.execute("select * from 表名") >>>lines=cur.fetchall() >>>f ...
- Python3操作MySQL,查询数据并保存到文件中
我们在测试过程中,可能需要到数据库中拉去一些数据,为从测试准备.比如最近在做接口性能测试的时候,就需要很多数据来支撑,所以就需要的数据库去查询数据,下面就是python3 查询 mysql 并且保存到 ...
- 【MySQL】MySQL中查询出数据表中存在重复的值list
1.目的:查询MySQL数据表中,重复记录的值 2.示例: 3.代码: select serial_num,count(*) as count FROM card_ticket GROUP BY se ...
随机推荐
- C# winfrom FastReport Print
1.引用 using FastReport; using FastReport.Barcode; 2.code private void toolStripButtonPrint_Click(obje ...
- C# Lambda快速深度拷贝
背景:今天上班在班车上和一个同事讨论有关C#拷贝效率的问题,聊到了多种深度拷贝方法,其中就提到了一种Lambda表达式拷贝的方法,这位同事说这种深度拷贝快是快但是如果对象里面再嵌入对象就不能深度拷贝了 ...
- js笔试题一套(未完待续)
1.下面程序的运行结果是: function test(x, y, z) { alert(test.length); alert(arguments.length); alert(arguments. ...
- Linux常用的编辑保存退出命令
Vi Vim进入编辑后退出 按ESC后 1.保存退出 :wq :x 最快捷的方法:直接按shift+zz,或者切换到大写模式按ZZ,就可以保存退出了,即是按2下大写的Z.区别::wq 强制性写入文件并 ...
- dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
dubbo-2.8.4需用jdk版本为1.8,dubbo-2.5.3可以使用1.7版本的jdk.
- Android键盘属性
在主xml中android:windowSoftInputMode的属性"stateUnspecified"软键盘的状态(是否它是隐藏或可见)没有被指定.系统将选择一个合适的状态或 ...
- redis的特性
- 如何关闭打开了多个activity的activity
专门建立一个类,内部有一个静态的linklist对象,用来记录打开的activity,如果该ACTIVITY没有被打开过,在每一个activity oncreate方法中将自己的实例加入这个list. ...
- Border Layout
------------------siwuxie095 根面板 contentPane 的默认布局就是 Border Layout B ...
- 百度Apollo解析——0.使用VSCode编译Apollo项目
1.安装微软Visual Studio Code 1.1 方法一 开始之前,首先需要安装Ubuntu Make.虽然Ubuntu Make存在Ubuntu15.04官方库中,但是需要Ubuntu Ma ...