python backend database link ----- MySQLdb

Please confirm good connection to the database before the following matters:

  1. It has been suggested good corresponding database

  2. In the database has been established in the corresponding table

  3. The module is installed already MySQldb

 

Example:

Import MySQLdb # Open Database Connectivity DB = MySQLdb . Connect ( "localhost" , "testuser" , "test123" , "the TESTDB" , charset = 'UTF8' ) # using the cursor () method to get the operating cursor Cursor = DB . Cursor () sql = "" "you want to perform a database operation statement" "" 

       
the try : # execute SQL statements  the Cursor . the Execute ( SQL ) # submitted to the database db . the commit () the except : # rollback when an error occurs db . ROLLBACK ()
      
Using either fetchone # () method to obtain a data Data = Cursor . Either fetchone () Print "Database Version:% S" % Data # close the connection database DB . Close ()   

 

Guess you like

Origin www.cnblogs.com/ouwen-lengyu/p/11940031.html