Python Connection Database

Source: Internet
Author: User

MySQL module temporarily does not support python3.0 above, because I download Python is version 3.0, so you want to connect to the database can only use other methods.

python3.x connection MySQL program has: Oursql, Pymysql, MYCONNPY, etc., here is mainly installed Pymysql

1. Installation

Pymysql Installation: Find the location of the Python folder pip program to open the command window:

Pip Install Pymysql3

2. Use

After the installation is complete, the following steps are followed for the database connection

Introduce API modules.
Gets the connection to the database.
Executes SQL statements and stored procedures.
Close the database connection.

12345678910111213141516171819202122232425262728293031 import pymysql#查询#连接数据库conn = pymysql.connect(host=‘数据库服务器名‘,user=‘用户名‘,passwd=‘密码‘,db=‘数据库名‘,pot=‘数据路端口号‘,charset=‘utf8‘)#获取游标cur = conn.cursor()cur.execute(‘数据库查询语句‘)#获取数据,fetchone获取一条数据,fetchall获取全部数据data = cur.fetchall()for d in data: print(d)#关闭游标cur.close()#关闭数据库conn.close()#插入、删除,修改操作#连接数据库conn = pymysql.connect(host=‘数据库服务器名‘,user=‘用户名‘,passwd=‘密码‘,db=‘数据库名‘,pot=‘数据路端口号‘,charset=‘utf8‘)#获取游标cur = conn.cursor()cur.execute(‘数据库插入语句‘)#提交当前事务到数据库conn.commit()#rowcount:返回数据条数或影响行数print(‘插入:‘,cur.rowcount,‘条数据‘)#关闭游标cur.close()#关闭数据库conn.close()

The operation of the database can be added to the try...except statement catch error, when an error occurs, you can rollback the database operation, back to the modification:

12345678910111213 try: # 执行sql语句 cur.execute(sql) # 提交到数据库执行 cur.commit()except: # 发生错误时回滚 cur.rollback()# 关闭游标cur.close()#关闭数据库conn.close()

Attach the code for the successful connection:

Import Pymysql
conn = Pymysql.connect (host= ' 127.0.0.1 ', port=33060,user= ' zyy ', passwd= ' 123456 ')
Print ("Connection succeeded")
# Create a Cursor object using the cursor () method cursor
cursor = Conn.cursor ()

# Execute SQL Query Using Execute () method here Query database version information
Cursor.execute ("Select VERSION ()")

# Use the Fetchone () method to get a single piece of data.
data = Cursor.fetchone ()

Print ("Database version:%s"% data)

Python Connection 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.