zoukankan      html  css  js  c++  java
  • 操作数据库mysql

     1 一、连接数据库
     2 import pymysql
     3 
     4 #写博客的时候要给这些信息打马赛克
     5 #ip    xxxxxxx
     6 #port   xxx
     7 #user   xxx、
     8 #password  xxx
     9 #db       xxx
    10 
    11 conn = pymysql.connect(host='xxxxxx',port=3xxx6,user='xxxx',password='1xxxxx',db='xxxx',charset='utf8')
    12 cur = conn.cursor() #建立游标  相当于仓库管理员
    13 sql = 'select * from app_myuser limit 5;'
    14 cur.execute(sql) #执行sql语句   不会返回结果 只会执行
    15 
    16 result = cur.fetchall()  #用它返回结果
    17 print(result)
    18 
    19 cur.close()  #关闭游标
    20 conn.close() #关闭连接
    21 ============================================================================
    22 
    23 #添加一条数据  要commit
    24 conn = pymysql.connect(host='xxxxx',port=xxx,user='xxx',password='xxxx',db='xxx',charset='utf8')
    25 cur = conn.cursor() #建立游标  相当于仓库管理员
    26 #cur = conn.cursor(pymysql.cursors.DictCursor) #字典的形式
    27 sql = "select * from app_myuser where username = '白';"
    28 sql2 = "insert into app_myuser (username,passwd,is_admin) values ('白','124','819');"
    29 cur.execute(sql) #执行sql语句   不会返回结果 只会执行
    30 conn.commit()#提交
    31 result = cur.fetchall()  #是对于有返回结果的需要这个语句
    32 print(result)
    33 
    34 cur.close()  #必须关闭
    35 conn.close() #必须关闭
    36 
    37 ========================================================================
    38 
    39 conn = pymysql.connect(
    40                        host='1xxxxxx',
    41                        port=xxx,
    42                        user='xxxz',
    43                        password='1xxxx',
    44                        db='xxxx',
    45                        charset='utf8',
    46                        autocommit = True  #自动提交
    47                        )
    48 ps:写这个就不需要在手动提交
    49 ===========================================================================
  • 相关阅读:
    五种方式获取文件扩展名-转载未验证
    http状态代码-转载
    求两个时间的日期差-部分
    jsp相对路径解决方案
    OAUI前台设计(二)
    jsp生命周期
    OA办公页面设计
    Redis全局命令及数据结构
    Redis集群简介及部署
    sql中 in到底走不走索引
  • 原文地址:https://www.cnblogs.com/baiby/p/10833763.html
Copyright © 2011-2022 走看看