文档服务地址:http://47.92.0.57:3000/ 周报索引地址:http://47.92.0.57:3000/s/NruNXRYmV

Commit f4ede532 by 李景熙

修改上传文件的相关代码,将数据库配置统一移到model文件中

parent 29108bba
from peewee import * from peewee import *
from pymongo import MongoClient
import datetime
mysql = MySQLDatabase( mysql = MySQLDatabase(
'test', 'test',
...@@ -9,50 +7,4 @@ mysql = MySQLDatabase( ...@@ -9,50 +7,4 @@ mysql = MySQLDatabase(
passwd='root', passwd='root',
charset='utf8' charset='utf8'
) )
# 连接mysql数据库 mysql.connect()
# mysql.connect();
# 连接mongodb数据库
client = MongoClient('mongodb://root:mongodb@localhost:27017')
class MysqlBaseModel(Model):
# 内部类
class Meta:
database = mysql; # 每一个继承BaseModel类的子类都是连接db表
class User(MysqlBaseModel):
username = CharField(unique=True); # 每个属性都是一个表字段
class Tweet(MysqlBaseModel):
user = ForeignKeyField(User, related_name='tests');
message = TextField();
created_date = DateTimeField(default=datetime.datetime.now);
is_published = BooleanField(default=True);
if __name__ == '__main__':
# mysql操作示例
# User.create_table(); 创建User表
# Tweet.create_table(); 创建Tweet表
# 添加
# user = User.create(username="tom")
# Tweet.create(user=user, message="this is test msg")
# # 查询
# t = Tweet.get(message="this is test msg")
# print(t.user_id)
# print(t.created_date)
# print(t.is_published)
# mongodb操作示例
mongodb = client['test']
collection = mongodb['testcollection'].find()
for t in collection:
print(t)
col = mongodb['test1']
image = {'onepic': '1.jpg', 'twopic': '2.jpg', 'threepic': '3.jpg'}
aim = {'name': 'test_1', 'document': 'this is test msg!', 'image': image}
col.insertone(aim)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment