[Python] coreseek(sphinx)基于mysql的python数据源演示代码 →→→→→进入此内容的聊天室

来自 , 2019-07-27, 写在 Python, 查看 105 次.
URL http://www.code666.cn/view/23fa71cc
  1. # -*- coding:utf-8 -*-
  2. # coreseek3.2 4.1 python source演示操作mysql数据库
  3. # author: sharejs.com
  4. # date: 2012-09-13 10:20
  5.  
  6. from os import path
  7. import os
  8. import sys
  9. import pymysql
  10. import datetime
  11.  
  12. class MainSource(object):
  13.     def __init__(self, conf):
  14.         self.conf =  conf
  15.         self.idx = 0
  16.         self.data = []
  17.         self.conn = None
  18.         self.cur = None
  19.  
  20.     def GetScheme(self):  #获取结构,docid、文本
  21.         return [
  22.             ('id' , {'docid':True, } ),
  23.             ('title', { 'type':'text'} )
  24.         ]
  25.  
  26.     def GetFieldOrder(self): #字段的优先顺序
  27.         return [('title', 'context')]
  28.  
  29.     def Connected(self):   #如果是数据库,则在此处做数据库连接
  30.         if self.conn==None:
  31.             self.conn = pymysql.connect(host='127.0.0.1', user='root', passwd='xxxxxxx', db='sharejs')
  32.             self.cur = self.conn.cursor()
  33.             self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
  34.             sql = 'SELECT id,title FROM articles'
  35.             self.cur.execute(sql)
  36.             self.data = [ row for row in self.cur]
  37.         pass
  38.  
  39.     def NextDocument(self):   #取得每一个文档记录的调用
  40.         if self.idx < len(self.data):
  41.             item = self.data[self.idx]
  42.             self.docid = self.id = item['id'] #'docid':True
  43.             self.title = item['title'].encode('utf-8')
  44.             self.idx += 1
  45.             return True
  46.         else:
  47.             return False
  48.  
  49. if __name__ == "__main__":    #直接访问演示部分
  50.     conf = {}
  51.     source = MainSource(conf)
  52.     source.Connected()
  53.  
  54.     while source.NextDocument():
  55.         print "id=%d, subject=%s" % (source.docid, source.title)
  56.     pass
  57. #eof
  58.  
  59. #//python/5037

回复 "coreseek(sphinx)基于mysql的python数据源演示代码"

这儿你可以回复上面这条便签

captcha