Example #1
1
def connect():
    if host.startswith("/"):
        conn = MySQLdb.connect(unix_socket=host, user=user, passwd=pw, db=db)
    else:
        conn = MySQLdb.connect(host=host, user=user, passwd=pw, db=db, port=3306)

    return conn
Example #2
1
    def read_current(config):
        data = []
        chandata = []

        # Read from DB
        db = MySQLdb.connect("localhost", "monitor", "1234", "smartRectifier")
        curs = db.cursor()
        cmd = "SELECT srIBat FROM sensorDataCurrent ORDER BY srIBat ASC LIMIT 1"
        curs.execute(cmd)
        value = curs.fetchone()
        db.close()
        data.append(value[0])

        # Update data to NULL
        db = MySQLdb.connect("localhost", "monitor", "1234", "smartRectifier")
        curs = db.cursor()
        with db:
            cmd = 'UPDATE sensorDataCurrent SET srIBat = NULL WHERE name = "currentData"'
            curs.execute(cmd)
        db.close()

        for i in range(len(data)):
            chandata.append(
                {
                    "name": "Battery Current",
                    "mode": "float",
                    "unit": "Custom",
                    "customunit": "Amp",
                    "LimitMode": 1,
                    "LimitMaxError": 20.0,
                    "LimitMaxWarning": 15.0,
                    "value": float(data[i]),
                }
            )
        return chandata
Example #3
1
    def __init__(self, host="", port=0, user="", passwd="", db="", dbConfig=""):
        try:
            self.sql = u""
            if host != "":
                self.host = host
                self.port = port
                self.user = user
                self.passwd = passwd
                self.db = db
                self.connect = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset="utf8")
            else:

                config = json.loads(
                    open(r"E:\xuweitao\WebSpider\taobao\DBconfigure.json" if dbConfig == "" else dbConfig).read()
                )
                self.host = config["host"]
                self.port = config["port"]
                self.user = config["user"]
                self.passwd = config["passwd"]
                self.db = db if db != "" else config["database"]
                self.connect = MySQLdb.connect(
                    host=config["host"],
                    port=config["port"],
                    user=config["user"],
                    passwd=config["passwd"],
                    db=db if db != "" else config["database"],
                    charset="utf8",
                )
            self.cursor = self.connect.cursor()
        except MySQLdb.Error, e:
            pass
 def generateDBConnector(self, root):
     if root == "QUICKR":
         return MySQLdb.connect(self.QUICKRLOCAL, self.QUICKRROOT, self.QUICKRPASS, self.QUICKRDB)
     if root == "BIKEWALE":
         return MySQLdb.connect(self.BIKEWALELOCAL, self.BIKEWALEROOT, self.BIKEWALEPASS, self.BIKEWALEDB)
     if root == "TEST":
         return MySQLdb.connect(self.QUICKRLOCAL, self.QUICKRROOT, self.QUICKRPASS, self.QUICKRDB)
Example #5
1
    def connect(self):
        if self.currentdb is None or self.config[self.currentdb] is False:
            return False, False

        if not self.currentdb in globalConfig.connection:
            # FIXME: DO NOT TRY HERE
            try:
                globalConfig.connection[self.currentdb] = MySQLdb.connect(
                    host=self.config[self.currentdb]["host"],
                    port=int(self.config[self.currentdb]["port"]),
                    user=self.config[self.currentdb]["user"],
                    passwd=self.config[self.currentdb]["passwd"],
                    db=self.config[self.currentdb]["db"],
                )
                globalConfig.connection[self.currentdb].autocommit(True)
            except Exception, e:
                if "slave_host" in self.config[self.currentdb]:
                    try:
                        globalConfig.connection[self.currentdb] = MySQLdb.connect(
                            host=self.config[self.currentdb]["slave_host"],
                            port=int(self.config[self.currentdb]["slave_port"]),
                            user=self.config[self.currentdb]["slave_user"],
                            passwd=self.config[self.currentdb]["slave_passwd"],
                            db=self.config[self.currentdb]["db"],
                        )
                        globalConfig.connection[self.currentdb].autocommit(True)
                    except Exception, e:
                        if DB.debug is True:
                            print "cannot connect the slave database %s " % e
                        return False, False
                else:
                    if DB.debug is True:
                        print "cannot connect the master database %s no slave" % e
                        return False, False
