pymysql

使用 shell / python 进行sql的excel报表导出

戏子无情 提交于 2021-02-01 12:57:27
  如果要求你进行一个表数据的导出,如果使用shell的话,很容易做到,即执行一下 select 语句就可以拿到返回结果了! 如下: /usr/bin/mysql -u " ${username} " -p " ${password} " --host=${host} -D " ${database} " < ${sql_script_path} > ${export_data_full_path1};   如上执行完成之后,数据就被导出到 export_data_full_path1 指定的文件位置去了。   如果想要使用 excel 格式来打开,有一个很简单的方法,即把后缀名命名为: .xls 就可以了。唯一的缺点是,此时你可能看到一个提示,即:文件名后缀与具体的格式不匹配等等!但是你仍然可以正常打开!   但是对于有中文一类的导出操作,则又是,另一番景象了!不过我们可以通过一个简单的编码转换来解决这个问题! iconv -futf8 -tgb2312 -o ${export_data_full_path1} ${export_data_full_path1};   转换之后,即使有中文也能正常查看了!   至于使用 shell 进行发送邮件,则也是简单的一比! echo " yesterday report infomation, FYI . " | mail -s "

Python导出sql语句结果到Excel

别等时光非礼了梦想. 提交于 2021-02-01 11:52:12
本文档是因为每周需要统计线上数据库中客户新增资源,手动执行实在是麻烦,就写了个脚本导出到Excel,顺便发一封邮件。 (当然这不是线上的真实脚本,不过根据个人需求稍微修改下,还是可以直接用的。拿去不谢!!) 将想发出邮件的SQL语句写到exec.sql: vim /tmp/ exec.sql select * from db; 编辑Python脚本: root@localhost:/ tmp# vim exportsql.py # !/usr/bin/ python # coding: utf - 8 import sys import xlwt import pymysql import datetime import subprocess import time import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication import os.path host = ' localhost ' user = ' root ' pwd = ' jeqThs1qOVbHGRz0 ' port = 3306 db = ' mysql ' sql

(1045, “Access denied for user 'root'@'ip-address' (using password: YES)”) in python

馋奶兔 提交于 2021-01-29 10:31:52
问题 I want to access MySql database hosted on domain my_domain_name using pymysql ( python library) . My code is: connection = pymysql.connect('my_domain_name', user, passd, db) But I receive this error (1045, "Access denied for user 'root'@'ip-address' (using password: YES)") In most answers, people suggest that: either password is wrong or 'root' user do not have privileges to access database But, I can access the database using direct link as: http://www.my_domain_name/phpmyadmin/index.php

Update multiple columns on conflict postgres

℡╲_俬逩灬. 提交于 2021-01-29 06:40:50
问题 I have to write a query to update a record if it exists else insert it. I am doing this update/insert into a postgres DB. I have looked into the upsert examples and most of them use maximum of two fields to update. However, I want to update multiple columns. Example: query="""INSERT INTO table (col1,col2,col3,col4,col5,col6,col7,col8,col9,..col20) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT(col2) DO UPDATE SET (col1,col2,col3,col4,col5,col6,col7,col8,col9,.

Scrapy Pipeline doesn't insert into MySQL

巧了我就是萌 提交于 2021-01-27 17:55:39
问题 I'm trying to build a small app for a university project with Scrapy. The spider is scraping the items, but my pipeline is not inserting data into mysql database. In order to test whether the pipeline is not working or the pymysl implementation is not working I wrote a test script: Code Start #!/usr/bin/python3 import pymysql str1 = "hey" str2 = "there" str3 = "little" str4 = "script" db = pymysql.connect("localhost","root","**********","stromtarife" ) cursor = db.cursor() cursor.execute(

django.db.utils.InterfaceError: (0, '')

[亡魂溺海] 提交于 2021-01-27 06:28:14
问题 I've recently implemented django in a tool I'm developing. While doing some tests I have been getting an django.db.utils.InterfaceError: (0, '') error. I have read that it might be a global cursor issue, however I am only doing queries through django, letting it handle the cursors. Basically I have information about compounds which, I save to mySQL during several moments of the tool execution. This particular piece of code will execute several times (which seems to work fine so far) and will

Python, sharing mysql connection in multiple functions - pass connection or cursor?

只谈情不闲聊 提交于 2021-01-27 02:52:01
问题 I am opening mysql connection in the main function and use that connection in multiple functions called by the main function. Is there anything wrong with passing cursor from main function instead of passing the connection? I.e.: Pass in cursor from main function def main(): conn = pymysql.connect(...) with conn as cursor: func1(cursor) func2(cursor) conn.close() def func1(cursor): cursor.execute('select ...') def func2(cursor): cursor.execute('insert ...') Pass in connection from main

Python, sharing mysql connection in multiple functions - pass connection or cursor?

泪湿孤枕 提交于 2021-01-27 02:51:57
问题 I am opening mysql connection in the main function and use that connection in multiple functions called by the main function. Is there anything wrong with passing cursor from main function instead of passing the connection? I.e.: Pass in cursor from main function def main(): conn = pymysql.connect(...) with conn as cursor: func1(cursor) func2(cursor) conn.close() def func1(cursor): cursor.execute('select ...') def func2(cursor): cursor.execute('insert ...') Pass in connection from main

<scrapy爬虫>爬取quotes.toscrape.com

风格不统一 提交于 2021-01-25 07:35:16
1.创建scrapy项目 dos窗口输入: scrapy startproject quote cd quote 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) import scrapy class QuoteItem(scrapy.Item): # define the fields for your item here like: text = scrapy.Field() author = scrapy.Field() tags = scrapy.Field()    3.创建爬虫文件 dos窗口输入: scrapy genspider myspider quotes.toscrape.com 4.编写myspider.py文件(接收响应,处理数据) # -*- coding: utf-8 -*- import scrapy from quote.items import QuoteItem class MyspiderSpider(scrapy.Spider): name = 'myspider' allowed_domains = ['quotes.toscrape.com'] start_urls = ['http://quotes.toscrape.com/'] def parse(self, response): for each in

四 .Django (原生mysql操作)

倖福魔咒の 提交于 2021-01-24 13:44:06
一. Django原生mysql(项目操作增删改查) 1.mysql(语句封装) 回顾 https://www.cnblogs.com/ugfly/p/7739182.htm 表的三种关系 一对一 一对多 多对多 回顾一下mysql知识 https://www.cnblogs.com/dangrui0725/p/9594625.html 使用Python将Excel中的数据导入到MySQL https://www.cnblogs.com/taceywong/p/5428356.html https://zzk.cnblogs.com/s/blogpost?Keywords=python%E7%9A%84mysql&pageindex=1 python 原生mysql引用 函数封装 import pymysql # 查 所数据 def get_all(sql): conn = pymysql.connect(host= " localhost " , user= " root " , password= " root " , database= " db6 " ) cur = conn.cursor(cursor= pymysql.cursors.DictCursor) cur.execute(sql) res = cur.fetchall() cur.close() conn