Sqlite数据库
第三方库名:sqlite3
import sqlite3
#创建数据库连接对象
conn = sqlite3.connect("my_data.db")
#创建数据库操控对象
control = conn.cursor()
#查询
#返回可迭代对象
info = control.execute("select * from novel")
data = [i for i in info]
##增加
##返回操控对象,插入值需要注意,字符串要用单引号括起来
control.execute("insert into novel(id,name,author) values('{}','{}','{}')".format("1","虚之神","小書"))
conn.commit()
##删除
##返回操控对象,删除值需要注意,字符串要用单引号括起来
control.execute("delete from novel where name = '{}'".format("虚之神"))
conn.commit()
#修改
#返回操控对象,修改值需要注意,字符串要用单引号括起来
control.execute("update novel set author = '{}' where name = '{}'".format("小尘","虚之神"))
conn.commit()
#关闭数据库
conn.close()
Mysql数据库
第三方库名:pymysql
import pymysql
#创建数据库连接对象
conn = pymysql.connect(host = "localhost",user = "root",passwd = "root",database = "data",port = 3306)
#创建数据库操控对象
control = conn.cursor()
#查询
#返回查询条数,若取值则需要使用fetchall(拿来)方法取值,可迭代对象(元组)
info = control.execute("select * from novel")
data = [i for i in control.fetchall()]
#增加
#返回插入条数,插入值需要注意,字符串要用单引号括起来
info = control.execute("insert into novel(name,author,type) values('{}','{}','{}')".format("虚之神","赵小虚","玄幻"))
conn.commit()
#删除
#返回删除条数,删除值需要注意,字符串要用单引号括起来
info = control.execute("delete from novel where name = '{}'".format("虚之神"))
conn.commit()
#修改
#返回修改条数,修改值需要注意,字符串要用单引号括起来
info = control.execute("update novel set type = '{}' where id = '{}'".format("仙侠",541))
conn.commit()
#关闭数据库
conn.close()
Mongo数据库
第三方库名:pymongo
import pymongo
#创建mongo连接对象
conn = pymongo.MongoClient(host = "localhost",port = 27017)
#获取mongo连接对象里面的数据库
database = conn["test"]
#获取数据库里面的集合
collection = database["infos"]
#查询
#返回一个可迭代对象,info为查询的全部数据
result = collection.find()
info = [i for i in result]
#增加
#返回result对象,insert_one意思是插入一条数据
result = collection.insert_one({
"name": "小書","old":"88","sex":"男1"})
#修改
#返回result对象,update_many意思是更新多条数据
result = collection.update_many({
"name":"小書"},{
"$set":{
"name":"小書666"}})
#删除
#返回result对象,delete_many意思是删除条数据
result = collection.delete_many({
"name":"小書","sex":"男1"})
#关闭数据库
conn.close()
Redis数据库
第三方库名:redis
import redis
#创建数据库连接对象
conn = redis.StrictRedis(host = "localhost",db = 0,port = 6379)
#增加数据
conn.rpush("novel","a")
#关闭数据库
conn.connection_pool.disconnect()
来源:oschina
链接:https://my.oschina.net/u/4403110/blog/4919163