Example #6
1
    def getConn(type_name):
        if DBFactory.my_kv.has_key(type_name):
            return DBFactory.my_kv[type_name]

        if type_name in ("finance", "adclick", "adupdate", "adpublish"):
            url_key = "hibernate.connection.%s.url" % type_name
            username_key = "hibernate.connection.%s.username" % type_name
            passsword_key = "hibernate.connection.%s.password" % type_name

            url = DBFactory.kv_dict.get(url_key, "")
            _user = DBFactory.kv_dict.get(username_key, "")
            _passwd = DBFactory.kv_dict.get(passsword_key, "")
            if url == "" or _user == "" or _passwd == "":
                sys.exit(1)

            reObj = re.search("jdbc:mysql://(\w+)/(.*?)\?", url)
            if not reObj:
                sys.exit(1)

            _host = reObj.group(1)
            _db = reObj.group(2)

            DBFactory.my_kv[type_name] = MySQLdb.connect(
                host=_host, user=_user, passwd=_passwd, db=_db, port=3306, charset="utf8"
            )

        elif type_name in ("logdb",):
            DBFactory.my_kv[type_name] = MySQLdb.connect(
                host="tb081", user="test", passwd="test", db="logdb", port=3306, charset="utf8"
            )

        return DBFactory.my_kv[type_name]
def connectDatabase():
    """
    Connect to the mysql database, if it fails, go down in flames.
    This will connect to the database of the site we want to work on.
    """
    site = wikipedia.getSite()
    language = site.language()
    family = site.family.name
    conn_start = MySQLdb.connect(
        "sql.toolserver.org", db="toolserver", user=config.db_username, passwd=config.db_password, use_unicode=True
    )
    cursor_start = conn_start.cursor()

    query_start = u"""SELECT dbname, server FROM wiki WHERE family='%s' AND (lang='%s' OR (lang='en' AND is_multilang=1)) LIMIT 1"""
    cursor_start.execute(query_start % (family, language))
    (dbname, server) = cursor_start.fetchone()
    conn_start.close()

    conn = MySQLdb.connect(
        "sql-s" + str(server) + ".toolserver.org",
        db=dbname,
        user=config.db_username,
        passwd=config.db_password,
        use_unicode=True,
    )
    cursor = conn.cursor()
    return (conn, cursor)
def main_function():
    conn = MySQLdb.connect(host="localhost", user="root", passwd="tanzania", db="twitter_analysis")
    hq_conn = MySQLdb.connect(host="localhost", user="root", passwd="tanzania", db="twitter")

    training_tweets = get_test_tweets(conn)
    training_feature_set = process_tweets(training_tweets)

    classifier = DecisionTreeClassifier.train(training_feature_set)

    test_tweets = get_training_tweets(conn)
    test_feature_set = process_tweets(test_tweets)

    classifier_accuracy = accuracy(classifier, test_feature_set)

    alt_full_matrix = {"+": {"+": 0, "-": 0, "E": 0}, "-": {"+": 0, "-": 0, "E": 0}, "E": {"+": 0, "-": 0, "E": 0}}

    # for f in test_tweets:
    # f = test_tweets[0]

    # print f
    # guess = classifier.classify(process_tweet(f[1]))
    # print guess
    # 	update_tweet_polarity(f[0], guess, conn)
    ##	pl = classifier.prob_classify(process_tweet(f[1]))
    # 	idx = f[2]
    # 	if idx == 'I' or idx == 'O':
    # 		idx = 'E'
    # 	alt_full_matrix[idx][guess] += 1

    # print alt_full_matrix

    print "classifier accuracy: " + repr(classifier_accuracy)
