码迷,mamicode.com
首页 > 数据库 > 详细

python学习笔记-数据库

时间:2018-05-11 23:59:04      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:个数   增删改查   pass   .exe   fetchall   password   connect   else   student   

一、mysql数据库:

python操作mysql数据库 python3中操作mysql数据需要安装一个第三方模块,pymysql,使用pip install pymysql安装即可,在python2中是MySQLdb模块,在python3中没有MySQLdb模块了,所以使用pymysql。

连接数据库的步骤:

  1.链接数据库:账号、密码、ip、端口号、数据库;

  2.建立游标;

  3.执行sql(执行insert、delete、update语句时,必须得commit)

  4.获取结果;

  5.关闭游标;

  6.连接关闭;

import pymysql

coon = pymysql.connect(host=118.24.3.40,user=jxz,passwd=123456,port=3306,db=jxz,charset=utf8)
    #port必须写int类型,charset这里必须写utf8

cur = coon.cursor() #建立游标
cur.execute(select * from stu;)#执行sql语句
cur.execute(insert into stu (id,name,sex) VALUE (1,"牛寒阳","女");)
res = cur.fetchall()  #获取所有返回的结果
print(res)
cur.close()#关闭游标
coon.close()#关闭连接

获取元祖操作:

 import pymysql
    
    conn = pymysql.connect(host=127.0.0.1, port=3306, user=root, passwd=123456, db=data,charset=utf8)# 创建连接,指定数据库的ip地址,账号、密码、端口号、要操作的数据库、字符集
    cursor = conn.cursor()  #建立游标
    effect_row = cursor.execute("update students set name = ‘niuhy‘ where id = 1;")# 执行SQL,并返回受影响行数
    effect_row = cursor.execute("update students set name = ‘niuhy‘ where id = %s;", (1,))# 执行SQL,并返回受影响行数
    effect_row = cursor.executemany("insert into students (name,age) values (%s,%s); ", [("andashu",18),("12345",20)])
    cursor.execute("select * from students;")#执行select语句
    row_1 = cursor.fetchone()#获取查询结果的第一条数据,返回的是一个元组
    row_2 = cursor.fetchmany(3)# 获取前n行数据
    row_3 = cursor.fetchall()# 获取所有数据
    conn.commit()# 提交,不然无法保存新建或者修改的数据
    new_id = cursor.lastrowid    # 获取最新自增ID
    print(new_id)
    cursor.close() # 关闭游标
    conn.close()# 关闭连接

获取字典操作:

import pymysql
   
    conn = pymysql.connect(host=127.0.0.1, port=3306, user=root, passwd=123456, db=data,charset=utf8) # 创建连接,指定数据库的ip地址,账号、密码、端口号、要操作的数据库、字符集
    cursor = conn.cursor() # 创建游标
    cursor = coon.cursor(cursor=pymysql.cursors.DictCursor)#需要指定游标的类型,字典类型
    cursor.execute("select * from user;")# 执行SQL
    #获取返回结果,这个时候返回结果是一个字典
    res = cursor.fetchone()#返回一条数据
    res2 = cursor.fetchall()#所有的数据一起返回

将操作数据库写成一个函数:

def my_db(host,user,passwd,db,sql,port=3306,charset=utf8):
    import  pymysql
    coon = pymysql.connect(user=user,host=host,port=port,passwd=passwd,db=db,charset=charset)
    cur = coon.cursor()#建立游标
    cur.execute(sql)#执行sql
    if sql.strip()[:6].upper()==SELECT:
        res = cur.fetchall()
        print(res)
    else:
        coon.commit()
         res = ok
    cur.close()
    return res

 

二、redis数据库:

操作redis redis是一个nosql类型的数据库,数据都存在内存中,有很快的读写速度,python操作redis使用redis模块,pip安装即可

import pymysql,json,redis
r = redis.Redis(host=118.24.3.40,password=HK139bc&*,db=1,port=6379)#指定链接redis的端口和ip以及哪个数据库
# 增删改查
r.set(hh,hello!)#数据库里面新增一个值
#修改也是set
r.delete(天蝎座:hh)#删除指定key
r.setex(hh,hello,10)#设置key的失效时间,最后这个参数是秒,10秒钟后session失效,删除掉
hh= r.get(hh)
print(hh)
hh.decode()#把二进制转成字符串
print(r.keys())#获取到所有的key
print(r.keys(h*))#获取到开头的key
print(r.keys(*h*))#获取包含t的key
print(r.get(呵呵))#获获取不存在的key时返回None
r.set(天蝎座:hh,呵呵)#key中有冒号,则冒号前面的是文件夹的名字
print(r.get(天蝎座:hh))

 

python学习笔记-数据库

标签:个数   增删改查   pass   .exe   fetchall   password   connect   else   student   

原文地址:https://www.cnblogs.com/huangr/p/9026190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!