python连接数据库查询判断是否有记录

Python087

python连接数据库查询判断是否有记录,第1张

#-*- coding: utf-8 -*-

import MySQLdb, datetime, time

#code数字含义

code_mean = {10:"开始下载(10)",

11:"下载完成(11)",

12:"安装界面(12)",

13:"安装成功(13)",

14:"启动游戏(14)",

16:"更新开始(16)"}

#Networktype数字含义

network_type_mean = {1:"3G 网络",

2:"2G 网络",

3:"WIFI网络"}

#当前测试人员拥有手机

phonelist = {1:"0049990********", 2:"8689430********", 3:"3558680********"}

#当前系统时间

nowtime = (datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))

print "当前时间:" + nowtime

today = str(nowtime).split()[0]

#去除日期

HMS = nowtime.split()[1]

print "目前测试人员拥有的手机如下:"

print "HTC *** :1"

print "HTC **** :2"

print "HUAWEI ***** :3"

phont_imei = raw_input("请选择你要查询的手机,输入上列手机对应的数字即可:")

time_start = raw_input("请输入需要查询的起始时间(格式如后:" + HMS + ",默认日期为今天): ")

imei = phonelist[int(phont_imei)]#-*- coding: utf-8 -*-

import MySQLdb, datetime, time

#code数字含义

code_mean = {10:"开始下载(10)",

11:"下载完成(11)",

12:"安装界面(12)",

13:"安装成功(13)",

14:"启动游戏(14)",

16:"更新开始(16)"}

#Networktype数字含义

network_type_mean = {1:"3G 网络",

2:"2G 网络",

3:"WIFI网络"}

#当前测试人员拥有手机

phonelist = {1:"0049990********", 2:"8689430********", 3:"3558680********"}

#当前系统时间

nowtime = (datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))

print "当前时间:" + nowtime

today = str(nowtime).split()[0]

#去除日期

HMS = nowtime.split()[1]

print "目前测试人员拥有的手机如下:"

print "HTC *** :1"

print "HTC **** :2"

print "HUAWEI ***** :3"

phont_imei = raw_input("请选择你要查询的手机,输入上列手机对应的数字即可:")

time_start = raw_input("请输入需要查询的起始时间(格式如后:" + HMS + ",默认日期为今天): ")

imei = phonelist[int(phont_imei)]

Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

str.find(str, beg=0, end=len(string))

str -- 指定检索的字符串

beg -- 开始索引,默认为0。

end -- 结束索引,默认为字符串的长度。

初学者建议用上面的,进阶可以用正则表达式

1、说明

可以使用find或者index来查询字符串,可以使用replace函数来替换字符串。

2、示例

1)查询

>>>'abcdefg'.find('cde')

结果为2

'abcdefg'.find('acde')

结果为-1

'abcdefg'.index('cde')

结果为2

2)替换

'abcdefg'.replace('abc','cde')

结果为'cdedefg'

3、函数说明

1)find(...)

S.find(sub[, start[, end]]) ->int

返回S中找到substring sub的最低索引,使得sub包含在S [start:end]中。 可选的 参数start和end解释为切片表示法。

失败时返回-1。

2)index(...)

S.index(sub[, start[, end]]) ->int

与find函数类似,但是当未找到子字符串时引发ValueError。

3)replace(...)

S.replace(old, new[, count]) ->str

返回S的所有出现的子串的副本旧换新。 如果可选参数计数为给定,只有第一个计数出现被替换。