python之mysql
一、开发文档
https://mysqlclient.readthedocs.io
二、安装
pip install mysqlclient==1.3.12
三、获取记录
fetchone fetchmany fetchall
四、简单实例
import MySQLdb import pprint #创建一个connection对象,代表了一个数据库连接 connection = MySQLdb.connect( host="192.168.2.244", user="root", passwd="123456", db="crodigyrtu", charset="utf8") #返回一个cursor对象 c = connection.cursor() #调用cursor对象的exeute方法查表 c.execute("SELECT * FROM room_device") rows = c.fetchmany(c.rowcount) #c.rowcount返回获取的行数 pprint.pprint(rows) #调用cursor对象的exeute方法增加数据 c.execute("INSERT INTO room_device ···········") #一定要执行commit才能插入成功 connection.commit() #调用cursor对象的exeute方法删除数据 c.execute("DELETE FROM room_device ···········") #一定要执行commit才能插入成功 connection.commit()