本文整理汇总了Python中mysql.connector.MySQLConnection.is_connected方法的典型用法代码示例。如果您正苦于以下问题:Python MySQLConnection.is_connected方法的具体用法?Python MySQLConnection.is_connected怎么用?Python MySQLConnection.is_connected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mysql.connector.MySQLConnection
的用法示例。
在下文中一共展示了MySQLConnection.is_connected方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: query_row
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def query_row(table):
"""Returns the next row of a query result set or None
str -> tuple"""
try:
dbconfig = read_db_config() # returns a dict of connection parameters
print("Connecting " + dbconfig["user"] + " to " + dbconfig["database"] + "...")
conn = MySQLConnection(**dbconfig)
if conn.is_connected():
print("Connection established.")
else:
print("Connection failed")
cursor = conn.cursor(buffered=True)
sql_command = "SELECT * FROM " + table
print("Executed command: " + sql_command + ".")
cursor.execute(sql_command)
row = cursor.fetchone()
return row
# The fetchall method is similar but memory-consuming
# rows = cursor.fetchall()
# print('Total rows:', cursor.rowcount)
# return rows
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
示例2: Connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def Connect():
kwargs = ReadingMySQLConfig()
MyConnection = MySQLConnection(**kwargs)
try:
if MyConnection.is_connected():
print("Connected")
except Error as e:
print(e)
finally:
MyConnection.close()
示例3: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
""" Connect to MySQL database """
dbConfig = readDbConfig()
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**dbConfig)
if(conn.is_connected()):
print('Connected to database')
return conn
else:
print('Failed to connect to database')
except Error as error:
print(error)
示例4: __init__
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def __init__(self):
global cursor
global conn
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(user='root', password='New9835', database='geom') # Connect to MySQL database
cursor = conn.cursor()
if conn.is_connected():
print('connection established.')
else:
print('connection failed.')
except Error as error:
print(error)
示例5: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
db_config = read_db_config()
try:
print 'Connecting to database: {0}...'.format(db_config['database'])
conn = MySQLConnection(**db_config)
if conn.is_connected():
print 'Connection established.'
return conn
else:
print 'Connection failed.'
except Error as e:
print e.message
示例6: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
"""Connect to MySQL database
none -> none"""
db_config = read_db_config()
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_config)
if conn.is_connected():
print('Connection established.')
else:
print('Connection failed.')
except Error as e:
print(e)
finally:
conn.close()
print('Connection closed.')
示例7: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
''' Connect to MySql Database'''
db_config = read_db_config()
print db_config
try:
conn = MySQLConnection(**db_config)
print "Connecting to MySql database..."
if conn.is_connected():
print "Connection establish"
else:
print "Connection Failed"
except Error as e:
print "[SOMETHING BAD HAPPEND...]",e
finally:
conn.close()
print 'Connection Closed'
示例8: sql
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def sql(self, args, **kwargs):
if args == 'test':
try:
print('--- Connect to MySQL server:', self.__SQLhost)
connect = MySQLConnection(host=self.__SQLhost, database=self.__SQLbase,
user=self.__SQLuser, password=self.__SQLpass)
if connect.is_connected():
print('--- Connected to MySQL database', self.__SQLbase)
connect.close()
except SQLError as e:
print('***', e)
self.__systest = False
示例9: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect(db_data):
""" Connect to MySQL database """
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_data)
if conn.is_connected():
print('connection established.')
else:
print('connection failed.')
except Error as error:
print(error)
finally:
conn.close()
print('Connection closed.')
示例10: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
""" Connect to MySQL database """
db_config = read_db_config()
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_config)
if conn.is_connected():
print('connection established.')
else:
print('connection failed.')
except Error as error:
print(error)
return conn
示例11: connecting
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connecting():
db_config = read_db_config()
success = False
try:
print('Connecting to database....')
mysqlConn = MySQLConnection(**db_config)
if mysqlConn.is_connected():
print('connection estabslished.')
success = True
else:
print('connection failed')
return mysqlConn
except Error as err:
print(err)
return
示例12: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect(fName='../../conf/config.ini'):
""" Connect to MySQL database """
db_config = read_db_config(fName)
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_config)
if conn.is_connected():
print('connection established.')
else:
print('connection failed.')
except Error as error:
print(error)
finally:
conn.close()
print('Connection closed.')
示例13: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
""" Connect to MySQL database """
db_config = {'password': 'root', 'host': 'localhost', 'user': 'root', 'database': 'stats'}
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_config)
if conn.is_connected():
print('connection established.')
else:
print('connection failed.')
except Error as error:
print(error)
finally:
conn.close()
print('Connection closed.')
示例14: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
"""Connect to mysql database"""
db_config = read_db_config()
try:
"""
conn = mysql.connector.connect(host='localhost',
database='python_mysql',
user='root',
password='tegbeton') """
print("Connecting to MySQL databse...")
conn = MySQLConnection(**db_config)
if conn.is_connected():
print("Connection established")
else:
print("connection failed.")
except Error as e:
print(e)
finally:
conn.close()
print("Connection closed..!")
示例15: connect
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import is_connected [as 别名]
def connect():
""" Connect to MySQL database """
db_config = read_db_config("config.ini", "mysql")
table_config = read_db_config("config.ini", "tabledata")
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_config)
if conn.is_connected():
print('connection established.')
spotifyCall(conn, table_config)
else:
print('connection failed.')
except Error as error:
print(error)
finally:
conn.close()
print('Connection closed.')