免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3060 | 回复: 5
打印 上一主题 下一主题

请教一个python连接mysql数据库的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-16 01:37 |只看该作者 |倒序浏览
5可用积分
我是一个python的初学者,最近有个问题很困惑,希望可以得到你的帮助,先表示感谢。

具体问题是,程序运行完一直在连接数据库,我想数据取完就彻底不要在连接数据库了,不然相应好慢。但因为我还是不熟悉python的循环。只要窗口移动或者窗口最大化,程序都还要继续连接数据库,弄得程序很慢。希望大家帮忙看看。非常感谢。代码如下:


# -*- coding: cp936 -*-
import wx
import wx.grid
import MySQLdb
class TestTable(wx.grid.PyGridTableBase):
      def __init__(self):
          wx.grid.PyGridTableBase.__init__(self)
          self.colLabels = ["Cmts", "ip地址","mac地址"]
        
      def GetNumberRows(self):
          return 30
      def GetNumberCols(self):
          return 3
      def IsEmptyCell(self, row, col):
          return False   
        
      def SetValue(self, row, col, value):
          pass
            
      def GetColLabelValue(self, col):
          return self.colLabels[col]
   
      def GetValue(self, row, col):
          db=MySQLdb.connect(host='localhost',user='root',passwd='26817873',db='cmcheck')
          cur=db.cursor()
          cur.execute('select cmts,ip,mac from cmmonitor where ip like "10.1.33.%";')
          ###row=cur.fetchone()
          rowtab=cur.fetchall()   
          return "%s" % rowtab[row][col]
          db.close()
          db.commit()
          cur.close()
                  
         
      
class TestFrame(wx.Frame):
      def __init__(self):
          wx.Frame.__init__(self, None, title="Ip对应的MAC地址",
                          size=(400,200))
          grid = wx.grid.Grid(self)
          table = TestTable()
          grid.SetTable(table, True)
if __name__ == '__main__':
   app = wx.PySimpleApp()
   frame = TestFrame()
   frame.Show()
   app.MainLoop()

最佳答案

查看完整内容

回来翻自己的老贴,看到了这个贴子。楼主好象欠我五分。

论坛徽章:
0
2 [报告]
发表于 2008-11-16 01:37 |只看该作者
回来翻自己的老贴,看到了这个贴子。

楼主好象欠我五分。

论坛徽章:
0
3 [报告]
发表于 2008-11-16 08:47 |只看该作者
应该在 TestTable 的 __init__ 中连接并读入整个表格,

GetValue 中不要有连接代码,只要根据行列返回值即可。

论坛徽章:
0
4 [报告]
发表于 2008-11-16 09:59 |只看该作者

回复 #2 retuor 的帖子

能否详细说明一下。通过代码示例,谢谢

论坛徽章:
0
5 [报告]
发表于 2008-11-16 10:16 |只看该作者
我这里没有装 mysql,写了也无法测试。

这个是从网上抄回来的:


  1. import MySQLdb

  2. # Create a connection object and create a cursor
  3. Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="joe", passwd="egf42" db="tst")
  4. Cursor = Con.cursor()

  5. # Make SQL string and execute it
  6. sql = "SELECT * FROM Users"
  7. Cursor.execute(sql)

  8. # Fetch all results from the cursor into a sequence and close the connection
  9. Results = Cursor.fetchall()
  10. Con.close()
复制代码


象上面的代码那样,SELECT *,fetchall,那么全部信息都在 Results 里了。这时你关掉连接。由于 __init__ 只执行一次,所以以后都不会再连。

而在 GetValue 那里,你只需要写一段代码,把相应的信息从 Results 中读取出来就行了。

论坛徽章:
0
6 [报告]
发表于 2008-11-16 23:05 |只看该作者

回复 #4 retuor 的帖子

非常感谢你,已经根据你说的解决了。谢谢!!
做如下调整就OK了。

# -*- coding: cp936 -*-
import wx
import wx.grid
import MySQLdb

# Create a connection object and create a cursor
Con = MySQLdb.connect(host='localhost',user='root',passwd='26817873',db='cmcheck')
Cursor = Con.cursor()

# Make SQL string and execute it
sql = "select cmts,ip,mac from cmmonitor;"
Cursor.execute(sql)

# Fetch all results from the cursor into a sequence and close the connection
Results = Cursor.fetchall()
Con.close()

class TestTable(wx.grid.PyGridTableBase):
      def __init__(self):
          wx.grid.PyGridTableBase.__init__(self)
          self.colLabels = ["Cmts", "ip地址","mac地址"]

      def GetNumberRows(self):
          return 30

      def GetNumberCols(self):
          return 3

      def IsEmptyCell(self, row, col):
          return False   
        
      def SetValue(self, row, col, value):
          pass
            
      def GetColLabelValue(self, col):
          return self.colLabels[col]
   
      def GetValue(self, row, col):
          return "%s" % Results[row][col]

                  
         
      
class TestFrame(wx.Frame):
      def __init__(self):
          wx.Frame.__init__(self, None, title="Ip对应的MAC地址",
                          size=(400,200))
          grid = wx.grid.Grid(self)
          table = TestTable()
          grid.SetTable(table, True)

if __name__ == '__main__':
   app = wx.PySimpleApp()
   frame = TestFrame()
   frame.Show()
   app.MainLoop()
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP