Python processes MySQL Databases
1. Check the user environment: mysql-server has been installed and Python-mysqldb has been installed.
Aptitude show mysql-server; aptitude show Python-mysqldb
2. Create a database, create a table, add data, and change the password of the MySQL database to oracle11g.
Use MySQL;
Update User Set Password = Password ( ' Oracle11g ' ) Where User = ' Root ' ;
Flush Privileges ;
-- -------------------
Create Database Myfamily;
Use Myfamily;
Create Table People (ID Int , Name Varchar ( 30 ), Age Int , Sex Char ( 1 ));
Insert Into People Values ( 0 , ' Zihao Xu ' , 5 , ' M ' );
Select * From People; Exit ;
3. Compile pymysql. py for testing Program Phoenix @ Debian: ~ $ Cat pymysql. py
# ! /Usr/bin/Python
# Filename: pymysql. py
# Coding: UTF-8
Import Mysqldb
Conn = Mysqldb. Connect (host = ' 127.0.0.1 ' , DB = ' Myfamily ' ,
User = ' Root ' , Passwd = ' Oracle11g ' )
Cur = Conn. cursor ()
r = cur.exe cute ( ' insert into people values (6, \ ' Bill Gates \ ' , 58, \ ' m \ ' ) ' )
cur.exe cute ( ' Delete from people where age = 58 ' )
Conn. commit ()
R=Cur.exe cute ('Select * from people')
R=Cur. fetchall ()
PrintR