Python connect MySQL pymysql module

Source: Internet
Author: User
Tags bulk insert

The following demo is based on the MySQLdb module in Python2

First, insert data

?
123456789101112131415 import MySQLdb conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘1234‘,db=‘mydb‘) cur = conn.cursor() reCount = cur.execute(‘insert into UserInfo(Name,Address) values(%s,%s)‘,(‘alex‘,‘usa‘))# reCount = cur.execute(‘insert into UserInfo(Name,Address) values(%(id)s, %(name)s)‘,{‘id‘:12345,‘name‘:‘wupeiqi‘}) conn.commit() cur.close()conn.close() print reCount
ImportMysqldbconn= MySQLdb.connect (host='127.0.0.1', user='Root', passwd='1234', db='MyDB') cur=conn.cursor () Li=[     ('Alex','USA'),     ('SB','USA'),]recount= Cur.executemany ('INSERT INTO UserInfo (name,address) VALUES (%s,%s)', Li) conn.commit () Cur.close () conn.close ()Printrecount BULK INSERT data
BULK INSERT Data

Ii. deletion of data

?
1234567891011121314 import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘1234‘,db=‘mydb‘)cur = conn.cursor()reCount = cur.execute(‘delete from UserInfo‘)conn.commit()cur.close()conn.close()print reCount

Third, modify the data

?
12345678910111213 import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘1234‘,db=‘mydb‘) cur = conn.cursor()reCount = cur.execute(‘update UserInfo set Name = %s‘,(‘alin‘,))conn.commit()cur.close()conn.close()print reCount

Iv. Data Search

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344 # ############################## fetchone/fetchmany(num)  ##############################import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘1234‘,db=‘mydb‘)cur = conn.cursor()reCount = cur.execute(‘select * from UserInfo‘)print cur.fetchone()print cur.fetchone()cur.scroll(-1,mode=‘relative‘)print cur.fetchone()print cur.fetchone()cur.scroll(0,mode=‘absolute‘)print cur.fetchone()print cur.fetchone()cur.close()conn.close()print reCount# ############################## fetchall  ##############################import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘1234‘,db=‘mydb‘)#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)cur = conn.cursor()reCount = cur.execute(‘select Name,Address from UserInfo‘)nRet = cur.fetchall()cur.close()conn.close()print reCountprint nRetfor i in nRet:    print i[0],i[1]

    

  

Python connect MySQL pymysql module

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.