Python experience (06)-Python processes MySQL Databases

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.