Guest User

Untitled

a guest
May 11th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import contextlib
  4.  
  5. import pymysql
  6.  
  7. from zhihu_test.zhihu_config import spe_config
  8. from zhihu_test.zhihu_const import INSERT_INTO_TABLES
  9.  
  10. # 定义上下文管理器,连接后自动关闭连接
  11. @contextlib.contextmanager
  12. def _mysql():
  13.  
  14. conn = pymysql.connect(**spe_config)
  15. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
  16. try:
  17. yield cursor
  18. finally:
  19. conn.commit()
  20. cursor.close()
  21. conn.close()
  22.  
  23. def _query(sql, args):
  24. with _mysql() as cursor:
  25. row_count = cursor.execute(sql, args)
  26. return row_count
  27.  
  28. def _query_result(sql, args):
  29. with _mysql() as cursor:
  30. cursor.execute(sql, args)
  31. return cursor.fetchall()
  32.  
  33. def _query_many(sql, args):
  34. with _mysql() as cursor:
  35. row_count = cursor.executemany(sql, args)
  36. return row_count
  37.  
  38. def insertInto_auth_group(*args):
  39. """
  40. 向auth_group表插入信息
  41. :param args: 插入的记录元组
  42. :return: 插入成功的条数
  43. """
  44.  
  45. return _query(INSERT_INTO_TABLES['auth_group'], args)
  46.  
  47. def insertInto_auth_group_permissions(*args):
  48. """
  49. 向auth_group_permissions表插入信息
  50. :param args: 插入的记录元组
  51. :return: 插入成功的条数
  52. """
  53.  
  54. return _query(INSERT_INTO_TABLES['auth_group_permissions'], args)
  55.  
  56. def insertInto_content_type_id(*args):
  57. """
  58. 向content_type_id表插入信息
  59. :param args: 插入的记录元组
  60. :return: 插入成功的条数
  61. """
  62.  
  63. return _query(INSERT_INTO_TABLES['content_type_id'], args)
  64.  
  65. def insertInto_auth_user(*args):
  66. """
  67. 向auth_user表插入信息
  68. :param args: 插入的记录元组
  69. :return: 插入成功的条数
  70. """
  71.  
  72. return _query(INSERT_INTO_TABLES['auth_user'], args)
  73.  
  74. def insertInto_auth_user_groups(*args):
  75. """
  76. 向auth_user_groups表插入信息
  77. :param args: 插入的记录元组
  78. :return: 插入成功的条数
  79. """
  80.  
  81. return _query(INSERT_INTO_TABLES['auth_user_groups'], args)
  82.  
  83. def insertInto_auth_user_user_permissions(*args):
  84. """
  85. 向auth_user_user_permissions表插入信息
  86. :param args: 插入的记录元组
  87. :return: 插入成功的条数
  88. """
  89.  
  90. return _query(INSERT_INTO_TABLES['auth_user_user_permissions'], args)
  91.  
  92. def insertInto_collections(*args):
  93. """
  94. 向collections表插入信息
  95. :param args: 插入的记录元组
  96. :return: 插入成功的条数
  97. """
  98.  
  99. return _query(INSERT_INTO_TABLES['collections'], args)
  100.  
  101. def insertInto_collections_articles(*args):
  102. """
  103. 向collections_articles表插入信息
  104. :param args: 插入的记录元组
  105. :return: 插入成功的条数
  106. """
  107.  
  108. return _query(INSERT_INTO_TABLES['collections_articles'], args)
  109.  
  110. def insertInto_comments(*args):
  111. """
  112. 向comments表插入信息
  113. :param args: 插入的记录元组
  114. :return: 插入成功的条数
  115. """
  116.  
  117. return _query(INSERT_INTO_TABLES['comments'], args)
  118.  
  119. def insertInto_daily_article(*args):
  120. """
  121. 向daily_article表插入信息
  122. :param args: 插入的记录元组
  123. :return: 插入成功的条数
  124. """
  125.  
  126. return _query(INSERT_INTO_TABLES['daily_article'], args)
  127.  
  128. def insertInto_django_admin_log(*args):
  129. """
  130. 向django_admin_log表插入信息
  131. :param args: 插入的记录元组
  132. :return: 插入成功的条数
  133. """
  134.  
  135. return _query(INSERT_INTO_TABLES['django_admin_log'], args)
  136.  
  137. def insertInto_django_content_type(*args):
  138. """
  139. 向django_content_type表插入信息
  140. :param args: 插入的记录元组
  141. :return: 插入成功的条数
  142. """
  143.  
  144. return _query(INSERT_INTO_TABLES['django_content_type'], args)
  145.  
  146. def insertInto_django_migrations(*args):
  147. """
  148. 向django_migrations表插入信息
  149. :param args: 插入的记录元组
  150. :return: 插入成功的条数
  151. """
  152.  
  153. return _query(INSERT_INTO_TABLES['django_migrations'], args)
  154.  
  155. def insertInto_django_session(*args):
  156. """
  157. 向django_session表插入信息
  158. :param args: 插入的记录元组
  159. :return: 插入成功的条数
  160. """
  161.  
  162. return _query(INSERT_INTO_TABLES['django_session'], args)
  163.  
  164. def insertInto_tags(*args):
  165. """
  166. 向tags表插入信息
  167. :param args: 插入的记录元组
  168. :return: 插入成功的条数
  169. """
  170.  
  171. return _query(INSERT_INTO_TABLES['tags'], args)
  172.  
  173. import time
  174. if __name__ == '__main__':
  175.  
  176. # insertInto_auth_user('123', f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 1, 'hf', 'f', 'h',
  177. # 'hf@123.com', 1, 1, '2018-05-04')
  178. # insertInto_auth_user('123', f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 0, '张三', '三', '张',
  179. # 'zhangsan@123.com', 0, 1, '2018-05-06')
  180. # insertInto_auth_user('123', f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 0, '李四', '四', '李',
  181. # 'lisi@123.com', 0, 1, '2018-05-06')
  182. # insertInto_auth_user('123', f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 0, '王五', '五', '王',
  183. # 'wangwu@123.com', 0, 1, '2018-05-06')
  184. # insertInto_auth_user('123', f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 0, '赵六', '六', '赵',
  185. # '赵六@123.com', 0, 1, '2018-05-06')
  186. # insertInto_tags('2018-05-04','法学')
  187. # insertInto_tags('2018-05-04', '经济学')
  188. # insertInto_tags('2018-05-04', '心理学')
  189. # insertInto_tags('2018-05-04', '历史学')
  190. # insertInto_tags('2018-05-04', '工学')
  191. # insertInto_daily_article('2018-05-06', '张三的第一篇历史学文章', '张三的第一篇历史学文章的内容', '张三的第一篇历史学文章的图片',
  192. # f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 4, 2)
  193. # insertInto_daily_article('2018-05-06', '李四的第一篇经济学文章', '李四的第一篇经济学文章的内容', '李四的第一篇经济学文章的图片',
  194. # f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 2, 3)
  195. # insertInto_daily_article('2018-05-06', '王五的第一篇法学文章', '王五的第一篇法学文章的内容', '王五的第一篇法学文章的图片',
  196. # f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 1, 4)
  197. # insertInto_daily_article('2018-05-06', '张三的第一篇工学文章', '张三的第一篇工学文章的内容', '张三的第一篇工学文章的图片',
  198. # f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 5, 2)
  199. # insertInto_daily_article('2018-05-06', '张三的第二篇历史学文章', '张三的第二篇历史学文章的内容', '张三的第二篇历史学文章的图片',
  200. # f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", 4, 2)
  201. # insertInto_comments(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}",
  202. # '赵六对张三的第一篇历史学文章的评论', 4, 5)
  203. # insertInto_comments(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}",
  204. # '赵六对李四的第一篇经济学文章的评论', 2, 5)
  205. # insertInto_comments(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}",
  206. # '张三对王五的第一篇法学文章的评论', 1, 2)
  207. # insertInto_comments(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}",
  208. # '李四对张三的第二篇历史学文章的评论', 4, 3)
  209. # insertInto_comments(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}",
  210. # '王五对李四的第一篇经济学文章的评论', 2, 4)
  211. # insertInto_collections(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", '张三的收藏夹', 2)
  212. # insertInto_collections(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", '李四的收藏夹', 3)
  213. # insertInto_collections(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", '王五的收藏夹', 4)
  214. # insertInto_collections(f"{time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))}", '赵六的收藏夹', 5)
  215. # insertInto_collections_articles(1, 2)
  216. # insertInto_collections_articles(1, 3)
  217. # insertInto_collections_articles(2, 1)
  218. # insertInto_collections_articles(2, 3)
  219. # insertInto_collections_articles(2, 4)
  220. # insertInto_collections_articles(3, 1)
  221. # insertInto_collections_articles(3, 2)
  222. # insertInto_collections_articles(3, 4)
  223. # insertInto_collections_articles(3, 5)
  224. # insertInto_collections_articles(4, 1)
  225. # insertInto_collections_articles(4, 2)
  226. # insertInto_collections_articles(4, 3)
Add Comment
Please, Sign In to add comment