The premise here is that the MySQL database is already installed on Windows, and the configuration is complete, and the table can be operated normally. On this basis just install Mysql-python-1.2.4b4.win32-py2.7.exe on OK, only 1M more. This is a bit like the jar package in JDBC.
Download Link: http://sourceforge.net/projects/mysql-python/,
Baidu Cloud Disk: Http://pan.baidu.com/s/1dDgnfpR Password: 7BNA
Then import MYSQLDB will be able to use, the following gives the test code:
#coding =utf-8#python operation MySQL Database test code import time, MySQLdb, Sysprint "HelloWorld" #连接conn =mysqldb.connect (host= " localhost ", user=" root ", passwd=" Yanzi ", db=" MyDB ", charset=" UTF8 ") cursor = Conn.cursor () print" Connection succeeded "# #增 # sql =" Insert Into UserInfo (username, pswd) values (%s,%s) "# param = (" haha "," ha11 ") # n = cursor.execute (sql, param) # print n# conn.com MIT () # #更新 # sql = "Update userinfo set pswd =%s where username =%s" # param = ("999999999999", "Zhang San") # Cursor.execute (SQL, param) #删sql = "Delete from UserInfo where username =%s" param = ("Zhang San") n = cursor.execute (sql, param) print nconn.commit () # Check sql = "SELECT * from userinfo" n = cursor.execute (sql) for rows in Cursor.fetchall (): For cols in Rows:print Co Ls,print "" #关闭指针对象和连接cursor. Close () conn.close ()
The overall operation is very similar to JDBC, and it is OK to pass in a SQL and params. In the operation of the introduction of the concept of the cursor, in the SQLite database is also the cursor responsible for the operation, all a meaning. Note that Conn.commit () must be executed after deletion and increment, otherwise the operation is not valid. But in JDBC there is no such thing. Remember to release the cursor and conn when you close the database.
MySQLdb online documentation RELATED LINKS 1 Links 2 Links 3