1. Insert Operation :
1#!/uer/bin/env python2#-*-coding:utf-8-*-3 4 Import MySQLdb5 6conn = MySQLdb.connect (host='192.168.1.100', user='Root', passwd='123456', db='Oldb')7 8Cur =conn.cursor ()9 Tenrecount = Cur.execute ('INSERT into students (Name,sex,age,tel) VALUES (%s,%s,%s,%s)',('Alex2','Mans', +,'18923143')) One# recount = Cur.execute ('SELECT * from students;') A - Conn.commit () - cur.close () the conn.close () -Print (recount)
Output information:
3
2. Query operation
1#!/uer/bin/env python2#-*-coding:utf-8-*-3 4 Import MySQLdb5 6conn = MySQLdb.connect (host='192.168.1.100', user='Root', passwd='123456', db='Oldb')7 8Cur =conn.cursor ()9 Ten# recount = Cur.execute ('INSERT into students (Name,sex,age,tel) VALUES (%s,%s,%s,%s)',('alex4','Mans', in,'18923143')) Onerecount = Cur.execute ('SELECT * from students;') A - - print (Cur.fetchone ())
---------------------------------- the print (Cur.fetchall ()) #取出所有数据
----------------------------------
Print (Cur.fetchmany (3)) #指定3 data
Output information:
(1L, ' Alex ', ' man ', 18, ' 151515151 ')
------------------------------------
(1L, ' Alex ', ' man ', 18, ' 151515151 ')
(2L, ' alex2 ', ' man ', 19, ' 18923143 ')
(3L, ' alex2 ', ' man ', 19, ' 18923143 '))
-------------------------------------
()
3. BULK INSERT Data
1#!/uer/bin/env python2#-*-coding:utf-8-*-3 4 Import MySQLdb5 6conn = MySQLdb.connect (host='192.168.1.100', user='Root', passwd='123456', db='Oldb')7 8Cur =conn.cursor ()9 TenLi = [ One('Tommite','Mans', in,'189231432'), A('Kendi','Mans', -,'189231433'), -('Lili','Mans', +,'189231434'), - ] the -recount = Cur.executemany ('INSERT into students (Name,sex,age,tel) VALUES (%s,%s,%s,%s)', Li) - - Conn.commit () + cur.close () - conn.close () +Print (recount)
Output information:
3
9th Day python operation MySQL Database