python 查询 Mysql 并输出到文本
学习Python后写的第二个脚本,逻辑有点乱,等以后在优化! #!/usr/bin/env python'''author:wenminCreated on 2013-4-23'''?import MySQLdb?class MySQLHelper: #配置数据库信息并连接 def __init__(self,host="****",user="****",password="****",port=****
学习Python后写的第二个脚本,逻辑有点乱,等以后在优化!
#!/usr/bin/env python ''' author:wenmin Created on 2013-4-23 ''' ? import MySQLdb ? class MySQLHelper: #配置数据库信息并连接 def __init__(self,host="****",user="****",password="****",port=****,charset="utf8"): self.host=host self.user=user self.password=password self.port=port self.charset=charset try: self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.password,port=self.port) self.conn.set_character_set(self.charset) self.cur=self.conn.cursor() except MySQLdb.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) #取出需要统计的数据库名称 def db_name(self): un_db_name = ['information_schema','cz','ecshop','edutone','gz','mysql','newparent','parent','performance_schema','test','xx','yyhd'] name = [] try: self.cur.execute('show databases') for row in self.cur.fetchall(): for i in row: if i not in un_db_name: name.append(i) return name except MySQLdb.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) #指定查询的数据库名称 def selectDb(self,db): try: self.conn.select_db(db) except MySQLdb.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) #查询用户数 def user_quantity(self): try: self.cur.execute('select count(distinct phone) from pc_user') for row in self.cur.fetchall(): return row[0] except MySQLdb.Error as e: print("Mysql Error:%s\n" %(e)) #查询用户详细信息 def user_details(self,db): try: self.cur.execute('select a.phone,a.parents_name,a.student_name,a.type,c.grade_name,b.class_name,a.sex,"'+str(db)+'"from pc_user a,pc_class b,pc_grade c where a.class_id=b.id and a.grade_id=c.id group by a.phone') #for row in self.cur.fetchall(): # return row s = self.cur.fetchall() return s except MySQLdb.Error as e: print("Mysql Error:%s\n" %(e)) #查询议案数 def monion_quantity(self): try: self.cur.execute('select count(distinct id) from pc_motions') for row in self.cur.fetchall(): return row[0] except MySQLdb.Error as e: print("Mysql Error:%s\n" %(e)) #查询有效议案 def monion_details(self): try: self.cur.execute('select `motion_id`,count(*) from pc_motion_voterec group by motion_id having count(*)>5') # for row in self.cur.fetchall(): # return row s = self.cur.fetchall() return s except MySQLdb.Error as e: print("Mysql Error:%s\n" %(e)) ? def close(self): self.cur.close() self.conn.close() ? if __name__ == '__main__': school_db_name = MySQLHelper() school = school_db_name.db_name() for i in school: file = open('jwh/%s' % i,'w') file.write("================================================\n") d_name = str(i) i = MySQLHelper() i.selectDb(d_name) file.write("user_quantity:"+str(i.user_quantity())+"\n") file.write("================================================\n") s=i.user_details(d_name) for p in s: for m in p: file.write(str(m)+' ') file.write("\n") file.write("================================================\n") file.write("monion_quantity:"+str(i.monion_quantity())+"\n") file.write("================================================\n") l=i.monion_details() for p in l: for s in p: file.write(str(s)+" ") file.write("\n") i.close() file.close() school_db_name.close()
原文地址:python 查询 Mysql 并输出到文本, 感谢原作者分享。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

在開發一個小型應用時,我遇到了一個棘手的問題:需要快速集成一個輕量級的數據庫操作庫。嘗試了多個庫後,我發現它們要么功能過多,要么兼容性不佳。最終,我找到了minii/db,這是一個基於Yii2的簡化版本,完美地解決了我的問題。

在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。
