Python connects to MySQL database

Source: Internet
Author: User

The difference between raw_input and input in Python

    1. Raw_input The return result after getting the user input is a string type data

      Gets the user input, converts to a string type, and then returns

      (This figure is from document Python-2.7.6)

      ?

    2. Input gets the return value after the user input is a numeric type of data

      Equivalent to Eval (raw_input (cue message))

      (This figure is from document Python-2.7.6)

      ?

Connect to MySQL Database

Source :

Import MySQLdb #导入MySQLdb模块

print ' Connect database </br> '

#连接MySQL数据库 Connect the database

#Host If you fill out ' localhost ', error, please try to use ' 127.0.0.1 '

Db=mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' Michael ', db= ' test ')

#获取数据库游标 Get the database cursor

Cur=db.cursor ()

#执行SQL语句 Execute the SQL

R=cur.execute (' select * from people ')

#获取查询结果 get all the results selected

R=cur.fetchall ()

#输出查询结果

print ' Output database query result:</br> '

Print R

#关闭游标 Close the cursor

Cur.close ()

#关闭数据库连接 Close the database connection

Db.close ()

Python connects to MySQL database

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.