def main():
    table_name = sys.argv[1]
    corpus = ReviewCorpus.load("model/" + table_name)

    read_con = MySQLdb.connect(
        host=DB_SERVER, port=DB_SERVER_PORT, user=DB_USER, passwd=DB_PASSWORD, db=DB_NAME, charset="utf8mb4"
    )
    write_con = MySQLdb.connect(
        host=DB_SERVER, port=DB_SERVER_PORT, user=DB_USER, passwd=DB_PASSWORD, db=DB_NAME, charset="utf8mb4"
    )

    read_cursor = read_con.cursor()
    read_cursor.execute(SQL_QUERY % (table_name))

    write_cursor = write_con.cursor()

    for _ in range(read_cursor.rowcount):
        record = read_cursor.fetchone()
        hotel_review = format_review(record[1])
        tfidf_score = 0
        if hotel_review:
            tfidf_score = corpus.tfidf_score(hotel_review)

        write_cursor.execute(SQL_UPDATE % (table_name, tfidf_score, record[0]))
        write_con.commit()
        print "[%f]%s" % (tfidf_score, record[1])

    write_cursor.close()
    write_con.close()

    read_cursor.close()
    read_con.close()
Example #10
0
File: io.py Project: haehn/nipype
    def _list_outputs(self):
        """Execute this module.
        """
        import MySQLdb

        if isdefined(self.inputs.config):
            conn = MySQLdb.connect(db=self.inputs.database_name, read_default_file=self.inputs.config)
        else:
            conn = MySQLdb.connect(
                host=self.inputs.host,
                user=self.inputs.username,
                passwd=self.inputs.password,
                db=self.inputs.database_name,
            )
        c = conn.cursor()
        c.execute(
            "REPLACE INTO %s (" % self.inputs.table_name
            + ",".join(self._input_names)
            + ") VALUES ("
            + ",".join(["%s"] * len(self._input_names))
            + ")",
            [getattr(self.inputs, name) for name in self._input_names],
        )
        conn.commit()
        c.close()
        return None
    def initDb(self, dbConfDict1={}, dbConfDict2={}):
        if len(dbConfDict1) != 0:
            self.dbConfDict1 = dbConfDict1
        if len(dbConfDict2) != 0:
            self.dbConfDict2 = dbConfDict2

        dbConn1 = MySQLdb.connect(
            host=self.dbConfDict1.get("host"),
            user=self.dbConfDict1.get("user"),
            passwd=self.dbConfDict1.get("passwd"),
            port=self.dbConfDict1.get("port"),
            db=self.dbConfDict1.get("db"),
            charset=self.dbConfDict1.get("charset"),
        )
        dbConn2 = MySQLdb.connect(
            host=self.dbConfDict2.get("host"),
            user=self.dbConfDict2.get("user"),
            passwd=self.dbConfDict2.get("passwd"),
            port=self.dbConfDict2.get("port"),
            db=self.dbConfDict2.get("db"),
            charset=self.dbConfDict2.get("charset"),
        )
        cursor1 = dbConn1.cursor()
        cursor2 = dbConn2.cursor()
        self.dbPool["db1"] = cursor1
        self.dbPool["db2"] = cursor2
    def connect_database(self):
        "connects to the mysql database, based on the configuration file with detailed connection to a local/remote host or on a local server with data from my.conf"
        common_parameters = " use_unicode = 'True' , my_conv= {id : int, score : int, content: unicode, title : unicode},cursorclass=MySQLdb.cursors.DictCursor"

        if config.get("server", "use_my_cfg") == True:
            debug_mode("connecting to database with my.cnf", 3)
            self.ttrss_db = MySQLdb.connect(
                host=config.get("server", "host"), db=config.get("server", "database"), read_default_file="~/.my.cnf"
            )

        else:
            debug_mode("connecting to database with login data from config", 3)
            self.ttrss_db = MySQLdb.connect(
                host=config.get("server", "host"),
                user=config.get("server", "user"),
                passwd=config.get("server", "password"),
                db=config.get("server", "database"),
                # port=int (config.get('server','port') )
                use_unicode="True",
                # my_conv= {id : int, score : int, content: unicode, title : unicode},
                cursorclass=MySQLdb.cursors.DictCursor,
            )
        debug_mode("connection established", 3)
        # self.db_cursor = self.ttrss_db.cursor()
        self.db_cursor = self.ttrss_db.cursor()

        self.connected = True
        return self.connected
Example #13
0
def connect_db(host, user, password, dbname):
    """
        (str, str, int, str) -> int
        Given user information, try to connect database.
        Return 0 when success, if the first connection is not successful, try connect 5 times, if failed, return 1 and notify user. 
        Input arguments:
        user: your username
        password: your password
        dbname: the database we are going to log in
        """
    try:
        G.DB = MySQLdb.connect(host=host, user=user, passwd=password, db=dbname)
        print "Database Connected!"
        return 0
    except Exception, err:
        mins = 0
        while mins < 5:
            DB = MySQLdb.connect(host=host, user=user, passwd=password, db=dbname)
            time.sleep(60)
            mins += 1
        print "Fail to connect to database"
        record_log_activity(str(err))
        notify_admin(str(err))
        print traceback.format_exc()
        # Rollback in case there is any error
        DB.rollback()
        return 1
Example #14
0
def report_to_mysql(commit_time, last_commit_hash):
    """Reports the error to mysql"""
    LOG.debug("Report total to MySQL with hall user")
    con = MySQLdb.connect("servcinf", "hall", "hall", "cinfdata")
    cursor = con.cursor()
    query = "INSERT INTO dateplots_hall (time, type, value) VALUES " "(FROM_UNIXTIME(%s), %s, %s)"
    # 164 is pylint errors and 165 is number of lines
    LOG.debug("Using query: {}".format(query))
    LOG.debug("Sending: {}".format((commit_time, 164, sum(ERROR_COUNTER.values()))))
    cursor.execute(query, (commit_time, 164, sum(ERROR_COUNTER.values())))
    LOG.debug("Sending: {}".format((commit_time, 165, TOTAL_LINE_COUNT)))
    cursor.execute(query, (commit_time, 165, TOTAL_LINE_COUNT))
    con.commit()
    con.close()
    LOG.debug("Total number of errors and lines sent to mysql")

    LOG.debug("Report error and file stats to MySQL with pylint user")
    con = MySQLdb.connect("servcinf", "pylint", "pylint", "cinfdata")
    cursor = con.cursor()
    query = "INSERT INTO pylint (time, identifier, isfile, value, commit) " "VALUES (FROM_UNIXTIME(%s), %s, %s, %s, %s)"
    LOG.debug("Using query: {}".format(query))
    # Send error stats
    for key, value in ERROR_COUNTER.items():
        cursor.execute(query, (commit_time, key, False, value, last_commit_hash))
        LOG.debug("Sending: {}".format((commit_time, key, False, value, last_commit_hash)))
    # Send file stats
    for key, value in FILE_COUNTER.items():
        cursor.execute(query, (commit_time, key, True, value, last_commit_hash))
        LOG.debug("Sending: {}".format((commit_time, key, True, value, last_commit_hash)))
    con.commit()
    con.close()
    LOG.debug("Everything else sent to mysql")
