conn = pymysql.connect(host="127.0.0.1",#默认是本机 port=3306, #默认3306 user="root",#必填 password='密码',#必填 db="库名")#必填 #若是没有库会报pymysql.err.InternalError: (1049, "Unknown database '库名'") 全部咱们编辑能够这样 try: conn = pymysql.connect(host="127.0.0.1",#默认是本机 port=3306, #默认3306 user="root",#必填 password='16745',#必填 db="asds",)#必填 except pymysql.err.InternalError: print('没有库')
cursor = conn.cursor(pymysql.cursors.DictCursor) #自定义游标类型为字典 cursor = conn.cursor()#默认是元祖
cursor.max_stmt_length#1024000 cursor.rownumber#5 #当前结果集中游标所在行的索引(起始行号为 0) cursor.arraysize#1 #此读/写属性指定用.fetchmany()一次获取的行数。 # 默认1表示一次获取一行;也能够用于执行.executemany() cursor.lastrowid#None #只读属性提供上次修改行的rowid # DB仅在执行单个INSERT 操做时返回rowid 。 # 如未设rowid或DB不支持rowid应将此属性设置为None # 如最后执行语句修改了多行,例如用INSERT和.executemany()时lastrowid语义是未定义 cursor.rowcount #5 #最近一次 execute() 建立或影响的行数 # 如无cursor.execute()或接口没法肯定最后一个操做的rowcount则该属性为-1 # 该行数属性能够在动态更新其值的方式来编码。 # 这对于仅在第一次调用.fetch()方法后返回可用rowcount值的 数据库很是有用。
普通提交
python
count = cursor.execute('show tables') #返回值为受到影响的数据条数
防注入提交
mysql
table_name = input('table name :') count = cursor.execute('select table %s',(name,))
默认显示以前那一次显示的内容,只显示查看相关语法的内容,为返回值 cursor.fetchall() #查看所有 cursor.fetchone() #查看当前游标位置的一个值 cursor.fetchmay(N) #查看当前游标位置的n值
相对位置sql
cursor.scroll(1, "relative")数据库
cursor.scroll() #默认是相对位置fetch
绝对位置编码
cursor.scroll(0, "absolute")code
若是只选的sql语言会被表的数据形成修改贼必须索引
count = cursor.execute('修改的sql语句')
conn.commit()
cursor.close()接口