ü\Óü¿Æ¼¼Íø

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á
mito
²é¿´: 3426|»Ø¸´: 4

PythonʵսFlaskÏÎ½Ó Mysql+MongoDB+RedisÈý´óÊý¾Ý¿âʵսÊÓÆµ½Ì³Ì

[¸´ÖÆÁ´½Ó]

8Íò

Ö÷Ìâ

608

»ØÌû

27Íò

»ý·Ö

¹ÜÀíÔ±

Rank: 9Rank: 9Rank: 9

»ý·Ö
275170
QQ
·¢±íÓÚ 2018-4-29 08:10:42 | ÏÔʾȫ²¿Â¥²ã |ÔĶÁģʽ
¿Î³ÌÃû³Æ:  PythonʵսFlaskÏÎ½Ó Mysql+MongoDB+RedisÈý´óÊý¾Ý¿â ½áºÏORMODMÍøÒ×ÐÂÎÅÒÆ¶¯¶Ë+ºǫ́ʵսÊÓÆµ½Ì³Ì


¿Î³Ì¼ò½é:    

±¾Ì׽̳ÌÊʺÏÓÚÎÞPython±à³Ì¾­ÑéµÄ»áÔ±ÃÇ£¬¼òµ¥Ò×ѧ£¬²»ÐèÒªÄãÓÐÈκαà³Ì֪ʶ£¬´Ó×î¼òµ¥µÄ½Ì³ÌÈëÊÖ£¬¼òµ¥Ã÷ÁË¡£¿Î³Ì½²µÄºÜÏêϸ£¬¼´Ê¹¼´Ê¹Êý¾Ý¿âµÄÈëÃÅÕßѧÍêÕâÃſγÌÒ²»á¶ÔMysql+MongoDB+RedisÕâÈý´óÖ÷Á÷Êý¾Ý¿âÓÐËù¼û½â¡£Ò²¶®µÄÔõÑùÓÃPythonÈ¥²Ù×÷ÕâЩÊý¾Ý¿â£¬ÄÚÈݺÜʹÓôÓʵսÏîÄ¿¿ªÊ¼¡£»ùÓÚflaskÕâ¸ö¿ò¼Ü´î½¨¡£Ò»¸ö¼òµ¥µÄʵÀý£¬Èç¹ûÔÚ¹¤×÷ÖлáʹÓÃPython±¸·ÝSQLServerÊý¾Ý¿âÈ»ºó´ò°ü´«µ½Öö¥µÄλÖò¢É¾³ý¡£ÕâЩÄú¶¼¿ÉÒÔÔÚÕâÌ׽̳ÌÖÐѧ»á¡£ËäÈ»ÕâÊÇÒ»ÃÅPythonÓëÊý¾Ý¿âµÄ¿Î³Ìµ«Í¬Ê±Ò²ÄÜÊ¡ÏÂÄãѧϰFlaskµÄʱ¼ä¡£

----------------------¿Î³ÌĿ¼------------------------------
1¡¢Êý¾Ý¿â¼ò½é
½²½âÊý¾Ý¿â»ù±¾ÖªÊ¶£¬¹ØÏµÐÍÊý¾Ý¿âºÍ·Ç¹ØÏµÐÍÊý¾Ý¿âµÄ¶Ô±È£¬·Ç¹ØÏµÐÍÊý¾Ý¿âµÄ·ÖÀàÒÔ¼°ÆäÓ¦Óó¡¾°£¬×îºó½éÉÜMySQLÊý¾Ý¿â¡£
2¡¢mysql»ù´¡
½éÉÜMySQLµÄ°²×°¼°ÅäÖã¬Ê¹ÓÃNavicat ºÍ phpMyAdmin ½øÐÐͼÐλ¯¹ÜÀíÊý¾Ý¿â¡£È»ºó½²½âSQL»ù´¡£¬Öصã°üÀ¨½¨±í¡¢½¨¿âÓï¾ä£¬ÐÂÔö¡¢Ð޸ġ¢É¾³ý¡¢²éѯÓï¾äµÈÖØµã֪ʶ¡£ÎªÁ˹®¹Ì»ù´¡£¬ÔÚ´Ë´¦°²ÅÅÁËÒ»¸ö¡°Éè¼ÆÐÂÎÅ±í¡±µÄʵս¡£
3¡¢python API
Ê×ÏÈ£¬½Ì´ó¼ÒʹÓÃpip°²×°mysql-clientÒÀÀµ°ü£¬²¢Ê¹ÓÃpythonÀ´Á´½Ó/¶Ï¿ªÁ´½ÓÊý¾Ý¿â¡£½ÓÏÂÀ´½²½âʹÓÃfetchone\ fetchallÀ´²éѯһÌõ»òÕßÊǶàÌõÊý¾Ý¡¢·­Ò³¡¢ÐÂÔöÒ»Ìõ/¶àÌõÊý¾Ýµ½Êý¾Ý¿âÒÔ¼°ÊÂÎñ¿ØÖƵȡ£
4¡¢ORM
Ê×ÏȽéÉÜORMÒÔ¼°ÎÒÃÇҪʹÓõÄSQLAlchemyÏà¹ØÖªÊ¶¡£ÔÚ°²×°ºÃORMÏà¹Ø°üÖ®ºó£¬½²½âORMµÄÄ£ÐÍ¡¢³£¼ûÀàÐÍ£¬²¢Ê¹ÓÃORMн¨ÐÂÎűíµÄÄ£ÐÍ¡£½ÓÏÂÀ´Ê¹ÓøÃÄ£ÐͽøÐÐÐÂÎŵÄÐÂÔö¡¢Ð޸ĺÍɾ³ýµÈ²Ù×÷¡£
5¡¢ÍøÒ×ÐÂÎÅʵս
Ê×ÏÈչʾʵսÏîĿҪʵÏÖµÄÄÚÈÝ£¬ÍøÒ×ÐÂÎÅÊÖ»úÍøÒ³°æÊ×Ò³+ºǫ́¹ÜÀíÒ³Ãæ¡£È»ºó½éÉÜFlask web¿ò¼Ü£¬²¢½Ì´ó¼Ò´î½¨³öÒ»¸ö¼òµ¥µÄÍøÕ¾¡£ÔÚ´Ë»ù´¡ÉÏʵÏÖÐÂÎŵÄǰ̨£¬°üÀ¨Ê×Ò³¡¢°´Àà±ð²é¿´ÐÂÎÅ¡¢²é¿´ÐÂÎÅÏêÇ飻½ÓÏÂÀ´ÊµÏÖÐÂÎŵĺǫ́¹ÜÀí£¬°üÀ¨ÐÂÎŵÄÐÂÔö¡¢Ð޸ġ¢Ò첽ɾ³ý¡¢²éѯ·­Ò³µÈ¹¦ÄÜ¡£
6¡¢MongoDB Êý¾Ý¿â»ù´¡
Ê×ÏȽéÉÜMongoDB£¬½Ì´ó¼ÒÈçºÎ°²×°ºÍÆô¶¯MongoDB¡£È»ºó½éÉÜMongoDBÖеÄÎĵµ¡¢¼¯ºÏºÍÊý¾Ý¡£½ÓÏÂÀ´Ê¹ÓÃMongo Shell À´ÊµÕ½MongoÃüÁCRUD£©£¬°üÀ¨ÈçºÎ´´½¨¼Ç¼¡¢ÐÞ¸ÄÊý¾Ý¡¢²éѯºÍɾ³ýµÈ²Ù×÷¡£ÕÆÎÕÕⲿ·ÖÖ®ºó£¬°²ÅÅÁËÒ»¸öСµÄÁ·Ï°£¬¼´£ºÊ¹ÓÃMongoDBÉè¼ÆÒ»¸öѧÉúÐÅÏ¢±í£¬²¢°´ÕÕ×÷ÒµÒªÇóÖ´ÐÐһЩCRUD²Ù×÷¡£×îºóΪÁË·½±ã²Ù×÷
7¡¢Python²Ù×÷MongoDB
Ê×ÏȽéÉÜpymongoµÄ°²×°£¬½²½âÈçºÎʹÓÃpymongoÀ´Á¬½ÓÊý¾Ý¿â£¬×îºó½²½âʹÓÃfind(),find_one()µÈ·½·¨²éѯÊý¾Ý£¬ÒÔ¼°Ê¹ÓÃupdate()ÐÞ¸ÄÊý¾Ý£¬Ê¹ÓÃremove()ɾ³ýÊý¾ÝµÈ¡£
8¡¢MongoDB ODM£¨1£©
Ê×ÏȽéÉÜMongoDB ODMµÄ°²×°£¬È»ºóʹÓÃODMÀ´Á¬½ÓMongoDBÊý¾Ý¿â£¬½ÓÏÂÀ´½éÉÜODMµÄÄ£ÐÍ£¬²¢Ê¹ÓÃODM½¨Á¢Ñ§ÉúÐÅÏ¢µÄÄ£ÐÍ£¬×îºó½²½âʹÓÃODMÄ£ÐͶÔÏóµÄ·½·¨À´Ö´ÐÐÊý¾Ý¿âµÄÐÂÔö¡¢Ð޸ġ¢É¾³ýºÍ²éѯµÈ²Ù×÷¡£
9¡¢MongoDB ODM£¨2£©
Ê×ÏȽéÉÜMongoDB ODMµÄ°²×°£¬È»ºóʹÓÃODMÀ´Á¬½ÓMongoDBÊý¾Ý¿â£¬½ÓÏÂÀ´½éÉÜODMµÄÄ£ÐÍ£¬²¢Ê¹ÓÃODM½¨Á¢Ñ§ÉúÐÅÏ¢µÄÄ£ÐÍ£¬×îºó½²½âʹÓÃODMÄ£ÐͶÔÏóµÄ·½·¨À´Ö´ÐÐÊý¾Ý¿âµÄÐÂÔö¡¢Ð޸ġ¢É¾³ýºÍ²éѯµÈ²Ù×÷¡£
10¡¢MongoDB ÍøÒ×ÐÂÎÅʵս
Ê×ÏȽéÉÜÈçºÎÀûÓÃFlaskµÄÀ©Õ¹´î½¨ÏîÄ¿µÄ¿ò¼Ü£¬½ÓÏÂÀ´Õ¹Ê¾×îÖÕÎÒÃÇҪʵÏÖµÄЧ¹û¡£È»ºó½áºÏMongoDB ODM À´Íê³ÉÕû¸öʵսÏîÄ¿¡£
11¡¢RedisÊý¾Ý¿â»ù´¡
Ê×ÏȽéÉÜRedisµÄÓÃ;ºÍ³£ÓÃÀàÐÍ£¬ÖîÈ磺String,List,Set,HashµÈ¡£½ÓÏÂÀ´½²½âÈçºÎ°²×°RedisÒÔ¼°ÈçºÎʹÓÃRedis-cliÁ´½ÓRedisÊý¾Ý¿â¡£È»ºóÕë¶Ô²»Í¬µÄÊý¾ÝÀàÐÍ£¬½²½âRedisÌṩµÄÃüÁî¡£×îºó¸ø´ó¼Ò½éÉÜRedisµÄͼÐλ¯¹ÜÀí¹¤¾ß£¬ÒÔ·½±ã²é¿´¿ÉÊÓ»¯µÄÊý¾Ý¡£
12¡¢python²Ù×÷redis
½éÉÜredispyµÄ°²×°ºÍʹÓ㬽²½âʹÓÃget/setµÈÃüÁî²Ù×÷StringÀàÐ͵ÄÊý¾Ý£¬lpush/rpushµÈÃüÁî²Ù×÷ListÀàÐ͵ÄÊý¾Ý£¬sadd/sremµÈÃüÁî²Ù×÷SetÀàÐ͵ÄÊý¾Ý£¬ÒÔ¼°hset/hgetµÈ²Ù×÷HashÀàÐ͵ÄÊý¾Ý¡£
13¡¢python²Ù×÷resisÍøÒ×ÐÂÎÅʵս
Ê×Ïȸù¾ÝʵսÄÚÈÝ·ÖÎö³öÏîÄ¿µÄÄѵ㣬¼´£ºÈçºÎʹÓò»Í¬µÄÊý¾ÝÀàÐÍÀ´´æ´¢ÐÂÎÅÏà¹ØµÄÊý¾Ý¡£È磺ʹÓÃStingÖеÄincrÀ´´æ´¢ÐÂÎŵÄID£¬Ê¹ÓÃHashÀ´´æ´¢µ¥ÌõÐÂÎŵÄÊý¾Ý£¬Ê¹ÓÃSet\ListµÈʵÏÖÐÂÎŵķÖÒ³ºÍ·ÖÀà¡£½ÓÏÂÀ´½²½âredisÊÂÎñ£¬ÒÔ¼°ÔÚÐÂÔöÐÂÎÅÖÐʹÓÃÊÂÎñ¡£×îºó½²½âÈçºÎʹÓÃÕâЩÊý¾Ý½á¹¹À´×éÖ¯Õû¸öÐÂÎŵÄʵÏÖ

ÏÂÔØµØÖ·£º
Óοͣ¬Èç¹ûÄúÒª²é¿´±¾ÌûÒþ²ØÄÚÈÝÇë»Ø¸´

¸ü¶àÌû×ÓÍÆ¼ö

»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

0

Ö÷Ìâ

73

»ØÌû

226

»ý·Ö

Öм¶»áÔ±

Rank: 3Rank: 3

»ý·Ö
226
·¢±íÓÚ 2018-10-13 02:57:36 | ÏÔʾȫ²¿Â¥²ã
wefwfweffe
»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

0

Ö÷Ìâ

287

»ØÌû

692

»ý·Ö

¸ß¼¶»áÔ±

Rank: 4

»ý·Ö
692
·¢±íÓÚ 2018-10-23 08:32:23 | ÏÔʾȫ²¿Â¥²ã
¿¼ÂÇ¿¼ÂÇ·¿É¿Ú¿ÉÀÖÁËÁ˲ÄÁÏÁË
»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

0

Ö÷Ìâ

287

»ØÌû

692

»ý·Ö

¸ß¼¶»áÔ±

Rank: 4

»ý·Ö
692
·¢±íÓÚ 2018-10-23 08:32:41 | ÏÔʾȫ²¿Â¥²ã
¿¼ÂÇ¿¼ÂÇ·¿É¿Ú¿ÉÀÖÁËÁ˲ÄÁÏÁËÍÀÁú¼Ç
»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

0

Ö÷Ìâ

2Íò

»ØÌû

4Íò

»ý·Ö

½ûÖ¹·ÃÎÊ

»ý·Ö
41607
·¢±íÓÚ 2021-9-8 13:53:42 | ÏÔʾȫ²¿Â¥²ã
Ìáʾ: ×÷Õß±»½ûÖ¹»òɾ³ý ÄÚÈÝ×Ô¶¯ÆÁ±Î
»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

QQ|Archiver|ÊÖ»ú°æ|ü\Óü¿Æ¼¼Íø

GMT+8, 2025-11-30 03:55 , Processed in 0.195652 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí