Install Mysql-python to connect MySQL

Source: Internet
Author: User

Transmission Door

# yum-y Install Mysql-python

Run the above command to install the RPM installation package Centos7 comes with, but the default is to install it for python2.*

Connect MySQL Mini Demo

1 #-*-encoding:utf-8-*-2 ImportOS, sys, string3 ImportMySQLdb4 5 Try:6conn = MySQLdb.connect (host="localhost", user="Root", passwd="123456", db="MyDatabase")7 exceptException, E:8     Printe;9 sys.exit ()Ten  One #Get cursor obj to operator database Acursor =conn.cursor () -sql ="CREATE table if not EXISTS test (name varchar (+) primary key, age int (3))" - cursor.execute (SQL) the  - #insert data to DB -sql ="INSERT into Test (name, age) VALUES ('%s ',%d)"% ("Coode", 25) -SQL2 ="INSERT into Test (name, age) VALUES ('%s ',%d)"% ("Ben", 19) + Try: - cursor.execute (SQL) + conn.commit (); A Cursor.execute (SQL2) at Conn.commit () - exceptException, E: -     Printe -  - #Select data from DB -sql ="SELECT * FROM Test" in cursor.execute (SQL) -result =Cursor.fetchall () to #The result is a list + ifResult: -      forRsinchResult: the         PrintRs[0], rs[1] *  $ cursor.close ()Panax NotoginsengConn.close ()

Install Mysql-python to connect MySQL

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.