1. 程式人生 > >mysql學習(4)python操作數據庫

mysql學習(4)python操作數據庫

mit elf name table inpu cal 名稱 事務回滾 fault

整理了一下前面3期學的內容後,現在練習使用python去操作數據庫

#!python3
# coding:utf-8
import pymysql
class mysql_option():
def __init__(self, host, port, username, pwd, **kwargs):
‘‘‘
如果直接連接到具體的數據庫,只需要傳kwargs,即db = dbname
:param host:
:param port:
:param username:
:param pwd:
:param kwargs:
:return:
‘‘‘
self.conn = self.mysql_connect(host, port, username, pwd, **kwargs)
self.cursor = self.conn.cursor()

@staticmethod
def mysql_connect(host, port, username, pwd, **kwargs):
try:
connect = pymysql.connect(host = host, port = int(port), user = username, passwd = pwd, charset = ‘utf8‘, **kwargs)
print("Mysql數據庫連接成功")
return connect
except Exception as ex:
print("Mysql連接異常,具體報錯如下:\n%s"%(ex))
return False


def create_database(self, dbname):
‘‘‘
創建數據庫
:param dbname: 數據庫名稱
:return:
‘‘‘
try:
create_db_sql = "create database if not exists %s default character set utf8 collate utf_8_general_ci;"%(dbname)
self.cursor.execute(create_db_sql)
result = self.cursor.fetcall()
print("創建%s數據庫成功%s"%(dbname, result))
except Exception as ex:
print("創建%s數據庫失敗,具體原因如下:\n%s"%(dbname, ex))
self.cursor.close()
return False

def select_db(self, db_name):
try:
self.conn.select_db(db_name)
print("連接數據庫%s成功"%(db_name))
except Exception as ex:
print("連接數據庫%s失敗,具體原因如下:\n%s"%(db_name, ex))
self.cursor.close()
return False

def create_table(self, table_name):
create_table_sql = "create table if not exists %s"%(table_name)
try:
self.cursor.execute(create_table_sql)
result = self.cursor.fetcall()
print("創建%s數據表成功%s"%(table_name, result))
except Exception as ex:
print("創建%s數據表失敗,具體原因如下:\n%s"%(table_name, ex))
self.cursor.close()
return False

def sql_option(self, option):
try:
self.cursor.execute(option)
except Exception as ex:
print("操作語句執行失敗,具體原因如下:\n%s"%(ex))
self.conn.rollback() #事務回滾
else:
self.conn.commit()
print("SQL事物提交成功,提交結果:%s"%(self.cursor.rowcount))
finally:
self.cursor.close() #關閉連接

def show_db(self):
self.cursor.execute("show databases;")
result = self.cursor.fetchall()
return result

def show_table(self):
self.cursor.execute("show tables;")
result = self.cursor.fetchall()
return result

def show_desc(self, table_name):
sql_op = "desc %s"%(table_name)
self.cursor.execute(sql_op)
result = self.cursor.fetchall()
return result

def drop_db(self, db_name):
sql_op = "drop database %s"%(db_name)
print("警告:您將刪除數據庫%s,請再次確認刪除!(Y)確認 (N)取消"%(db_name))
confirm = input("請選擇:")
if confirm == "Y" or confirm == "y":
print("開始刪除……")
self.cursor.execute(sql_op)
print("刪除數據庫%s成功"%(db_name))
else:
print("本次操作已取消!")
return False

def drop_table(self, table_name):
sql_op = "drop table %s"%(table_name)
print("警告:您將刪除數據表%s,請再次確認刪除!(Y)確認 (N)取消"%(table_name))
confirm = input("請選擇:")
if confirm == "Y" or confirm == "y":
print("開始刪除……")
self.cursor.execute(sql_op)
print("刪除數據表%s成功%s"%(table_name)

else:
print("本次操作已取消!")

def close_mysql(self):
self.cursor.close()

if __name__ == ‘__main__‘:
mysql_option = mysql_option("192.168.183.128", "3306", "root", "Abc123!", db = ‘testdb‘)
# mysql_option = mysql_option("192.168.183.128", "3306", "root", "Abc123!")
# print(mysql_option.show_db()[4][0])
# db = mysql_option.show_db()[4][0]
# mysql_option.select_db(db)
# # mysql_option.show_table()
# # mysql_option.close_mysql()
# print(mysql_option.show_desc("test_tables"))
mysql_option.drop_db("testdb03")




mysql學習(4)python操作數據庫