def saved(res, database='1', db_name='test', username='root', passwd='a8416459'): # ??????,mysql?mongodb
if database == '1':
try:
conn = MySQLdb.Connection(
user=username, passwd=passwd, db=db_name, use_unicode=True)
cursor = conn.cursor() # ??cursor
cursor.execute('drop table urls') # ??????varchar(10)
cursor.execute(
'create table if not exists urls(ID tinyint primary key,URL varchar(50),CTIME timestamp not null default current_timestamp)')
k = 0
for i in res:
k += 1
# ???%s?????,???int??
cursor.execute(
"insert into urls (ID,URL) values (%s,%s)", [str(k), i])
cursor.execute('select * from urls')
return True
except Exception, e:
print e
return False
finally:
cursor.close()
conn.close()
else: # ??mongodb
ISOTIMEFORMAT = '%Y-%m-%d %X'
try:
conn = pymongo.MongoClient(host='localhost', port=27017)
db = conn[db_name]
coll = db['urls']
# coll.drop() #???????url??
coll.remove() # ??url??????
for i in res:
coll.insert({'URL': i, 'CTIME': time.strftime(
ISOTIMEFORMAT, time.localtime(time.time()))})
except Exception, e:
print e
finally:
conn.close()
return True
评论列表
文章目录