In fact, Python does the same thing as doing MySQL transactions:
[email protected] ~]# cat1. py#!/usr/bin/env pythonimport mysqldbdef connect_mysql (): Db_config= { 'Host':'127.0.0.1', 'Port':3306, 'User':'Root', 'passwd':'pzk123', 'DB':'Test'} C= MySQLdb.connect (* *db_config)returnCif__name__ = ='__main__': C=Connect_mysql () # Connect Database First cus=c.cursor () SQL=" # define the Build table statement CREATE TABLE t1 (IDintPrimary Key notNULL, name varchar (Ten) notNULL, AgeintNotNULL ); " " Try: Cus.execute (SQL) # Create data Table C.commit () except Exception asE:c.rollback () Raise efinally: C.close ()
The results are as follows:
" Use test; desc T1; "+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| ID int(one) | NO | PRI | NULL | | | name | varchar | NO | | NULL | | | age int(one) | NO | | NULL | | +-------+-------------+------+-----+---------+-------+
Python Create data table