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 ...
随机推荐
- CodeForces - 510B Fox And Two Dots (bfs或dfs)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Tortoisesvn 如何在资源管理器中断开连接
你在这个文件夹下打开“工具—文件夹选项—查看”,勾选“显示隐藏的文件夹”选项,可以看到在SVN所在的文件夹下面, 有一个.svn文件夹,把它删除了,刷新一下就可以了.
- Appium—python_ 安卓手机划屏幕操作
开始的时候 不知道 python_unittest框架的命名规则,导致方法进不去,后来 改变方法名 能获取 # conding=utf- from appium import webdriver im ...
- windows异常演示,指定异常类型,然后生成异常
#include "stdafx.h"#include <Windows.h>#include <float.h> DWORD Filter (LPEXCE ...
- C++知识点总结(5)
1.为何静态成员函数不能调用非静态成员函数 静态成员函数可以不需要类的实例就直接使用,非静态的成员函数很可能用到一些成员变量,而成员变量的创建和初始化是在创建了类的实例时在构造函数调用的时候才进行的. ...
- import gevent 协程 import greenlet
- global作用域
1 global在函数内部 $somevar=15; function addit () { GLOBAL $somevar; $somevar++ ; echo "somevar is ...
- php中用大括号把?>和<?php框起来的作用
<?php function my_function() { ?> My function was called <!--就是这里,为什么前面要用?>和< ?php 把M ...
- centos系统查看本机IP地址
centos系统查看本机IP地址,输入 ifconfig -a查看 centos查询上网公网IP输入 curl ifconfig.me 命令即可查看 centos查询上网网关IP,tracepath ...
- 760. Find Anagram Mappings乱序字符串的坐标位置
[抄题]: Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by rand ...