利用python對(duì)mysql表做全局模糊搜索并分頁實(shí)例
在寫django項(xiàng)目的時(shí)候,有的數(shù)據(jù)沒有使用模型管理(數(shù)據(jù)表是動(dòng)態(tài)添加的),所以要直接使用mysql。前端請(qǐng)求數(shù)據(jù)的時(shí)候可能會(huì)指定這幾個(gè)參數(shù):要請(qǐng)求的頁號(hào),頁大小,以及檢索條件。
'''tableName: 表名pageNum: 請(qǐng)求的頁的編號(hào)pageSize: 每一頁的大小searchInfo: 需要全局查詢的信息'''def getMysqlData(tableName, pageNum, pageSize, searchInfo):# 使用MySQLdb獲取的mysql游標(biāo) cursor = getCursor() # 用以獲取列標(biāo)題 colSql = ’select * from {} limit 1’.format(tableName) cursor.execute(colSql) columns = [col[0] for col in cursor.description] # 轉(zhuǎn)化查詢信息為sql searchCondition = ’,’.join(columns) searchInfo = 'WHERE CONCAT({}) like ’%{}%’'.format(searchCondition, searchInfo) # 用以獲取總數(shù) totalSql = 'select count(*) from {} {};'.format(tableName, searchInfo) cursor.execute(totalSql) total = cursor.fetchone()[0] # 用以獲取具體數(shù)據(jù) limit1 = (pageNum - 1) * pageSize limit2 = pageSize dataSql = 'select * from {} {} limit {},{};'.format(tableName, searchInfo, limit1, limit2) cursor.execute(dataSql) data = [ dict(zip(columns, row)) for row in cursor.fetchall() ] return (total, columns, data)'''total: 符合條件的數(shù)據(jù)總數(shù)columns: 字段名列表 [’字段名1’, ’字段名2’, ...]data: 數(shù)據(jù)對(duì)象列表 [{’字段名1’: 數(shù)據(jù)1,’字段名2’:數(shù)據(jù)1, ...},{’字段名1’: 數(shù)據(jù)2, ’字段名2’: 數(shù)據(jù)2, ...}, ...]'''
補(bǔ)充知識(shí):django 分頁查詢搜索--傳遞查詢參數(shù),翻頁時(shí)帶上查詢參數(shù)
django在分頁查詢的時(shí)候,翻頁時(shí),v層要傳遞查詢參數(shù),相應(yīng)的html翻頁連接也要帶上查詢參數(shù)
直接上代碼
view:
@login_requireddef search_name(request): username = request.session.get(’user’) search_name = request.GET.get(’name’) if search_name == None: search_name = request.GET.get(’name’) event_list = Event.objects.filter(name__contains=search_name) paginator = Paginator(event_list, 2) page = request.GET.get(’page’) try: contacts = paginator.page(page) except PageNotAnInteger: # 如果page不是整數(shù),取第一頁面數(shù)據(jù) contacts = paginator.page(1) except EmptyPage: # 如果page不在范圍內(nèi),則返回最后一頁數(shù)據(jù) contacts = paginator.page(paginator.num_pages) return render(request,’event_manage.html’,{’user’:username,’events’:contacts,’name’:search_name})
html:
<!--列表分頁器--> <div class='pagination'> <span class='step-links'> {% if events.has_previous %} <a href='http://www.aoyou183.cn/bcjs/?page={{ events.previous_page_number }}&&name={{ name }}' rel='external nofollow' >previous</a> {% endif %} <span class='current'> Page {{ events.number }} of {{ events.paginator.num_pages }} </span> {% if events.has_next %} <a href='http://www.aoyou183.cn/bcjs/?page={{ events.next_page_number }}&name={{ name }}' rel='external nofollow' >next</a> {% endif %} </span> </div> {% include ’include/pager.html’ %}
以上這篇利用python對(duì)mysql表做全局模糊搜索并分頁實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp批量添加修改刪除操作示例代碼2. ASP實(shí)現(xiàn)加法驗(yàn)證碼3. PHP循環(huán)與分支知識(shí)點(diǎn)梳理4. 讀大數(shù)據(jù)量的XML文件的讀取問題5. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案6. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)7. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能8. 解析原生JS getComputedStyle9. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲10. css代碼優(yōu)化的12個(gè)技巧
