zoukankan      html  css  js  c++  java
  • python连接数据库

    import pymysql;
    #1.连接数据库
    conn = pymysql.connect(
        host='localhost',
        user='root',
        password='***',#密码
        db='jiang', #数据库名
        charset='utf8',
    )
    #2.创建游标对象
    cur = conn.cursor()
    #3.对数据库进行CRUD操作
    try:
        create_sqli = "create table hello (id int, name varchar(30));"
        cur.execute(create_sqli)
    except Exception as e:
        print("创建数据表失败:", e)
    else:
        print("创建数据表成功;")
    #
    try:
        insert_sqli = "insert into hello values(1, '杨将');"
        cur.execute(insert_sqli)
    except Exception as e:
        print("插入数据失败:", e)
    else:
        conn.commit()
        print("插入数据成功;")
    # 4. 关闭游标
    cur.close()
    # 5. 关闭连接
    conn.close()
    

      pyton连接数据库需要先安装pymysql模块:pip install pymysql
    安装完成后导入pymysql模块:import pymysql

  • 相关阅读:
    C++基础学习1-编译与链接
    html学习
    使用BP拦截POST请求包
    2019.9.17
    搭建LAMP环境
    2019.9.16
    2019.9.12
    2019.9.11
    手脱无名壳tslgame_rl
    一款自制壳的脱壳
  • 原文地址:https://www.cnblogs.com/yongyuandishen/p/14155077.html
Copyright © 2011-2022 走看看