Python MySQLdb.Connection() Examples

The following are code examples for showing how to use MySQLdb.Connection(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: com_top_whois_service   Author: h-j-13   File: db_opreation.py    (license) View Source Project 6 votes vote down vote up
def db_connect(self):
        """?????"""
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host,
                    port=self.port,
                    user=self.user,
                    passwd=self.passwd,
                    charset=self.charset,
                    use_unicode=False)
            except MySQLdb.Error, e:
                log_db.error('connect error:' + str(e))
            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise (NameError, "Connect failure")
            log_db.warning("???????")
            self.db_lock.release() 
Example 2
Project: Malicious_Domain_Whois   Author: h-j-13   File: db_opreation.py    (license) View Source Project 6 votes vote down vote up
def db_connect(self):
        """?????"""
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host,
                    port=self.port,
                    user=self.user,
                    passwd=self.passwd,
                    charset=self.charset,
                    use_unicode=False)
            except MySQLdb.Error, e:
                log_db.error('connect error:' + str(e))
            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise (NameError, "Connect failure")
            log_db.warning("???????")
            self.db_lock.release() 
Example 3
Project: Malicious_Domain_Whois   Author: h-j-13   File: database.py    (license) View Source Project 6 votes vote down vote up
def db_connect(self):
        """?????"""
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host,
                    port=self.port,
                    user=self.user,
                    passwd=self.passwd,
                    charset=self.charset)
            except MySQLdb.Error, e:
                print ('connect error:' + str(e))
            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise (NameError, "Connect failure")
            print ("???????")
            self.db_lock.release() 
Example 4
Project: proxy_ip   Author: hereisok   File: db_operation.py    (license) View Source Project 6 votes vote down vote up
def get_connect(self):
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host = self.host, user = self.user, passwd = self.passwd, charset = self.charset)
            except MySQLdb.Error, e:
                print 'get_connect error_info: %d: %s' % (e.args[0], e.args[1])
            
            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise(NameError, "Connect failure")

            # # ????
            # try:
            #     self.cursor.execute('SET character_set_connection=utf8, character_set_results=utf8, character_set_client=utf8')
            # except MySQLdb.Error, e:  
            #     print str(datetime.datetime.now()).split(".")[0], "ERROR %d: %s" % (e.args[0], e.args[1])
            self.db_lock.release()

    # ??????? 
Example 5
Project: micro-meteorological-station   Author: touchEngine   File: display.py    (license) View Source Project 6 votes vote down vote up
def get_data():
	global data_dict
	conn = MySQLdb.Connection(host="localhost", user="root", passwd="123456",
charset="UTF8")
	conn.select_db('mms')
	cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
	cursor.execute("select * from t_data where item_id = (select max(item_id)from t_data)")
	conn.commit()
	tmp = cursor.fetchall()[0]
	data_dict['humidity'].set(tmp['humidity'])
	data_dict['temperature'].set(tmp['temperature'])
	data_dict['pressure'].set(tmp['pressure'])
	data_dict['sea_level_press'].set(tmp['sea_level_press'])
	data_dict['illuminance'].set(tmp['illuminance'])
	data_dict['API'].set(tmp['api'])
	data_dict['location'].set(0)
	data_dict['altitude'].set(tmp['altitude'])
	data_dict['rainfall'].set(tmp['rainfall'])
	cursor.close()
	conn.close()
	return data_dict 
Example 6
Project: Malicious_Domain_Whois   Author: h-j-13   File: DomainVerify.py    (license) View Source Project 5 votes vote down vote up
def get_connect(self):
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host, user=self.user, passwd=self.passwd, charset=self.charset)
            except MySQLdb.Error, e:
                print str(datetime.datetime.now()).split(".")[0], "ERROR %d: %s" % (e.args[0], e.args[1])

            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise(NameError, "Connect failure")
            self.db_lock.release()

    #????? 
Example 7
Project: Malicious_Domain_Whois   Author: h-j-13   File: Time_test.py    (license) View Source Project 5 votes vote down vote up
def get_connect(self):
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host, user=self.user, passwd=self.passwd, charset=self.charset)
            except MySQLdb.Error, e:
                print str(datetime.datetime.now()).split(".")[0], "ERROR %d: %s" % (e.args[0], e.args[1])

            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise(NameError, "Connect failure")
            self.db_lock.release()

    #????? 
Example 8
Project: Malicious_Domain_Whois   Author: h-j-13   File: DB_opreation.py    (license) View Source Project 5 votes vote down vote up
def get_connect(self):
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host, user=self.user, passwd=self.passwd, charset=self.charset)
            except MySQLdb.Error, e:
                print str(datetime.datetime.now()).split(".")[0], "ERROR %d: %s" % (e.args[0], e.args[1])

            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise(NameError, "Connect failure")
            self.db_lock.release()

    # ????? 
Example 9
Project: Malicious_Domain_Whois   Author: h-j-13   File: count_log.py    (license) View Source Project 5 votes vote down vote up
def get_connect(self):
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host, user=self.user, passwd=self.passwd, charset=self.charset)
            except MySQLdb.Error, e:
                print str(datetime.datetime.now()).split(".")[0], "ERROR %d: %s" % (e.args[0], e.args[1])

            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise(NameError, "Connect failure")
            self.db_lock.release()

    # ????? 
Example 10
Project: CommonScripts   Author: kushao1267   File: extract_urls.py    (license) View Source Project 5 votes vote down vote up
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 
Example 11
Project: MyLib   Author: h-j-13   File: DB_opreation.py    (license) View Source Project 5 votes vote down vote up
def get_connect(self):
        if self.db_lock.acquire():
            try:
                self.conn = MySQLdb.Connection(
                    host=self.host, user=self.user, passwd=self.passwd, charset=self.charset)
            except MySQLdb.Error, e:
                print str(datetime.datetime.now()).split(".")[0], "ERROR %d: %s" % (e.args[0], e.args[1])

            self.cursor = self.conn.cursor()
            if not self.cursor:
                raise(NameError, "Connect failure")
            self.db_lock.release()

    # ?????