本文整理汇总了Python中mysql.connector.MySQLConnection.close方法的典型用法代码示例。如果您正苦于以下问题:Python MySQLConnection.close方法的具体用法?Python MySQLConnection.close怎么用?Python MySQLConnection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mysql.connector.MySQLConnection
的用法示例。
在下文中一共展示了MySQLConnection.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mysql_fetchall
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def mysql_fetchall():
'''Query a MySQL database using fetchall()'''
try:
db_config = read_db_config()
#Creating a new MySQLConnection object
conn = MySQLConnection(**db_config)
#Creating a new MySQLCursor object from the MySQLConnection object
cursor = conn.cursor()
#Selects all rows from the space table
cursor.execute("SELECT * from space")
#selects all the rows in the cursor result set
rows = cursor.fetchall()
print('Number of rows: %d' % cursor.rowcount)
#prints the rows out
for row in rows:
print(row)
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
print('Connection closed.')
示例2: insert_character
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def insert_character(char_id, name, realname, gender, origin, image, siteurl, deck):
query = "INSERT INTO characters(id, name, realname, gender, origin, image, siteurl, deck) " \
"VALUES(%s, %s, %s, %s, %s, %s, %s, %s)"
args = (char_id, name, realname, gender, origin, image, siteurl, deck)
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
except Error as error:
print error
finally:
cursor.close()
conn.close()
示例3: mysql_fetchone
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def mysql_fetchone():
'''Query a MySQL database using fetchone()'''
try:
db_config = read_db_config()
#Creating a new MySQLConnection object
conn = MySQLConnection(**db_config)
#Creating a new MySQLCursor object from the MySQLConnection object
cursor = conn.cursor()
#Selects all rows from the space table
cursor.execute("SELECT * from space")
#selects the next row in the cursor result set
row = cursor.fetchone()
#prints the row out and gets the next row
while row is not None:
print(row)
row = cursor.fetchone()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
print('Connection closed.')
示例4: query_with_fetchone
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def query_with_fetchone(Lista1,v2):
try:
conn = MySQLConnection(host=DB_HOST,user=DB_USER,password=DB_PASS,database=DB_NAME)
cursor = conn.cursor()
cursor.execute("SELECT nombre FROM clientes")
row = cursor.fetchone()
Lista1.delete(0,END)
while row is not None:
d=""
for i in row:
if i== "("or i==")"or i=="'"or i ==",":
pass
else:
d = d+i
Lista1.insert(END,d)
row = cursor.fetchone()
except Error as e:
print(e)
finally:
Lista1.grid(column=1,row=2)
update(v2)
cursor.close()
conn.close()
示例5: insert_user
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def insert_user(name, password, email):
query = "INSERT INTO users(username, password, email) " \
"VALUES(%s,%s,%s)"
args = (name, password, email)
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
示例6: update_book
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def update_book(book_id, title):
# read database configuration
db_config = read_db_config()
# prepare query and data
query = """ UPDATE books
SET title = %s
WHERE id = %s """
data = (title, book_id)
try:
conn = MySQLConnection(**db_config)
# update book title
cursor = conn.cursor()
cursor.execute(query, data)
# accept the changes
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
示例7: get_comments
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def get_comments(self, article_id):
try:
conn = MySQLConnection(**self.db)
query = "SELECT * FROM comment WHERE article_id=%s"
cursor = conn.cursor()
cursor.execute(query, (article_id,))
list_of_comments = list()
row_comment = cursor.fetchone()
if row_comment is None:
return None
while row_comment is not None:
list_of_comments.append(row_comment)
row_comment = cursor.fetchone()
return list_of_comments
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
示例8: insert_imei
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def insert_imei(query, args):
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
mensaje = ('Last insert id: %s' % (cursor.lastrowid,) )
print(mensaje)
else:
mensaje = 'Last insert id not found'
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
return mensaje
示例9: query_with_fetchone
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def query_with_fetchone():
try:
dbconfig = {'password': 'root', 'host': 'localhost', 'user': 'root', 'database': 'stats'}
conn = MySQLConnection(**dbconfig)
cursor = conn.cursor()
cursor.execute("select COUNT(*) from clientBookings where MONTH(CheckInDate) = '2' AND YEAR(CheckInDate) = '2005' AND status = 'A'")
accept = cursor.fetchone()
x = accept[0]
print(accept)
print x
cursor1 = conn.cursor()
cursor1.execute("select COUNT(*) from clientBookings where MONTH(CheckInDate) = '2' AND YEAR(CheckInDate) = '2005' AND status = 'X'")
cancel = cursor1.fetchone()
print(cancel)
cursor2 = conn.cursor()
cursor2.execute("select COUNT(*) from clientBookings where MONTH(CheckInDate) = '2' AND YEAR(CheckInDate) = '2005' AND status = 'p'")
wait = cursor2.fetchone()
print(wait)
cursor3 = conn.cursor()
cursor3.execute("select COUNT(*) from clientBookings where MONTH(CheckInDate) = '2' AND YEAR(CheckInDate) = '2005' AND status = 'R'")
request = cursor3.fetchone()
print(request)
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
示例10: insert_one
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def insert_one(star_name, star_location):
'''Insert one row into a MySQL database'''
query = ("INSERT INTO space(star_name, star_location)"
"VALUES(%s, %s)")
args = (star_name, star_location)
try:
db_config = read_db_config()
#Creating a new MySQLConnection object
conn = MySQLConnection(**db_config)
#Creating a new MySQLCursor object from the MySQLConnection object
cursor = conn.cursor()
#Inserts one row into the database
cursor.execute(query, args)
if cursor.lastrowid:
print('Insert ID: ', cursor.lastrowid)
else:
print('Insert Failure')
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
print('Connection closed.')
示例11: input_data
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def input_data():
for x in range(len(gabungan)):
gabungan[x]
query = "INSERT INTO data_test "
nilai = "VALUES (\"\"," + "\"" + file + "\"" + ","
for x in range(len(gabungan)):
nilai = nilai + str(gabungan[x])
if x < len(gabungan)-1 :
nilai = nilai + ", "
query = query + nilai + ")"
# print query
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
except Error as e:
print('Error:', e)
finally:
cursor.close()
conn.close()
示例12: insert_book
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def insert_book(title, isbn):
query = "INSERT INTO books(title, isbn) " "VALUES(%s, %s)"
args = (title, isbn)
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print ("last insert id", cursor.lastrowid)
else:
print ("last insert id not found")
conn.commit()
except Error as error:
print error
finally:
cursor.close()
conn.close()
示例13: query_row
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [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()
示例14: data_laundry
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def data_laundry(user,saldo_user):
kodeLau = randomTiket()
total = 0
status = ""
print '1. Cuci laundry'
print '2. Cuci laundry + setrika'
print '3. Cuci laundry + setrika + delivery'
pilLaundry = input('Masukan pilihan : ')
berat = input('Masukan berat pakaian : ')
if pilLaundry == 1 :
total = 5000 * berat
elif pilLaundry == 2:
total = 7000 * berat
elif pilLaundry == 3:
total = 7000 * berat + 3000
fix_update = saldo_user - total
if fix_update < 0:
status = "Lunas"
print "Saldo anda tidak mencukupi"
tambah_saldo = raw_input("Ingin menambah saldo anda ? ")
if tambah_saldo == 'y' or 'Y' or 'Ya' or 'YA' or 'ya':
saldoTambah(saldo_user,fix_update,user)
else:
status = "Lunas"
query1 = "INSERT INTO data_user (kodeLau,user,status,total)"\
"VALUES(%s,%s,%s,%s)"
args1 = (kodeLau,user,status,total)
try:
dbConfig = bacaConfig()
connection = MySQLConnection(**dbConfig)
query = connection.cursor()
query.execute(query1,args1)
if query.lastrowid:
print "Data laundry berhasil dimasukan"
query2 = "UPDATE user SET saldo = %s WHERE user = %s"
data2 = (fix_update,user)
try:
dbConfig = bacaConfig()
conn2 = MySQLConnection(**dbConfig)
cursor2 = conn2.cursor()
cursor2.execute(query2,data2)
conn2.commit()
except Error as e:
print (e)
finally:
cursor2.close()
conn2.close()
else:
print "Gagal menginput"
connection.commit()
except Error as e:
print (e)
finally:
query.close()
connection.close()
示例15: login_data
# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import close [as 别名]
def login_data(request):
try:
username = request.POST.get('username','')
phone = request.POST.get('phone', '')
Error_dict = {}
dbconfig = {'password': 'root', 'host': 'localhost', 'user': 'root', 'database': 'login_database'}
conn = MySQLConnection(**dbconfig)
cursor = conn.cursor()
username = "'" + username + "'"
cursor.execute("select COUNT(*) from client_info where Name = " + username + " AND phone_no = " + str(phone) + " ")
count = cursor.fetchone()
if(count[0] <= 0) :
Error_dict['Wrong_values'] = "Wrong Username or Phone no"
else :
context_dict = {}
context_dict['name'] = username
return render_to_response('logged_in_user.html', context_dict)
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
return render_to_response('login_form.html', Error_dict)