• python(九)mysql操作


    需要安装pymysql:pip install pymysql

    1. 先导入mysql模块:import pymysql

    2. conn=connect.pymysql(host=地址,username=用户名,password=密码,   db=XX,post=3306,autocommit=Ture)

        cur=conn.cursor()(可以指定格式指定字典形式)

       sql=写sql语句

       cur.exec(sql)

       cur.close()

       conn.close()

    mysql的端口号是固定的

    import pymysql
    connect = pymysql.connect(host='110.20.30.4',
                              user='kkk',
                              password='123456789',
                              db='kk',
                              port=3306,
                              charset="utf8",
                              autocommit=True
                              )#建立连接
    cur = connect.cursor(pymysql.cursors.DictCursor)#建立游标
    # sql = 'select * from students;'
    # sql="insert into students values (99,'蔡明昌','男',38,'射手座','北京');"
    sql='select * from students limit 3;'#sql语句
    cur.execute(sql)#执行sql
    # connect.commit() #提交
    # connect.rollback() #回滚,sql执行失败的时候,用在事务里
    # print(cur.fetchall()) #获取sql执行的结果
    # print(cur.fetchone()) #获取sql执行的结果,只获取一条结果
    # result2 = cur.fetchmany(10) #获取sql执行的结果#获取指定的条数
    print(cur.description)
    
    # print(result)
    # print(result1)
    # print(result2)
    
    cur.close()
    connect.close()
    
    #commit操作,修改数据的情况下,必须得commit
    #事务:多条sql一起执行,如果一条失败,两条sql都算失败
  • 相关阅读:
    json对象字符串互转
    git stash压栈
    Array、ArrayList和List三者的区别
    弱类型dynamic与var
    使用git初始化项目
    git本地分支关联远程分支
    mysql索引
    js中 var functionName = function() {} 和 function functionName() {} 两种函数声明的区别
    深入理解JavaScript中的this关键字
    c#读取xml文件
  • 原文地址:https://www.cnblogs.com/dmjsd/p/11079402.html
Copyright © 2020-2023  润新知