Install MySQLdb in Windows, and use Python to add, delete, modify, and query the MySQL database.
The premise here is that the MySQL database has been installed on windows, and the configuration is complete, the normal table creation can be performed. On this basis, you only need to install mysql-python-1.2.4b4.win32-py2.7.exe on the OK page, only 1 MB more. This is similar to the jar package in jdbc.
Download link: http://sourceforge.net/projects/mysql-python,
Baidu Cloud Disk: http://pan.baidu.com/s/1ddgnfprpassword :7bna
Then import MySQLdb to use it. the test code is given below:
# Coding = UTF-8 # python operation MySQL database test code import time, MySQLdb, sysprint "HelloWorld" # connection conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "yanzi", db = "mydb", charset = "utf8") cursor = conn. cursor () print "connection successful" ## add # SQL = "insert into userinfo (username, pswd) values (% s, % s)" # param = ("haha ", "ha11") # n = cursor.exe cute (SQL, param) # print n # conn. commit () # update # SQL = "update userinfo set pswd = % s where username = % s" # param = ("999999999999", "James ") # cursor.exe cute (SQL, param) # delete SQL = "delete from userinfo where username = % s" param = ("zhang san") n = cursor.exe cute (SQL, param) print nconn. commit () # Query SQL = "select * from userinfo" n = cursor.exe cute (SQL) for rows in cursor. fetchall (): for cols in rows: print cols, print "# Close pointer object and connection cursor. close () conn. close ()
The overall operation is similar to that in Jdbc. it is OK to pass an SQL statement and params. The concept of cursor is introduced in the operation. in the SQlite database, Cursor is also responsible for the operation. Note: After deletion and addition, you must execute conn. commit (). Otherwise, the operation is invalid. But this is not the case in Jdbc. When you close the database, remember to release cursor and conn.
MySQLdb online document link 1 link 2 link 3