pymysql
conn = pymysql.connect(
host= dbhost, #数据库地址
port= 3306, #数据库端口号
user= 'testpymysql', #数据库账号
password='afk!cTRnDnVq*bvZ' #数据库密码
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #字典形式
sql = 'show databases;' #sql语句
cursor.execute(sql)
### 查
#data = cursor.fetchall()
### 增删改
# conn.commit()
cursor.close()
conn.close()
pymongo
import pymongo
# 链接mongo数据库,ip(本地)+ip
conn = pymongo.MongoClient('localhost:27017')
# 链接数据库
db = conn['Scrapy']
# 链接集合
collections = db['book']
# 语法跟mongodb语法差不多
datas = collections.find() # 查询集合所有数据
for data in datas:
print(data)
conn.close()
如图:
标签:数据库,pymysql,cursor,实例,pymongo,链接,conn From: https://www.cnblogs.com/yigehulu/p/17455992.html