coreseek自带了mssql的python数据源演示,我简单的修改了一下,用于mysql测试通过
# -*- coding:utf-8 -*- # coreseek3.2 4.1 python source演示操作mysql数据库 # author: 75271.com # date: 2012-09-13 10:20 from os import path import os import sys import pymysql import datetime class MainSource(object): def __init__(self, conf): self.conf = conf self.idx = 0 self.data = [] self.conn = None self.cur = None def GetScheme(self): #获取结构,docid、文本 return [ ('id' , {'docid':True, } ), ('title', { 'type':'text'} ) ] def GetFieldOrder(self): #字段的优先顺序 return [('title', 'context')] def Connected(self): #如果是数据库,则在此处做数据库连接 if self.conn==None: self.conn = pymysql.connect(host='127.0.0.1', user='root', passwd='xxxxxxx', db='sharejs') self.cur = self.conn.cursor() self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor) sql = 'SELECT id,title FROM articles' self.cur.execute(sql) self.data = [ row for row in self.cur] pass def NextDocument(self): #取得每一个文档记录的调用 if self.idx < len(self.data): item = self.data[self.idx] self.docid = self.id = item['id'] #'docid':True self.title = item['title'].encode('utf-8') self.idx += 1 return True else: return False if __name__ == "__main__": #直接访问演示部分 conf = {} source = MainSource(conf) source.Connected() while source.NextDocument(): print "id=%d, subject=%s" % (source.docid, source.title) pass #eof