Python operates the MySQL database, including creating tables, deleting tables, inserting data, deleting data, modifying data, and so on.
#-*-Coding:utf-8-*-
Import Mysql.connector
Import OS
Class MySQLdb:
#是否打印sql
Print_sql = True
#数据库连接
Mysql_conn = None
def __init__ (self):
' Initialize database connection '
Self.mysql_conn = Self.get_conn ()
def get_conn (self):
#return Mysql.connector.connect (user= ' root ', password= ', database= ' test ', use_unicode=true)
Return Mysql.connector.connect (user= ' root ', password= ', database= ' python_test ', charset= ' UTF8 ')
def commit (self):
"Commit database Transaction"
If Self.mysql_conn is not None:
Self.mySql_Conn.commit ()
def get_cursor (self):
‘‘‘
The method is to get the database's cursor object, and the parameter is the connection object of the database
If the connection object for the database is not none, the database connection object that is created is returned
Cursor object, otherwise a cursor object that is an in-memory data
Cursor objects created by the Library connection object
‘‘‘
If Self.mysql_conn is not None:
Return Self.mySql_Conn.cursor ()
Else
Return Self.mySql_Conn.cursor ()
def close_cursor (self,cursor):
"' Close database Cursor object and database connection object '
Try
If cursor is not None:
Cursor.close ()
Finally
If cursor is not None:
Cursor.close ()
################################################################
#创建表, delete table operations
################################################################
def create_table (self, strSQL):
"' Create database table: ' '
If strSQL is not None and strSQL! = ":
cursor = Self.get_cursor ()
If Self.print_sql:
Print (' Execute sql:[{}] '. Format (strSQL))
Cursor.execute (strSQL)
Self.commit ()
Print (' Create database table successfully! ')
Self.close_cursor (Cursor)
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
def drop_table (self,table):
If the table exists, delete the table and, if there is data in the table, use the
Use caution when using the method! ‘‘‘
If table is not None and table! = ':
strSQL = ' DROP table IF EXISTS ' + table
If Self.print_sql:
Print (' Execute sql:[{}] '. Format (strSQL))
cursor = Self.get_cursor ()
Cursor.execute (strSQL)
Self.commit ()
Print (' Delete database table [{}] succeeded! '. Format (table))
Self.close_cursor (Cursor)
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
#####################################################################
#数据库操作
#####################################################################
def insert_multidata (Self,strsql, data):
"Insert Data"
If strSQL is not None and strSQL! = ":
If data is not None:
cursor = Self.get_cursor ()
For D in data:
If Self.print_sql:
Print (' Execute sql:[{}], parameter: [{}] '. Format (strSQL, D))
Cursor.execute (strSQL, D)
Self.commit ()
Self.close_cursor (Cursor)
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
def insert_data (Self,strsql):
"Insert Data"
If strSQL is not None and strSQL! = ":
cursor = Self.get_cursor ()
Print (' Execute sql:[{}] '. Format (strSQL))
Cursor.execute (strSQL)
Self.commit ()
Self.close_cursor (Cursor)
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
def get_all_item (Self,strsql):
"Query all Data"
If strSQL is not None and strSQL! = ":
cursor = Self.get_cursor ()
If Self.print_sql:
Print (' Execute sql:[{}] '. Format (strSQL))
Cursor.execute (strSQL)
Listr = Cursor.fetchall ()
Self.close_cursor (Cursor)
Return LISTR
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
Return None
def get_one_item (Self,strsql, data):
"Query a piece of data"
If strSQL is not None and strSQL! = ":
If data is not None:
#Do this instead
D = (data,)
cursor = Self.get_cursor ()
If Self.print_sql:
Print (' Execute sql:[{}], parameter: [{}] '. Format (strSQL, data))
Cursor.execute (strSQL, D)
R = Cursor.fetchall ()
If Len (r) > 0:
For e in range (Len (r)):
Print (R[e])
Else
Print (' The [{}] equal none! '. Format (data))
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
def update_data (Self,strsql, Data):
"Update Data"
If strSQL is not None and strSQL! = ":
If data is not None:
cursor = Self.get_cursor ()
For D in data:
If Self.print_sql:
Print (' Execute sql:[{}], parameter: [{}] '. Format (strSQL, D))
Cursor.execute (strSQL, D)
Self.commit ()
Self.close_cursor (Cursor)
Else
Rint (' The [{}] is empty or equal none! '. Format (strSQL))
def delete_multidata (Self,strsql, data):
"Delete more than one SQL data"
If strSQL is not None and strSQL! = ":
If data is not None:
cursor = Self.get_cursor ()
For D in data:
If Self.print_sql:
Print (' Execute sql:[{}], parameter: [{}] '. Format (strSQL, D))
Cursor.execute (strSQL, D)
Self.commit ()
Self.close_cursor (Cursor)
Else
Rint (' The [{}] is empty or equal none! '. Format (strSQL))
def delete_data (Self,strsql):
"Delete a SQL data"
If strSQL is not None and strSQL! = ":
If Self.print_sql:
Print (' Execute sql:[{}] '. Format (strSQL))
cursor = Self.get_cursor ()
Cursor.execute (strSQL)
Self.commit ()
Self.close_cursor (Cursor)
Else
Print (' The [{}] is empty or equal none! '. Format (strSQL))
#########################################################################
#测试代码
#########################################################################
db = MySQLdb ()
#删除数据表
#db. drop_table (' person ')
#ENGINE =innodb DEFAULT Charset=utf8
#创建数据库表
Create_table_sql = ' CREATE table ' person ' (
' id ' int (one) is not NULL,
' Name ' varchar (not NULL),
' Gender ' varchar (4) DEFAULT NULL,
' Age ' int (one) DEFAULT NULL,
' Address ' varchar (+) DEFAULT NULL,
PRIMARY KEY (' id ')
);‘‘‘
#db. Create_table (Create_table_sql)
#删除数据,
Delsql= ' Delete from person '
#db. Delete_data (Delsql)
#插入数据测试, insert a statement
Insert_sql = ' INSERT into ' VALUES (3, ' Xiaoli ', ' female ', 18, ' Shandong ') '
#db. Insert_data (Insert_sql)
#插入数据测试, insert multiple statements
Insert_sql = ' INSERT into ' values (%s,%s,%s,%s,%s) '
data = [[1, ' Xiaowang ', ' Male ', 20, ' Guangdong '],
[2, ' Xiaozhang ', ' Male ', 22, ' Henan '],
[3, ' Xiaoli ', ' Male ', 18, ' Shandong '],
[4, ' Xiaoliu ', ' female ', 21, ' Shanxi ']
Db.insert_multidata (Insert_sql,data)
#查询数据
A= db.get_all_item (' select * from person ')
For item in a:
For it in item:
Print it
Python operation MySQL Database