randint = random.randrange(1, 4) if randint == 1: row['Genre'] = 'CHEMISTRY' if randint == 2: row['Genre'] = 'PHYSICS' if randint == 3: row['Genre'] = 'BIOLOGY' if randint == 4: row['Genre'] = 'ECOLOGY' level.append('3') else: level.append('4') # Open Connection to MySQL cnx = MySQLdb.Connection(user='******', passwd='ticktock', host="chip.swau.edu", db='library') cursor = cnx.cursor() # Creating the add user function add_user = ( "INSERT INTO books (title, lastname, firstname, isbn_16, isbn_10, pub_year," " publisher, category, availability, level, img_url)" "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") for x in range(len(title)): book_title = title[x] # Book title generation isbn10 = random.randrange(1000000000000, 9999999999999) # Generate ISBN 10 isbn16 = random.randrange(1000000000000000, 9999999999999999) # Generate ISBN 16 year = random.randrange(1400, 2017) # Generate a year
# -*- coding: utf-8 -*- from scrapy.spider import Spider import scrapy from scrapy.selector import Selector from scrapy.contrib.spiders import SitemapSpider import re from urlparse import urlparse import MySQLdb conn = MySQLdb.Connection(host="172.26.253.3", user="******", passwd="platform", db="dns_domain", charset="utf8") cursor = conn.cursor() class pachongSpider(Spider): name = "domain" #定义名称 allowed_pamains = ["baidu.com/"] start_urls = [ #定义抓取网页的起始点 "http://site.baidu.com/" ] url_repeat = "www.hao123.com" def parse(self, response): #定义抓取方法 for url in response.xpath('//a/@href').extract(): res = urlparse(url) domain = res.hostname if domain: if self.url_repeat != domain:
def resetDatabase(): """ Reset the entire database """ dbRoot = ReadMySQLConfig('MySQL_root') dbUser = ReadMySQLConfig('MySQL_finclab') try: conn = mdb.Connection(**dbRoot) cursor = conn.cursor() query = """ # Drop user DROP USER IF EXISTS {user}@{host}; FLUSH PRIVILEGES; # Create a new database 'finclab' DROP DATABASE IF EXISTS {db}; CREATE DATABASE {db}; USE {db}; # Create a new user and grant privileges CREATE USER {user}@{host} IDENTIFIED BY '{passwd}'; GRANT ALL PRIVILEGES on {db}.* TO {user}@{host}; FLUSH PRIVILEGES; # Create the 'exchange' table CREATE TABLE exchange ( id int NOT NULL AUTO_INCREMENT, abbrev varchar(32) NOT NULL, name varchar(255) NOT NULL, city varchar(255) NULL, country varchar(255) NULL, currency varchar(64) NULL, timezone_offset time NULL, created_date datetime NOT NULL, last_updated_date datetime NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; # Create the 'data_vendor' table CREATE TABLE data_vendor ( id int NOT NULL AUTO_INCREMENT, name varchar(64) NOT NULL, website_url varchar(255) NULL, support_email varchar(255) NULL, created_date datetime NOT NULL, last_updated_date datetime NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; # Create the 'symbol' table CREATE TABLE symbol ( id int NOT NULL AUTO_INCREMENT, exchange_id int NULL, ticker varchar(64) NOT NULL, instrument varchar(64) NOT NULL, name varchar(255) NULL, sector varchar(255) NULL, currency varchar(32) NULL, created_date datetime NOT NULL, last_updated_date datetime NOT NULL, PRIMARY KEY (id), KEY index_exchange_id (exchange_id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; # Create the 'daily_price' table CREATE TABLE daily_price ( id int NOT NULL AUTO_INCREMENT, data_vendor_id int NOT NULL, symbol_id int NOT NULL, price_date datetime NOT NULL, created_date datetime NOT NULL, last_updated_date datetime NOT NULL, open_price decimal(19,4) NULL, high_price decimal(19,4) NULL, low_price decimal(19,4) NULL, close_price decimal(19,4) NULL, adj_close_price decimal(19,4) NULL, volume bigint NULL, PRIMARY KEY (id), KEY index_data_vendor_id (data_vendor_id), KEY index_symbol_id (symbol_id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; """.format(**dbUser) cursor.execute(query) except mdb.Error as e: print(e) finally: cursor.close() conn.close()
values = table.cell(rows, cols).value # 读出第rows行第cols列的数据 temp_list.append(values) for temp_i in range(ncols): if temp_i == ncols - 1: temp_value_string = temp_value_string + '\'' + temp_list[ temp_i] + '\'' else: temp_value_string = temp_value_string + '\'' + temp_list[ temp_i] + '\'' + ',' temp_value_string += ')' # print temp_value_string # 不理解可在此处打印temp_value_string查看内容 sql_sta = u"INSERT INTO %s VALUES %s" % (SQL_table_name, temp_value_string) # print sql_sta # 此处可打印数据库insert语句 cursor.execute(sql_sta) # 将insert语句交给游标执行 tempDB.commit() if __name__ == '__main__': excel_table_name = u'excelFileName.xls' excel_sheet_name = u'excelTableName' SQL_table_name = u'SQL_TableName' tempDB = MySQLdb.Connection('localhost', 'SQL_Username', 'SQL_Password', 'SQL_DatebaseName', charset='utf8') # tempDB变量: 服务器地 数据库登录名 数据库密码 要导入数据的数据库名 main_pro(excel_table_name, excel_sheet_name, SQL_table_name, tempDB)
import MySQLdb from django.core.management.base import BaseCommand, CommandError from outside_spending.models import * from django.db.models import * dbcfg = settings.DATABASES['default'] assert 'mysql' in dbcfg['ENGINE'].lower( ), "The import_crp_donors command requires a MySQL database." cursor = MySQLdb.Connection(dbcfg['HOST'], dbcfg['USER'], dbcfg['PASSWORD'], dbcfg['NAME']).cursor() def purge_contribs_by_filing_number(filing_num): old_contribs = Contribution.objects.filter(filing_number=filing_num) for oc in old_contribs: oc.delete() class Command(BaseCommand): help = "Removes amended reports, and those that don't fit our time scale" requires_model_validation = False def handle(self, *args, **options): old_summaries = F3X_Summary.objects.filter( coverage_to_date__lte='2011-01-01') for os in old_summaries: print "Deleting F3X: %s %s-%s %s" % ( os.committee_name, os.coverage_from_date, os.coverage_to_date,
( select a.company_id, sum(query_time)/sum(count) avg_response from (select company_id,count(*) count, sum(unix_timestamp(end_query_time) - unix_timestamp(created_at)) query_time from task where created_at >='%s' and created_at <'%s' and query_count =1 group by 1)a group by 1 )response on response.company_id=c.id where c.is_delete='false'; """ wasu_sql2 = """ select c.company_name, sum(count) from (select company_id,count(*) count from task where created_at >='%s' and created_at <'%s' and (unix_timestamp(now())-unix_timestamp(created_at))>10800 and (status='new' or status='query') group by 1 union all select company_id,count(*) count from task where created_at >='%s' and created_at <'%s' and (unix_timestamp(end_query_time)-unix_timestamp(created_at))>10800 group by 1)a, company c where c.id=a.company_id group by 1; """ wasu_update_sql = """update OPM_MediaWise set task_pending_num=task_submitted_num-task_finished_num where report_at='%s' and data_center='EQX';""" try: script_name = __file__ reportInfoGet.def_report_script_status("running", script_name, script_start_time, "ok") conn_pentaho = MySQLdb.Connection(reporting_host, reporting_user, reporting_password, reporting_usedb) cur_pentaho = conn_pentaho.cursor() cur_pentaho.execute("set autocommit=1") conn_wmw = MySQLdb.Connection(db_wmw_host, db_wmw_user, db_wmw_password, db_wmw_usedb, port=db_wmw_port) cur_wmw = conn_wmw.cursor() tmp_wsql1 = wasu_sql1 % (STARTDATE, ENDDATE, STARTDATE, ENDDATE, STARTDATE, ENDDATE, STARTDATE, ENDDATE, STARTDATE, ENDDATE, STARTDATE, ENDDATE, STARTDATE, ENDDATE) tmp_wsql2 = wasu_sql2 % (STARTDATE, ENDDATE, STARTDATE, ENDDATE)
FNULL = open(os.devnull, 'w') if not os.path.exists('./blast_db'): os.makedirs('./blast_db') print '========================================' print 'Establishing connection with MySQL' host = '127.0.0.1' user = '******' password = '******' port = 3306 db = 'bacteria' conn = MySQLdb.Connection(host=host, user=user, passwd=password, port=port, db=db) cursor = conn.cursor() print 'Getting list of organisms...' sql = 'SELECT * FROM `organisms`' cursor.execute(sql) organisms = cursor.fetchall() l = len(organisms) print 'Creating BLAST databases...' count = 0 bar = progressbar.ProgressBar(
#!C:\Python27\python #encoding:utf-8 import urllib2 import json import MySQLdb import socket import sys reload(sys) sys.setdefaultencoding('utf8') UPDATE_RATE = 50 conn = MySQLdb.Connection(host='172.26.253.3', user='******', passwd='platform', db='platform_schema_gd', charset='utf8') cursor = conn.cursor() cursor.execute( "SELECT ip FROM ip_province_result where (country = '' or country is null)" ) #获得要查询的ip ips = cursor.fetchall() sql = "update ip_province_result set country = %s,region = %s,city = %s,isp = %s where ip = %s " i = 0 for ip in ips: apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip[0] try: # headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
#! /usr/bin/env python # -*- coding: utf-8 -*- import MySQLdb # the database connection, adjust to your local configuration db = MySQLdb.Connection( host="localhost", # your host, usually localhost user="******", # your username passwd="root", # your password db="acma_cartoon") # name of the data base db.set_character_set('utf8') cursor = db.cursor() # Load Stopwords stopwords = [] with open("../assets/stopwords_long.txt") as stopwords_file: for line in stopwords_file: stopwords.append(line.rstrip()) class CorpusIterator: """ This class is a memory-friendly way to iterate over the database entries matched by an SQL query :param query The SQL Query :returns entries from the database one by one """ def __init__(self, query): self.query = query pass
## PREVIOUS 10 DAY STOCK SCREENER ## # This section of the apps downloads closing prices for stocks # that made the finviz homepage for various metrics in the past 10 days # # variables used in setting paths for file IO # path = "/Users/ripple/Dropbox/Python/shtf/data/" baseurl = 'http://www.nasdaq.com/symbol/' urltail = '/real-time#.UXA2Iytlei0' #Read the file and send to MySQL db = MySQLdb.Connection(host="127.0.0.1", port=3306, user="******", passwd="", db="shtf") db.autocommit(True) db = db.cursor() # move through files that are listed Day10.txt backwards to Day1.txt for x in range(10, 1, -1): stockcsv = csv.reader(open(path + 'day' + str(x) + '.txt', 'r')) num = x + 1 if x < 10: moveto = open(path + 'day' + str(x + 1) + '.txt', 'w') for row in stockcsv: print 'Getting current data for ' + row[0] + ' ' + row[1] + ' ' + row[ 2] + ' ...' stock = row[0]
def __init__(self, db_host, user, pwd, db_name, charset="utf8", use_unicode = True): print "init begin" print db_host, user, pwd, db_name, charset , use_unicode self.conn = MySQLdb.Connection(db_host, user, pwd, db_name, charset=charset , use_unicode=use_unicode) print "init end"
# -*- coding: UTF-8 -*- import sys reload(sys) sys.setdefaultencoding('utf8') import MySQLdb conn = MySQLdb.Connection(host="127.0.0.1", port=3306, user="******", passwd="root", db="python_mysql_test", charset="utf8") cur = conn.cursor() sql_str = "select * from user" cur.execute(sql_str) rs = cur.fetchall() for row in rs: print "id = %d , name = %s" % row cur.close() conn.close()
http://www.lijiejie.com ''' import httplib import MySQLdb as mdb import threading import Queue import time headers = { 'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)', 'Connection': 'keep-alive' } dbconn = mdb.Connection(host='127.0.0.1', user='******', passwd='******', db='dbname', charset='utf8') cursor = dbconn.cursor() id_queue = Queue.Queue(100) lock = threading.Lock() def check_album_id(): global headers, dbconn, cursor, lock, id_queue while True: albumid = id_queue.get() print 'test album id', albumid if albumid == None: id_queue.task_done() break
def __init__(self, db_host, db_user, db_pass, db_charset, db_unix_socket): self.conn = MySQLdb.Connection(db_host, db_user, db_pass, charset = db_charset, unix_socket = db_unix_socket) print "Connect Mysql successfully!"
#!/usr/bin/env python import MySQLdb import os # Good for backing up old data you aren't working with anymore. # You can always reimport the raw data files if you have the structure ready to go conn = MySQLdb.Connection(host="localhost", user="", passwd='') #conn = MySQLdb.Connection(db="payroll", host="localhost", user="", passwd='') mysql = conn.cursor() cnt = mysql.execute('show databases') print '%s databases' database_names = mysql.fetchall() for db_name in database_names: n = db_name[0] if n == 'information_schema': pass else: if os.path.isdir(n): cmd = 'rm -rf %s' % n os.system(cmd) os.mkdir(n) mysql.execute('use %s' % n) table_cnt = mysql.execute('show tables') print '%s has %s tables' % (n, table_cnt) table_list = mysql.fetchall() for table in table_list:
def graph(i, mdp, csv): db = mdb.Connection(host='localhost', db='cassiopee', passwd=mdp, user='******', charset='utf8') if (i == '1'): query_sector = "select * from sector_instances" df_sector = psql.read_sql(query_sector, con=db) df_sector.plot(x='sname', y='quantity', kind='bar', title='Number of products per sector') export_to_csv(i, csv, df_sector) plt.show() if (i == '2'): query_sector = "select * from icscert_instances" df_sector = psql.read_sql(query_sector, con=db) df_sector.plot(x='icscert', y='quantity', kind='bar', title='Number of CVE per ICSCERT') export_to_csv(i, csv, df_sector) plt.show() if (i == '3'): query_sector = "select * from sfp2_instances" df_sector = psql.read_sql(query_sector, con=db) df_sector.plot(x='sfp2', y='quantity', kind='bar', title='Number of CVE per SFP2') export_to_csv(i, csv, df_sector) plt.show() if (i == '4'): query_sector = "select * from sfp1_instances" df_sector = psql.read_sql(query_sector, con=db) df_sector.plot(x='sfp1', y='quantity', kind='bar', title='Number of CVE per SFP1') export_to_csv(i, csv, df_sector) plt.show()
def connect_to_db(opts): db = MySQLdb.Connection(user=opts.user, passwd=opts.password, db=opts.mysql_db) return db
def main(): count = 0 bme = bme680.BME680(i2c_addr=0x77) # Initialize db con = MySQLdb.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB) c = con.cursor() # c.execute(CREATE TABLE IF NOT EXISTS data (temp FLOAT, pres FLOAT, hum FLOAT, gas FLOAT, lux INT, dbs FLOAT)) # Initialize sensor bme.set_humidity_oversample(bme680.OS_2X) bme.set_pressure_oversample(bme680.OS_4X) bme.set_temperature_oversample(bme680.OS_8X) bme.set_filter(bme680.FILTER_SIZE_3) bme.set_gas_status(bme680.ENABLE_GAS_MEAS) # Initialize USB mic pyaud = pyaudio.PyAudio() stream = pyaud.open(format=pyaudio.paInt16, channels=1, rate=32000, input_device_index=2, input=True) # Main loop while (count < REPEAT): # Record time now = time.strftime('%Y-%m-%d %H:%M:%S') # Read from BME bme.get_sensor_data() tempCelcius = float("{0:.2f}".format(bme.data.temperature)) # Convert the above variable to fahrenheit temperature = float(tempCelcius * (9 / 5) + 32) pressure = float("{0:.2f}".format(bme.data.pressure)) humidity = float("{0:.2f}".format(bme.data.humidity)) gas = float("{0:.2f}".format(bme.data.gas_resistance)) # Read from lux sensor tsl = TSL2561(debug=True) luxVal = tsl.lux() # Read from USB mic rawsamps = stream.read(2048, exception_on_overflow=False) samps = numpy.fromstring(rawsamps, dtype=numpy.int16) decib = analyse.loudness(samps) + 60 print(" " + now) print("BME680--------------------------") print("Temperature: {}".format(temperature)) print("Pressure: {}".format(pressure)) print("Humidity: {}".format(humidity)) print("Gas: {}".format(gas)) print("TSL2561-------------------------") print("Lux: {}".format(luxVal)) print("USB Mic-------------------------") print("Sound in dB: {}".format(decib)) print("________________________________") values = (temperature, pressure, humidity, gas, luxVal, decib, now) add_val = ("INSERT INTO data " "(temp, pres, hum, gas, lux, db, dt)" "VALUES (%s, %s, %s, %s, %s, %s, %s)") c.execute(add_val, values) count += 1 time.sleep(WAIT_PERIOD) con.commit() con.close()
from __future__ import print_function import os import MySQLdb if __name__ == '__main__': conn = MySQLdb.Connection( host='localhost', user='******', port=3306, db='image_classifier', ) conn.query("""SELECT * FROM images""") result = conn.store_result() for i in range(result.num_rows()): row = result.fetch_row() image_id = row[0][0] path = "data-sanitized/%07d.png" % image_id if not os.path.exists(path): with conn.cursor() as cursor: cursor.execute("DELETE FROM images WHERE image_id = %d" % image_id) conn.commit()
def lambda_handler(event, context): root = logging.getLogger() if root.handlers: for handler in root.handlers: root.removeHandler(handler) logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) logger.info("Entered lambda_handler") # Ingore DB warnings, likely duplicates filterwarnings('ignore', category=MySQLdb.Warning) conn = MySQLdb.Connection(host=os.environ['DB_HOST'], user=os.environ['DB_USER'], passwd=os.environ['DB_PASSWORD'], port=int(os.environ['DB_PORT']), db=os.environ['DB_NAME']) userId = os.environ['ENHPASE_USER_ID'] key = os.environ['ENPHASE_KEY'] systemId = os.environ['ENPHASE_SYSTEM_ID'] # set tz code from http://www.localeplanet.com/java/en-AU/index.html tzCode = os.environ['TIME_ZONE'] # free tier only allows so many requests per minute, space them out with delays sleepBetweenRequests = int(os.environ['SLEEP_BETWEEN_REQUESTS']) # Start/end dates to export startDate = pendulum.parse(os.environ["START_DATE"], tzinfo=tzCode) endDate = pendulum.parse(os.environ["END_DATE"], tzinfo=tzCode) # Shouldn't need to modify this url = 'https://api.enphaseenergy.com/api/v2/systems/%s/stats' % systemId logger.info('Starting report between %s and %s', startDate.to_date_string(), endDate.to_date_string()) period = pendulum.period(startDate, endDate) for dt in period.range('days'): time.sleep(1) logger.info('Requesting stats for date [%s] START [%s] END [%s]', dt, dt.start_of('day'), dt.end_of('day')) # HTTP Params params = { 'user_id': userId, 'key': key, 'datetime_format': 'iso8601', 'start_at': dt.start_of('day').int_timestamp, 'end_at': dt.end_of('day').int_timestamp } r = requests.get(url=url, params=params, timeout=5) dailyIntervals = [] if r.status_code == 200: logger.info('Got data for %s', dt.to_date_string()) for interval in r.json()['intervals']: logger.debug('Processing interval %s %s', r.json()['system_id'], interval['end_at']) systemId = r.json()['system_id'] endAt = pendulum.parse(interval['end_at']).to_datetime_string() devicesReporting = interval['devices_reporting'] powr = interval['powr'] enwh = interval['enwh'] thisInterval = (endAt, systemId, devicesReporting, powr, enwh) dailyIntervals.append(thisInterval) else: logger.error( 'Failed to get data for %s, response code %s and body %s', dt.to_date_string(), r.status_code, r.text) logger.info('Inserting %s rows for %s', len(dailyIntervals), dt.to_date_string()) # print(dailyIntervals) x = conn.cursor() try: sql = "INSERT IGNORE INTO stats (end_at, system_id, devices_reporting, powr, enwh) VALUES (%s,%s,%s,%s,%s)" # print(sql % dailyIntervals) x.executemany(sql, dailyIntervals) conn.commit() logger.info('Inserted %s rows for %s', len(dailyIntervals), dt.to_date_string()) except Exception as e: logger.error('Got exception inserting %s %s', dt.to_date_string(), e) # print(x._last_executed) conn.rollback() conn.close()
curs = conn.cursor() #curs.execute("select * from temSensor") #results = curs.fetchall() #for r in results: # print (r) ######################################################### ####fetching flowrates by remotly connecting to RP2###### ######################################################### HOST = "10.208.8.121" PORT = 3306 USER = "******" PASSWORD = "******" DB = "allSensors" connectionR = db.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB) cR = connectionR.cursor() ''' connectionR = db.Connection(host=HOST, port=PORT,user=USER, passwd=PASSWORD, db=DB) cR = connectionR.cursor() cR.execute("SELECT * FROM flowReadings ORDER BY id DESC LIMIT 1") resultsR = cR.fetchall() for rowR in resultsR: print (rowR) ''' #print (db_path) def getLastData():
from getpass import getpass import MySQLdb as mdb import fill_db mdp = getpass(prompt='Veuillez rentrer votre mot de passe mysql: ') while (True): try: db = mdb.Connection(host='localhost', passwd=mdp, user='******', db='cassiopee', charset='utf8') cursor = db.cursor() fd = open("../Modélisation/cassiopee.sql", 'r') sqlFile = fd.read() fd.close() # all SQL commands (split on ';') sqlCommands = sqlFile.split(';') # Execute every command from the input file for command in sqlCommands: # This will skip and report errors # For example, if the tables do not yet exist, this will skip over # the DROP TABLE commands if command.rstrip() != '': cursor.execute(command)
def run(): conn = MySQLdb.Connection(db="macpractice", host="localhost", user="******") mysql = conn.cursor() chrgIds, chrgAmts, chrgPmtamts, wrtOff, amount, personId, patientId, accountId, flname, incidentId = get_good_data(mysql, conn) pmt_tie_ins(mysql, conn, chrgIds, chrgAmts, chrgPmtamts, wrtOff, amount, personId, patientId, accountId, flname, incidentId)
model_names = [ 'ME', 'LEX', 'LE', 'ICE', 'LT', 'SE', 'LFU', 'AR', 'LM', 'TS', 'RT', 'NT', 'SC', 'SL', 'PM', 'OPD' ] model_numbers = [ '120', '320', '520', '2000', '1050', '5500', '1050', '400', '500', '800', '910', '920', '750', '770', '2500', '3000' ] tipologia_problema = [ 'software', 'logoramento', 'difetto di fabbrica', 'elettronico', 'rottura' ] db = MySQLdb.Connection("127.0.0.1", "mysqluser", "mysqlpwd", "tickets") cursor = db.cursor() # tickets for i in range(20000): durata = int(abs(random.gauss(80, 25))) livello = random.randint(1, 2) model_type = random.choice(model_names) model_number = random.choice(model_numbers) modello = 'NIDEK %s-%s' % (model_type, model_number) if livello == 2: duratasecondoliv = int(abs(random.gauss(40, 15))) else: duratasecondoliv = 'NULL' tipo_prob = random.choice(tipologia_problema)
sys.exit() cachefile = cachetype + '_cache' logfile = cachetype + '_service_log' # 设置默认编码为UTF-8,否则从数据库读出的UTF-8数据无法正常显示 reload(sys) sys.setdefaultencoding('utf-8') # 初始化数据库、防火墙信息 querycmd = "cat /etc/sysconfig/iptables" execmd = "sed -i '6i -A INPUT -s %s -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT' /etc/sysconfig/iptables" % local_ip ssh_cds.cds_init(querycmd, execmd, ipaddr, local_ip) # 连接数据库 conn = MySQLdb.Connection(host=ipaddr, user="******", passwd="0rd1230ac", charset="UTF8") conn.select_db('cache') # 创建指针,并设置数据的返回模式为字典 cursor = conn.cursor(MySQLdb.cursors.DictCursor) # 检查icached capture uri数目 ssh_cmd_icached = '/home/icache/icached debug' ssh_result = ssh.main(ssh_cmd_icached) cap_uri_str = int(ssh_result.split('capture uri:')[1].split("\n")[0].strip(" ")) # 启动时间 ISOTIMEFORMAT = '%Y-%m-%d %X' print 'start curl at time:' starttime = time.strftime(ISOTIMEFORMAT, time.localtime()) print starttime
def ConnectToDatabase(self): self.conn = MySQLdb.Connection(self.host, self.user, self.password, self.db) return self.conn
WHERE hotel_id in (%s) """ % ",".join(selection) cursor.execute(query) rows = cursor.fetchall() hotels = [Hotel(r[0], r[1].split(",")) for r in rows] logger.debug("Loaded %d hotels" % len(hotels)) cursor.close() if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(message)s') conn = MySQLdb.Connection(host="localhost", user="******", passwd="rextrebat", db="hotel_genome") logger.info( "[1] Loading chromosomes, categories, chromosome_ids and loc_scales") get_chromosomes() get_categories() get_chromosome_ids() get_loc_scales() logger.info("[2] Loading hotels data") selection = [228014, 125813, 188071, 111189, 212448] #load_hotels(selection) load_hotels() logger.info("[3] Tagging hotel chromosomes")
import numpy as np import matplotlib import matplotlib.pyplot as plt import statsmodels.api as sm # try: # fil = open("getdata.txt", "w") # except: # print "file open failed" # exit() try: recordfile = open("recordtcp2.txt", "w") except: print "open record file failed" exit() con = MySQLdb.Connection(host="localhost", user="******", passwd="lin", port=3306) cur = con.cursor() con.select_db('recordworking') rtt_down_record_rtt = [] rtt_down_record_sequence = [] rtt_down = {} rtt_up = {} resultsamount = 0 sql0 = "select id from winsize order by id desc limit 1;" cur.execute(sql0) result = cur.fetchall() resultsamount = result[0][0] resultsamount /= 10 last = 0
if f!=None: f.close() # f2=open('data_czce_20130711.htm','w') # f2.write(rawdata) # f2.close() # f3= open('data_czce_20130711.htm','r') # rawdata = f3.read() # f3.close() #delete already get data conn = None cursor = None try: conn = MySQLdb.Connection(dbconf.host, dbconf.user, dbconf.password, dbconf.dbname,charset='utf8') cursor = conn.cursor() delete_sql = "delete from lp where og='%s' and dt=%s;\ delete from sp where og='%s' and dt=%s;\ delete from tr where og='%s' and dt=%s;" check_sql = "select count(*) from lp where og='%s' and dt='%s';" cursor.execute(check_sql % ('2',url_date)) logging.info("already get data count need delete:%s" % cursor.fetchall()[0][0]) conn.commit() cursor.execute(delete_sql % (2,url_date,2,url_date,2,url_date)) logging.info(delete_sql % ('郑州',url_date,'郑州',url_date,'郑州',url_date)) except Exception,e: logging.error(" MySQL server exception while delete sql!!!")
print 'no resource ip input' exit() # 设置默认编码为UTF-8,否则从数据库读出的UTF-8数据无法正常显示 reload(sys) sys.setdefaultencoding('utf-8') #初始化数据库、防火墙信息 querycmd = "cat /etc/icache/iptables" execmd = "sed -i '6i -A INPUT -s %s -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT' /etc/icache/iptables" % local_ip ssh_cds.cds_init(querycmd, execmd, ipaddr, local_ip) if redirect_ip != '': ssh_cds.cds_init(querycmd, execmd, redirect_ip, local_ip) # 连接数据库 conn_src = MySQLdb.Connection(host=ipaddr, user="******", passwd="0rd1230ac", charset="UTF8") conn_src.select_db('cache') if redirect_ip != '': conn_red = MySQLdb.Connection(host=redirect_ip, user="******", passwd="0rd1230ac", charset="UTF8") conn_red.select_db('cache') # 创建指针,并设置数据的返回模式为字典 cursor_src = conn_src.cursor(MySQLdb.cursors.DictCursor) if redirect_ip != '': cursor_red = conn_red.cursor(MySQLdb.cursors.DictCursor) #检查icached capture uri数目