zoukankan      html  css  js  c++  java
  • 十四、数据库编程

    一、操作SQLite3数据库 

        从Python3.x版本开始,在标准库中已经内置了SQLlite3模块,它可以支持SQLite3数据库的访问和相关的数据库操作。在需要操作SQLite3数据库数据时,只须在程序中导入SQLite3模块即可。

    1)例子

    #操作sqlite3创建表
    #1.导入模块
    #2.创建连接对象
    #3.创建游标,用于执行语句
    #4.创建表语句,使用'''''',表示文本对象
    #5.执行sql语句
    #6.关闭连接close
    import sqlite3
    conn = sqlite3.connect("d:/py_sqlite3/sql01.sql")
    cur =conn.cursor()
    sql = '''create table t_person(
    pid integer primary key autoincrement,
    pname varchar not null,
    age INTEGER
    )'''
    try:
    cur.execute(sql)
    print("执行成功")
    except Exception as e:
    print(e)
    print("创建失败")
    finally:
    cur.close()
    conn.close()
    2)例子
    '''
    表中插入一条,多条数据
    executeexecutemany
    修改/删除同代码。使用execute即可
    '''
    #导入模块
    import sqlite3
    # 建立连接对象
    conn = sqlite3.connect("d:/py_sqlite3/sql01.sql")
    #创建游标对象,用于执行sql
    cur = conn.cursor()
    #插入sql
    sql = 'insert into t_person(pname,age) values(?,?)'
    try:
    # 执行插入
    # cur.execute(sql,('张三',24))
    # cur.executemany(sql,[('李四',24),('王五',22),('赵六',27)])
    print("操作成功")
    conn.commit()
    except Exception as e:
    print(e)
    print("操作失败")
    conn.rollback()
    finally:
    cur.close()
    conn.close()
    3)例子
    '''
    查询一条或者多条数据
    fetchall/fetchone
    '''
    #导入模块
    import sqlite3
    #建立连接
    conn = sqlite3.connect("d:/py_sqlite3/sql01.sql")
    #建立游标,用于执行sql
    cur = conn.cursor()
    #建立sql
    sql = 'select *from t_person'
    #执行语句
    try:
    cur.execute(sql)
    #1)返回结果集fetchall
    #person_all = cur.fetchall()
    #遍历结果集
    #for person in person_all:
    # print(person)
    #2)返回一个结果fetchone
    person = cur.fetchone()
    print(person)
    except Exception as e:
    print(e)
    print('执行失败')
    finally:
    cur.close()
    conn.close()
    二、操作Mysql数据库

    下载地址:https://dev.mysql.com/downloads
    mysql分为安装版和解压版,安装版是新手、开发人员使用,有系统配置文件,结尾是msi格式,这里使用安装版。
    安装此界面时,选择自定义:
    
    

          安装完毕:

            

             PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。

             在cmd下,输入命令 pip  install PyMySQL进行安装。

                

     1)例子:

      

    '''
    pyMysql创建表
    '''
    #导入模块:pyMysql
    import pymysql
    #创建连接mysql数据库
    conn = pymysql.connect(host='localhost', user='root',password='123456',database='python_db',port=3306)
    #创建游标对象,用于执行sql
    cur = conn.cursor()
    #建立sql
    sql ='''create table p_student(
    pid int primary key auto_increment,
    pname varchar(30) not null ,
    age int(2),
    score float(3,1)
    )
    '''
    try:
    #执行sql
    cur.execute(sql)
    print("执行成功")
    except Exception as e:
    print(e)
    print("执行失败")
    finally:
    cur.close()
    conn.close()

     2)例子

    '''
    插入mysql一条或者多条数据execute/executmany
    '''
    import pymysql
    conn = pymysql.connect(host='localhost',database='python_db',port=3306,password='123456',user='root')
    cur = conn.cursor()
    sql = 'insert into p_student(pname,age,score) values(%s,%s,%s)'
    try:
    # cur.execute(sql,('张三',18,99.3))
    cur.executemany(sql,[('王五',22,89.4),('赵六',22,99.8),('王麻子',34,88.8)])
    print("添加成功")
    conn.commit()
    except Exception as e:
    print("添加失败")
    conn.rollback()
    finally:
    cur.close()
    conn.close()

    3)例子

    '''
    #查询一条或者多条语句fetchall/fetchone
    '''
    import pymysql
    conn = pymysql.connect(host ='localhost',database='python_db',user ='root',password='123456',port=3306)
    cur =conn.cursor()
    sql =' select *from p_student'
    try:
    cur.execute(sql)
    student = cur.fetchone()
    # students=cur.fetchall()
    # for student in students:
    # pno = student[0]
    # pname = student[1]
    # age = student[2]
    # score = student[3]
    # print('pno:',pno,'pname:',pname,'age:',age,'score:',score)
    print("查询成功")
    print(student)
    except Exception as e:
    print(e)
    print(查询失败)
    finally:
    cur.close()
    conn.close()





  • 相关阅读:
    VUE入门
    搭建内网穿透服务
    nacos集群配置安装
    jenkins入门
    Linux系统管理与自动化运维
    SVN
    JAR包启动
    服务器rm -rf 恢复案例 好文章
    docker入门到放弃
    CentOS7安装图形界面及报错处理
  • 原文地址:https://www.cnblogs.com/dangjingwei/p/12318476.html
Copyright © 2011-2022 走看看