• #MySQL for Python(MySQLdb) Note


    #MySQL for Python(MySQLdb) Note
    
    #切记不要在python中创建表,只做增删改查即可。
    #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -->(3)选择数据库 -->(4)执行语句 -->(5)关闭连接
    
    #(0)引用库
    import MySQLdb
    
    
    #(1)创建连接
    con = MySQLdb.connect(user = "root", passwd = "123456",host = "127.0.0.1")
    
    
    #(2)创建游标
    cur = con.cursor()
    
    
    #(3)选择数据库
    con.select_db("ivms8100v3")
    
    
    #(4)执行语句
    
    #(4.1)单行插入
    sqli = "insert into users(strName, strCode, nLevel) value (%s, %s, %d)"
    cur.execute(sqli, ("admin", "admin", 3))
    
    #(4.2)多行插入
    sqlim = "insert into users(strName, strCode, nLevel) values (%s, %s, %d)"
    cur.executemany(sqlim, [("xjh", "admin", 3),("xjh2", "admin", 3)])
    
    #(4.3.1)单行查询
    cur.execute("select * from users")  #调用fetchone()、scroll()、fetchmany()前,先查询
    cur.fetchone()                      #默认从第一条记录开始查看,指针指向下一条记录。
    
    #(4.3.2)指定查询
    cur.scroll(0, "absolute")   #查训记录中索引为1的绝对位置
    
    #(4.3.3)多行查询
    cur.fetchmany(15)           #指定查看15条,指针指向下一条记录。
    
    #(4.3.4)多行查询一步完成
    cur.fetchmany(cur.execute("select * from users")) 
    
    
    #(5)关闭连接
    cur.close()     #先关闭游标
    con.close()     #再关闭连接

     

  • 相关阅读:
    pytest框架
    Zabbix第九章(zabbix自定义item)
    Zabbix第七章(Zabbix监控本地)
    Zabbix第五章(配置zabbix的语言)
    Zabbix第四章(zabbix-server安装)
    Zabbix第二章(下载)
    Zabbix第一章(简介)
    线性筛
    Eratosthenes筛法
    质数判定
  • 原文地址:https://www.cnblogs.com/xuejianhui/p/3732076.html
Copyright © 2020-2023  润新知