In Python, you can use the MySQLdb module to connect to the MySQL database and manipulate the MySQL database
" First step " MySQL Installation
Reference Documentation: http://blog.csdn.net/Jerry_1126/article/details/20837397
the second step connects to the MySQL
Welcome to the MySQL Monitor. Commands End With; or \g.your MySQL connection ID is 5Server version:5.5.19 mysql Community Server (GPL) mysql> #创建数据库mysql > Create data Base python; Query OK, 1 row affected (0.09 sec) #使用数据库mysql > Use python;database changed# Creating tables mysql> CREATE table people (name Varc HAR (+), age INT, Sex CHAR (1)); Query OK, 0 rows affected (0.44 sec) #插入数据mysql > insert into people values (' Tom ', ' M '); Query OK, 1 row affected (0.13 sec) mysql> insert into people values (' Jack ', null, NULL); Query OK, 1 row affected (0.06 sec) #查看数据mysql > select * FROM people;+------+------+------+| name | Age | Sex |+------+------+------+| Tom | 20 | M | | Jack | NULL | NULL |+------+------+------+2 rows in Set (0.05 sec)
Official website: Http://sourceforge.net/projects/mysql-python
Download the exe file that matches your operating system, Python version, and click Next to complete the installation. As below, it means the installation was successful!
Import MySQLdb # Imports MYSQLDB Module DB = mysqldb.connect (host = ' localhost ', # Connect to the database, the server is native user = ' root ', # Username: root passwd = ' 1234 ', # Password: 1234 db = ' python ') # data Library: Pythoncur = Db.cursor () # Gets the database cursor Cur.execute (' Insert in To people values ("Jee", +, "F") ') # Execute SQL, add record res = Cur.execute (' Delete from people where age=20 ') # Execute SQL, delete record Db.commit () # COMMIT TRANSACTION res = Cur.execute (' SELECT * from people ') # Execute SQL, get record res = Cur.fetchall () # Get all data print (RES) # Print Data cur.close () # Close cursor Db.close () # Close the database connection
For specific API definitions, please refer to:
http://blog.csdn.net/jerry_1126/article/details/40037899
Python operations on the MySQL database