例子来源于《python基础教程》第三版,57p
该例子主要是使用字典的方式,实现一个小型的数据库,通过查询字典的键值来获取用户的信息。本人修改了部分代码。#!/usr/bin/python3
-*- coding:utf-8 -*-
# 使用字典构建一个简单的数据库#导入模块,主要为了做异常退出
import os
# 构建people字典,用来存储用户信息
people = {‘Ailce‘:{‘phone‘: ‘2341‘,‘addr‘: ‘Foo drive 23‘},‘Beth‘: {‘phone‘: ‘9102‘,‘addr...
业务方要求每周发一封周报出来,将过去一周的线上项目的详细信息发送出来,我们的监控用的是zabbix,过去一直是手动填写,非常耗时耗力,而且显得非常不专业,所以我花了几个月时间学习Python,编写如下脚本供大家参考,望大神勿笑。
#!/usr/bin/env python
#-*- coding: utf8 -*-
import MySQLdb
import string
import xlsxwriter
import time,datetime
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)zdbhost = ‘ ‘...
需求:数据仓库中所有表的定义结构保存到新的文件中,保存后类似下面数据,重复的数据只保留7月份即可****************ods_log_info*****************lid string uid string mb_uid string operation string module string result string ts string remark1 string remark2 string remark3 string ****************ods_mbportal_201407*****************data_time_thread string data_module string data_operation string data_re...
以下代码实现了ip查询功能处理程序 复制代码 代码如下:import os,time
def getip(filepath): ip2city={} file=open(filepath,‘r‘) lines=file.readlines() file.close() for line in lines: ip=line.split(‘ ‘)[0] city=line.split(‘ ‘)[1] haship=hashm(ip) if haship in ip2city: pass else: ip2city[haship]=city print(‘Hash done!‘) ...
#coding=utf-8
import MySQLdb
conn = MySQLdb.Connect(host = ‘127.0.0.1‘,port=3306,user=‘root‘,passwd=‘‘,db=‘test‘,charset=‘utf8‘)
cursor = conn.cursor()
sql = "select * from orders"
cursor.execute(sql)#获取总记录数
print cursor.rowcount#获取一条数据
rs = cursor.fetchone()
print rs#获取3条数据,将从第二条开始
rs = cursor.fetchmany(3)
print rs#获取所有数据,返回所有的数据
rs = cursor.fetchal...
今天在生产系统中查询产品时,莫名提示错误:maximum recursion depth exceeded while calling a Python object,根据错误日志提示,发现在查询产品时,系统会构造一个domain,查询所有库位的库存量。当仓库较多的时候,构造的这个domain比较长,然后解析这个domain的方法distribute_negate是递归调用,因为递归次数太多,所以就提示错误。根据源码查看了生成domain的条件,这个部分不太好调整,所以后来直接找了个方法来增加递归的...
pymysql------操作mysql数据库openpyxl------操作excel表 连上mysql操作:1、打开数据库import pymysqldb=pymysql.connect(host,user,password,database)2、使用cursor()方法创建一个游标对象cursor=db.cursor()3、执行操作a、数据库插入 try: curcor.excute(sql) db.commit()except: db.rollback()b、数据库查询(fetchone()--该方法获取下一个查询结果集。结果集是一个对象、fetchall()-----接收全部的返回结果行.)cu...
一、多表连接查询: #重点:外链接语法准备表#建表create table department(
id int,
name varchar(20)
);create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum(‘male‘,‘female‘) not null default ‘male‘,
age int,
dep_id int
);#插入数据insert into department values
(200,‘技术‘),
(201,‘人力资源‘),
(202,‘销售‘),
(203,‘运营‘);insert into employee(name,sex,age,...
#coding=utf-8import mysql.connector
import importlib
import sys#连接数据库的信息
mydb = mysql.connector.connect(host=‘115.xx.10.121‘,port=‘3306‘,user=‘root‘,password=‘xxxxxZ6XPXbvos‘,)mycursor=mydb.cursor()
#创建数据库
mycursor.execute(‘create database test_cc‘)
#查询数据库
mycursor.execute(‘show databases‘)
for x in mycursor:if‘test_cc‘in x:print‘成功‘#删除数据库
mycursor.execute(...
3.子查询(难):当进行查询的时候,发现需要的数据信息不明确,需要先通过另一个查询得到,此查询称为子查询;执行顺序:先执行子查询得到结果以后返回给主查询组成部分:1).主查询部分2).子查询部分【注意事项】:子查询一定需要被定义/包裹在小括号内部,可以认为是显示的提升了代码执行的优先级需求1:查询薪资比Abel的高的有谁?分析:①.先查询出Abel的薪资是多少?②.将过滤条件定义为>①,然后进行查询得到最终需要的结果代码...
记录查询操作及获取查询结果列字段的方法1.sqlite3 中获取所有表名及各表字段名的操作方法SQLite 数据库中有一个特殊的表叫 sqlite_master,sqlite_master 的结构如下:CREATE TABLE sqlite_master (
type TEXT,
name TEXT,
tbl_name TEXT,
rootpage INTEGER,
sql TEXT
); 可以通过查询这个表来获取数据库中所有表的信息SELECT * FROM sqlite_master WHERE type='table';查询某张表的所有字段PRAGMA table_info(表名);
示例...
最近在用python操作mysql数据库时,碰到了下面这两个函数,标记一下:fetchone() :返回单个的元组,也就是一条记录(row),如果没有结果 则返回 Nonefetchall() :返回多个元组,即返回多个记录(rows),如果没有结果 则返回 ()需要注明:在MySQL中是NULL,而在Python中则是None 用法如下所示:fetchone()用法:cur.execute("select host,user,password from user where user=‘%s‘" %acc)jilu = cur.fetchone() ##此时 通过 jilu[...
场景:需要批量查询域名信息需要生成一个 domain_in.txt的文件
文件内容格式(尾行不可有空行):baidu.com
bilibili.com
代码import random
import string
from dns.resolver import Resolver
import requests
import socket
from parsel import Selector"""
# python3
pip install dnspython3
pip install lxml
pip install requests
pip install parsel
"""dns_resolver = Resolver()
dns_resolver.nameservers = ["8.8.8.8", "...
# Version: python3.6# Author: Gao# 多级菜单:三级菜单,可依次选择进入子菜单,列表,字典city_dict = { "四川省": { "成都市": ["锦江区", "青羊区", "金牛区", "武侯区", "成华区", "龙泉驿区"], "攀枝花市": ["东区", "西区", "仁和区", "米易县", "盐边县"], "泸州市": ["江阳区", "纳溪区", "龙马潭区", "泸县", "合江县"], ...
一 单表查询,以下是表内容 1一 having 过滤2 1.1 having和where3 select * from emp where id > 15;4 解析过程;from > where 找到数据 > 分组(没有默认一个组)> select 打印 where是出结果之前5 select * from emp having id > 15;6 解析过程;from > where 找到数据(没有约束条件,就是整个表)) > 分组(没有默认一个组)> select 打印 > having where是出结果之后7 上面2个输出是一样的,因为没有...