Example #15
0
def connect_mysql(instance, role="admin"):
    """Connect to a MySQL instance as admin

    Args:
    hostaddr - object describing which mysql instance to connect to
    role - a string of the name of the mysql role to use. A bootstrap role can
           be called for MySQL instances lacking any grants. This user does not
           exit in zk.

    Returns:
    a connection to the server as administrator
    """
    if role == "bootstrap":
        socket = host_utils.get_cnf_setting("socket", instance.port)
        username = "root"
        password = ""
        db = MySQLdb.connect(unix_socket=socket, user=username, passwd=password, cursorclass=MySQLdb.cursors.DictCursor)

    else:
        username, password = get_mysql_user_for_role(role)
        db = MySQLdb.connect(
            host=instance.hostname,
            port=instance.port,
            user=username,
            passwd=password,
            cursorclass=MySQLdb.cursors.DictCursor,
            connect_timeout=CONNECT_TIMEOUT,
        )
    return db
Example #16
0
    def delete_ul(self, name="ul_live"):
        find_list_sql = 'select l.list_id from list l where l.list_name like "' + name + '%"'
        conn = MySQLdb.connect(self.__dbHost, self.__dbUser, self.__dbPass, self.global_db_name, charset="utf8")
        to_delete_list_id = [str(i[0]) for i in exe_sql(conn, find_list_sql, False)]

        in_param = ",".join(to_delete_list_id)

        del_lc_sql = "delete from list_column where list_id in (" + in_param + ");"
        del_lf_sql = "delete from list_format where list_id in (" + in_param + ");"
        del_l_sql = "delete from list where list_id in (" + in_param + ");"
        exe_sql(conn, del_lc_sql, False)
        exe_sql(conn, del_lf_sql, False)
        exe_sql(conn, del_l_sql, False)
        conn.commit()
        close_conn(conn)
        #        print del_lc_sql;
        #        print del_lf_sql;
        #        print del_l_sql;

        list_conn = MySQLdb.connect(
            self.__list_db_host, self.__dbUser, self.__dbPass, self.list_db_name, charset="utf8"
        )
        del_subcribe_sql = [exe_sql(list_conn, "drop table l%s_subscriber;" % (id), False) for id in to_delete_list_id]
        list_conn.commit()
        close_conn(list_conn)

        print in_param
Example #17
0
File: db.py Project: tuksik/evette
def GetConnection(dbsettings, dbname="evette"):

    dbip = dbsettings.dbip
    dbuser = dbsettings.dbuser
    dbpass = dbsettings.dbpass

    try:
        if dbpass == "" or str(dbpass) == "None" or str(dbpass) == "False":
            connection = MySQLdb.connect(host=dbip, user=dbuser, db=dbname)
        else:
            connection = MySQLdb.connect(host=dbip, user=dbuser, db=dbname, passwd=dbpass)

        try:
            connection.set_character_set("utf8")
        except:
            pass

            # except MySQLdb.Error, e:

            # print "db: GetConnection: error: %d: %s " % (e.args[0], e.args[1])
            # success = False
            # connection = False

    except:
        connection = False

    return connection
def loading_labelData(sql, data):
    if data == "facebook":
        # load data to server
        db = MySQLdb.connect(
            host="10.0.106.71",  # your host, usually localhost
            user="vdthoang",  # your username
            passwd="LARCuser1142",  # your password
            db="nec_demo",
        )  # name of the data base
    elif data == "twitter":
        # load data to server
        db = MySQLdb.connect(
            host="localhost",  # your host, usually localhost
            user="root",  # your username
            passwd="ducthong",  # your password
            db="2015_allschemas",
        )  # name of the data base
    else:
        print "You need to give the correct command"
        quit()

    cur = db.cursor()
    # Use all the SQL you like
    cur.execute(sql)

    list_row_id = list()
    # print all the first cell of all the rows
    for row in cur.fetchall():
        list_row_id.append(row[0])

    return list_row_id
