MySQL is a good open source database, it is now widely used, so it is necessary to briefly introduce the method of using Python to operate the MySQL database. The Python operations database requires the installation of a third-party module, which is downloaded and documented in http://mysql-python.sourceforge.net/.
Since Python's database module has specialized database module specifications, in fact, no matter what kind of database used in a similar way, here is a demonstration of the code:
#-*-encoding:gb2312-*-import os, sys, stringimport mysqldb# connection Database Try:conn = MySQLdb.connect (h ost= ' localhost ', user= ' root ', passwd= ' xxxx ', db= ' test1 ') except Exception, E:print e sys.exit () # Gets the cursor object to manipulate the cursor = Conn.cursor () # CREATE TABLE sql = "CREATE table if not exists test1 (name varchar (+) primary key, age int (4))" Cursor.execute (SQL) # Insert Data sql = "INSERT into test1 (name, age) VALUES ('%s ',%d)"% ("Zhaowei", "") try:cursor.execute (SQL) except Exception, E: Print esql = "INSERT into test1 (name, age) VALUES ('%s ',%d)"% ("Zhang San", +) Try:cursor.execute (SQL) except Exception, E: Print e# insert Multiple sql = "INSERT into test1 (name, age) values (%s,%s)" val = (("John Doe", 24), ("Harry", 25), ("Hung Six", "Up") TRY:CURSOR.E Xecutemany (SQL, Val) except Exception, E:print e# querying out data sql = "SELECT * from Test1" cursor.execute (sql) AllData = cursor.fetc Hall () # If there is data returned, on the loop output, AllData is a two-dimensional list if Alldata:for rec in Alldata:print rec[0], Rec[1]cursor.close () conn.close ()