2012-12-17 87 views
1

我想从数据库“细节”中检索值,因为我想用数据库创建一个连接,如果连接存在,请不要建立新连接并继续前一个连接。为此,我曾尝试与以下:单身数据库连接

import MySQLdb 
import re 
import os 

class Find: 

    def __init__(self,addr): 
     self.addr = addr 


    dsn = { 
    'username': 'xyz', 
    'password': 'xyz', 
    'hostname': 'localhost', 
    'database': 'details' 
    } 
    the_database_connection = False 

    def connect(self,dsn): 
     '''This function saves the database connection, so if invoke this again, it gives you the same one, rather than making a second connection.''' 
     global the_database_connection 
     if not the_database_connection: 
      try: 
       the_database_connection = MySQLdb.connect(
        host=dsn['hostname'], 
        user=dsn['username'], 
        passwd=dsn['password'], 
        db=dsn['database']) 
      # so modifications take effect automatically 
       the_database_connection.autocommit(True) 
      except MySQLdb.Error, e: 
       print ("Couldn't connect to database. MySQL error %d: %s" %(e.args[0], e.args[1])) 
     return the_database_connection 
     x=conn.cursor() 
     sql = "select * from persons where address = %s" % addr 
     x.execute(sql) 
     rows = x.fetchall() 
     for row in rows: 
      print row 

if __name__ == "__main__": 
    a = Find(addr = "new street") 
    a.connect() 

但这种示值误差:a.connect需要2个参数是一个定义......我 如何界定上述DSN。

+0

你叫a.connect无DSN参数,将其删除,并使用self.dsn – sneawo

回答

0

您正在尝试重新创建连接池。

不幸的是,您的解决方案不会按原样工作。只有当所有连接参数 - 主机,端口,数据库,用户名,密码完全相同时,才能使用相同的连接。在你的情况下,你会在任何情况下返回相同的连接,这是完全错误的 - 如果任何属性不同,你必须创建新的连接。

相反,只需使用现有的连接池库之一,如pysqlpoolDBUtils(我确信还有其他的)。

+0

我想与DB只有一次连接并访问数据。我不想再次击中Db,那么对于这个所需的解决方案是什么 –

+0

如果连接参数不同,你不能避免这种情况。如果你使用连接池,它会自动为你做,而不需要你做任何事 – mvp

+0

参数总是相同的 –

0

您没有根据需要提供a.connect函数的dsn信息。将dsn信息从课程中拉出并使其成为主要功能的一部分。然后将其作为a.connect的参数反馈回来。像这样:

import MySQLdb 
import re 
import os 

class Find: 

    def __init__(self,addr): 
     self.addr = addr 

    def connect(self,dsn): 
     '''This function saves the database connection, so if invoke this again, it  gives you the same one, rather than making a second connection.''' 
     global the_database_connection 
     if not the_database_connection: 
      try: 
       the_database_connection = MySQLdb.connect(
        host=dsn['hostname'], 
        user=dsn['username'], 
        passwd=dsn['password'], 
        db=dsn['database']) 
       # so modifications take effect automatically 
       the_database_connection.autocommit(True) 
      except MySQLdb.Error, e: 
       print ("Couldn't connect to database. MySQL error %d: %s" %(e.args[0], e.args[1])) 
     return the_database_connection 
     x=conn.cursor() 
     sql = "select * from persons where address = %s" % addr 
     x.execute(sql) 
     rows = x.fetchall() 
     for row in rows: 
      print row 

if __name__ == "__main__": 
    a = Find(addr = "new street") 
    dsn = { 
    'username': 'xyz', 
    'password': 'xyz', 
    'hostname': 'localhost', 
    'database': 'details' 
    } 
    the_database_connection = False 
    a.connect(dsn) 
+0

它显示错误:def connect(self,self.dsn):SyntaxError:无效的语法 –

+0

删除了我的第二个代码片段。使用第一个。如果数据库连接参数永远不会改变,那么只需在主函数启动时初始化它们。 – joed4no

+0

它工作正常......但我有另一个问题:我把全局the_database_connection后print statement1和另一个打印语句2在尝试结束。当我正在执行查询第一次两个语句打印,但另一次只有一个打印语句应该打印为什么都打印?我的目标是建立一个连接一次,并重复使用相同的连接进行进一步的查询。 –