Example #19
0
def db_diff():

    if not ssh_tunnel:
        remote_conn = MySQLdb.connect(
            host=remote_mysql_host, port=remote_mysql_port, user=remote_user, passwd=remote_passwd, db=remote_db
        )

        local_conn = MySQLdb.connect(
            host=local_mysql_host, port=local_mysql_host, user=local_user, passwd=local_passwd, db=local_db
        )

        return db_compare(remote_conn, local_conn, db_compare_name)
    else:
        with SSHTunnelForwarder(
            (ssh_hostname, ssh_port),
            ssh_password=ssh_password,
            ssh_username=ssh_username,
            remote_bind_address=(remote_mysql_host, remote_mysql_port),
        ) as server:

            remote_conn = MySQLdb.connect(
                host="127.0.0.1", port=server.local_bind_port, user=remote_user, passwd=remote_passwd, db=remote_db
            )

            local_conn = MySQLdb.connect(
                host=local_mysql_host, port=local_mysql_port, user=local_user, passwd=local_passwd, db=local_db
            )

            return db_compare(remote_conn, local_conn, local_db)
Example #20
0
 def connect(self):
     """connect to the host"""
     if self.conn:
         return
     try:
         print "self.host=", self.host
         # print 'self.charset=', self.charset
         # print 'self.unix_socket=', self.unix_socket
         # raw_input()
         if self.unix_socket:
             self.conn = _mysql.connect(
                 host=self.host,
                 user=self.user,
                 passwd=self.passwd,
                 db=self.dbname,
                 port=self.port,
                 charset=self.charset,
                 unix_socket=self.unix_socket,
             )
         else:
             # print 'self.host=', self.host
             self.conn = _mysql.connect(
                 host=self.host,
                 user=self.user,
                 passwd=self.passwd,
                 db=self.dbname,
                 port=self.port,
                 charset=self.charset,
             )
     except _mysql.Error, e:
         print "mysql error %s: %s" % (e.args, e.message)
         raise e
    def get(self):
        
        if (os.getenv('SERVER_SOFTWARE') and   os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):

                    db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='earthquakes', user='root', charset='utf8', passwd='tas')
                #bucket_name = os.environ.get('BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
                
                
        else:
        
                    db = MySQLdb.connect(host='localhost', port=3306, db='earthquakes', user='root', charset='utf8', passwd='tas')
            
            
        bucket = "/summ/"
        filename = bucket + "allmonth.csv"
            
        gcs_file = cloudstorage.open(filename, 'r')
        fileReader = csv.reader(gcs_file)
        

   
  
        cursor = db.cursor()

            
            
        self.response.out.write(Main_page_html)
        
        db.commit()
        db.close() 
Example #22
0
def _connection(db=None):
    if db:
        _db = MySQLdb.connect(db=db, read_default_file="~/.my.cnf")
    else:
        _db = MySQLdb.connect(read_default_file="~/.my.cnf")
        # _db = MySQLdb.connect(host="127.0.0.1", user="pwinters", passwd="bonneaulab", port=13307,db="hpf")
    return _db
    def get(self):
        user = users.get_current_user()
        if not user:
            self.redirect("/")
        else:
            email = user.email()

            if os.getenv("SERVER_SOFTWARE") and os.getenv("SERVER_SOFTWARE").startswith("Google App Engine/"):
                db = MySQLdb.connect(
                    unix_socket="/cloudsql/hack-the-north-1:its-not-django", db="musicsite", user="root"
                )
            else:
                db = MySQLdb.connect(host="localhost", user="root", passwd="htndjango", db="musicsite")

            cursor = db.cursor()
            result = cursor.execute(
                'SELECT community_name,community_id FROM users WHERE email = "%s" AND invite_accepted = 0 AND invite_hidden=0;'
                % email
            )
            count = cursor.rowcount

            if count == 0:
                template_messages = {"message": "You have no pending invites!"}
            else:
                # known bug where if you have two pending invites of the same name, accepting will cause you to accept both
                pendinglist = []
                for row in cursor:
                    pendinglist.append(row[0])
                template_messages = {"message": "You have %s pending invite(s)!" % count, "invites": pendinglist}
            db.close()

            self.render_response("join_community_page.html", **template_messages)
