python之pymysql

import pymysqlfrom pymysql.cursors import DictCursor# coon = pymysql.connect(host='211.149.218.16',port=3306,user='jxz',passwd='123456',db='jxz',charset='utf8')# cur = coon.cursor(DictCursor)#創建遊標,倉庫管理員,指定遊標類型,返回字典# sql='select * from stu limit 2,9;'# # insert_sql = 'insert into stu VALUE (89,"nhy");'# # cur.execute(insert_sql)# cur.execute(sql)#執行sql語句# for c in cur:#直接循環遊標,每次循環的時候就是每一列的數據#     print(c)# # res = cur.fetchall()#獲取sql語句執行的結果,一次性所有獲取# # coon.commit()#提交# # print(res[0][1])# cur.close()# coon.close()def op_mysql(host,user,passwd,db,sql,charset='utf8',port=3306):    import pymysql    from pymysql.cursors import DictCursor    coon = pymysql.connect(host=host,user=user,passwd=passwd,db=db,charset=charset,port=port)    cur = coon.cursor(DictCursor)#指訂返回數據的類型是字典    cur.execute(sql)    if sql.strip().startswith('select'):        res = cur.fetchall()    else:        coon.commit()        res = 'ok'    cur.close()    coon.close()    print(res)    return res
相關文章
相關標籤/搜索