Python Operation Management Mysql Learning

Source: Internet
Author: User

Check whether the environment supports the MySQLdb module. If not, install the module as follows:
[Root @ bw-vm-soft ~] # Wgethttp: // response
[Root @ bw-vm-soft ~] # Tar zxvf MySQL-python-1.2.3.tar.gz
[Root @ bw-vm-soft ~] # Cd MySQL-python-1.2.3

Modify the mysql_config path in site. cfg
Vi site. cfg
Mysql_config =/usr/local/webserver/mysql/bin/mysql_config (depending on the mysql installation path)

[Root @ bw-VMS-soft MySQL-python-1.2.3] # python setup. py build
[Root @ bw-VMS-soft MySQL-python-1.2.3] # python setup. py install
After confirming that the installation is successful

Add/usr/local/webserver/mysql/lib/mysql in/etc/ld. so. conf and execute ldconfig

Then you can go to the topic to operate/manage Mysql.

Example 1: insert data
######################################## ###################################
#! /Usr/bin/env python
# _ * _ Coding: UTF-8 _*_
Import MySQLdb as mdb
Import sys

Con = mdb. connect ('localhost', 'root', '123', 'pythontest ')
With con:
Cur = con. cursor ()
Cur.exe cute ("create table if not exists \
Users (Id int primary key AUTO_INCREMENT, Name VARCHAR (25 ))")
Cur.exe cute ("insert into Users (Name) VALUES ('Richard shen ')")
Cur.exe cute ("insert into Users (Name) VALUES ('zhangsan ')")
Cur.exe cute ("insert into Users (Name) VALUES ('Li si ')")
Cur.exe cute ("insert into Users (Name) VALUES ('wang dongdong ')")
######################################## ###################################

Example 2: View data

#! /Usr/bin/env python
# _ * _ Coding: UTF-8 _*_
Import MySQLdb as mdb
Import sys

Con = mdb. connect ('localhost', 'root', '123', 'pythontest ')

With con:
Cur = con. cursor ()
Cur.exe cute ("SELECT * FROM Users ")

Numrows = int (cur. rowcount)
For I in range (numrows ):
Row = cur. fetchone ()
Print row [0], row [1]

Execution result:
1 Richard shen
2 Zhang san
3 Li si
4 Wang dongdong

This is the most basic operation of the database.

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.