Example #24
0
def create_database(host, user, passwd, db):
    try:
        mysql = MySQLdb.connect(unix_socket="/tmp/mysql.sock", host=host, user=user, passwd=passwd, db=db)
    except MySQLdb.Error:
        mysql = MySQLdb.connect(unix_socket="/tmp/mysql.sock", host=host, user=user, passwd=passwd)
        mysql.query("create database pygenomics")
        mysql = MySQLdb.connect(unix_socket="/tmp/mysql.sock", host=host, user=user, passwd=passwd, db=db)

    mysql.query("set names utf8")

    """create table "good_genes" to host all sequences for genes that have to
		be sequenced because are < 300bp"""

    table = "CREATE TABLE IF NOT EXISTS `goodGenes` ( \
				`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, \
				`geneCode` varchar(255) DEFAULT NULL, \
				`geneName` varchar(255) DEFAULT NULL, \
				`code` varchar(255) DEFAULT NULL, \
				`sequence` text DEFAULT NULL, \
				`timestamp` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', \
				PRIMARY KEY (`id`), \
				UNIQUE KEY `id` (`id`) \
				) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8"
    mysql.query(table)

    return mysql
Example #25
0
File: db.py Project: tuksik/evette
def CreateDatabase(dbsettings):

    success = False
    dbip = dbsettings.dbip
    dbuser = dbsettings.dbuser
    dbpass = dbsettings.dbpass

    if dbpass == "" or dbpass == "None" or dbpass == "False":

        connection = MySQLdb.connect(host=dbip, user=dbuser)

    else:
        connection = MySQLdb.connect(host=dbip, user=dbuser, passwd=dbpass)

    action = "CREATE DATABASE evette"
    SendSQL(action, connection)
    connection.close()

    connection = GetConnection(dbsettings)

    # print "Creating all tables"

    dbmethods.CreateAllTables(connection)

    # print "All tables created"

    connection.close()

    success = True

    return success
def open_master_connection():
    """
    Open a connection on the master
    """
    if options.defaults_file:
        conn = MySQLdb.connect(read_default_file=options.defaults_file)
        config = ConfigParser.ConfigParser()
        try:
            config.read(options.defaults_file)
        except:
            pass
        username = config.get("client", "user")
        password = config.get("client", "password")
        port_number = int(config.get("client", "port"))
    else:
        username = options.user
        port_number = options.port
        if options.prompt_password:
            password = getpass.getpass()
        else:
            password = options.password
        conn = MySQLdb.connect(
            host=options.host, user=username, passwd=password, port=options.port, unix_socket=options.socket
        )
    return conn, username, password, port_number
Example #27
0
File: db.py Project: tuksik/evette
def CheckServer(dbsettings):

    dbip = dbsettings.dbip
    dbuser = dbsettings.dbuser
    dbpass = dbsettings.dbpass

    try:
        if dbpass == "" or dbpass == "None" or dbpass == "False":

            connection = MySQLdb.connect(host=dbip, user=dbuser)

        else:
            connection = MySQLdb.connect(host=dbip, user=dbuser, passwd=dbpass)

        connection.close()
        success = True

        # except MySQLdb.Error, e:
        # print "dbip = ",dbip,", dbuser = ",dbuser,", dbpass = ",dbpass
        # print "db: CheckServer: error: %d: %s " % (e.args[0], e.args[1])
        # success = False

    except:
        success = False

    return success
def data_import(data_file, table, logger):
    logger.info("importing data from {} into table {}".format(data_file, table))
    connection = mdb.connect(host="localhost", db="FamilyCpolymerases", user="sequser")
    cursor = connection.cursor()
    tables = check_tables(cursor)
    if table not in tables:
        try:
            connection = mdb.connect(host="localhost", db="FamilyCpolymerases", user="sequser")
            cursor = connection.cursor()
            logger.info("table does not exist, creating table %s", table)
            create_table(table, logger)
            time.sleep(1)
            tables = check_tables(cursor)
            if table not in tables:
                logger.debug("why cannot it find the damn table!")
        except mdb.OperationalError:
            pass
    #            logger.error('unable to create table, checking...')
    #            tables = check_tables(cursor)
    #            logger.error('tables in db: {}'.format(str(tables)))
    #            logger.error('skipping to next table')
    #            time.sleep(1)
    #            return
    sql = "load data local infile '{}' ignore into table `{}` fields terminated by '\t' (Header, Sequence)"
    logger.debug("load data statement for {}: {}".format(table, sql.format(data_file, table)))
    cursor.execute(sql.format(data_file, table))
    connection.commit()
    connection.close()
    logger.info("data imported into table %s", table)
    return extract_accession_numbers(table, logger)
def make_corrected_epoch(dev_id, date_1, time_1, date_2, time_2):
    # Make lists of desired values
    info_1 = date_1.split("-") + time_1.split(":")
    info_2 = date_2.split("-") + time_2.split(":")

    # Convert times/dates into epoch time
    time1 = calendar.timegm((int(info_1[0]), int(info_1[1]), int(info_1[2]), int(info_1[3]), int(info_1[4]), 0))

    time2 = calendar.timegm((int(info_2[0]), int(info_2[1]), int(info_2[2]), int(info_2[3]), int(info_2[4]), 0))

    # Find UTC offset of device
    if os.getenv("SERVER_SOFTWARE", "").startswith("Google App Engine"):
        db = MySQLdb.connect(unix_socket="/cloudsql/bloomsky-backend-test:bloomsky", db="bloomsky", user="root")
    else:
        db = MySQLdb.connect(host="173.194.248.186", port=3306, user="wei", passwd=">lO9*iK,", db="bloomsky")
    cursor = db.cursor()
    cursor.execute("SELECT DISTINCT UTC FROM bloomsky.appi_skydevice WHERE DeviceID='" + str(dev_id) + "';")
    offset = int(cursor.fetchone()[0])
    db.close()

    # # Find UTC offset of device
    # conn = rethinkdb.connect(host='23.236.61.183',port=28015,db='bloomsky',auth_key='5Hx8X}Rx%6g`\rHkBXj+')
    # cursor = rethinkdb.table("skydata").filter({"DeviceID": dev_id}).order_by(rethinkdb.desc("TS")).limit(1).pluck("UTC").run(conn)
    # offset = int(cursor[0]["UTC"])
    # conn.close()

    # Correct time for UTC offset
    time1 = time1 - (int(offset) * 60 * 60)
    time2 = time2 - (int(offset) * 60 * 60)

    return [dev_id, time1, time2, int(offset)]
Example #30
0
File: role.py Project: ema/conpaas
    def take_snapshot(self):
        """1st session
        """
        db1 = MySQLdb.connect(self.conn_location, "root", self.conn_password)
        exc = db1.cursor()
        exc.execute("FLUSH TABLES WITH READ LOCK;")
        """2nd session
        """
        db2 = MySQLdb.connect(self.conn_location, "root", self.conn_password)
        exc = db2.cursor()
        exc.execute("SHOW MASTER STATUS;")
        rows = exc.fetchall()
        db2.close()
        i = 0
        ret = {}
        for row in rows:
            i = i + 1
            ret["position" + str(i)] = {"binfile": row[0], "position": row[1], "mysqldump_path": self.mysqldump_path}

        # dump everything except test?
        os.system(
            "mysql --user=root --password="
            + self.conn_password
            + " --batch --skip-column-names "
            + '--execute="SHOW DATABASES" | egrep -v "information_schema|test" '
            + "| xargs mysqldump --user=root --password="
            + self.conn_password
            + " --lock-all-tables --databases > "
            + self.mysqldump_path
        )

        exc = db1.cursor()
        exc.execute("UNLOCK TABLES;")
        db1.close()
        return ret