我可以将mysql数据库连接设置为类吗? [英] can i set up a mysql db connection as a class ?

查看:71
本文介绍了我可以将mysql数据库连接设置为类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里,

我有一个巨大的应用程序连接到MySQL。有三个线程

不断连接和断开连接到数据库。问题是,

如果出现错误,有时会在没有关闭

连接的情况下出错。我这样连接。

db = MySQLdb.connect(host =" localhost",user =" MyUser",

passwd =" MyPassword",db = Stuff)

cursor = db.cursor()


然后我使用cursor.execute(" SELECT yadda yadda


我的问题是,有没有办法我可以设置一个全局连接,以便当程序加载时,它连接一次,然后保持连接?



i可以分配光标的实例吗?



sk

解决方案

这绝对是要走的路..


-create a database_object

-initialise at start up

-然后根据需要将数据库对象传递给其他类...


如果你想要真的看上去看看一些ORM的......我想

有一个叫做SQLObject的Python吗?

ne*****@xit.net 写道:

嘿那里,连接的情况下出错。我这样连接。
db = MySQLdb.connect(host =" localhost",user =" MyUser",
passwd =" MyPassword",db =" Stuff")
cursor = db.cursor()
然后我使用cursor.execute(SELECT yadda yadda

我的问题是,有没有办法可以设置一个全局连接,以便当程序加载时,它连接一次,然后保持连接?也许
我可以分配光标的实例?

请有人让我知道你是否有任何好主意

sk



这太棒了!


好​​吧,我真的没有太多的时间进入ORMS(在你的

帖子之前,这是我听到的第一个)和我的东西应该在星期一
他是。


但是,如果我能够进行全局数据库连接,并且多个游标

指向同一个连接对象,我该怎么办没有

制作新的db connec tions?


类似

class db(self):

def __init __(self):

db = MySQLdb.connect(host =" localhost",user =" MyUser",

passwd =" MyPassword",

db =" Stuff" ;)

def cursor(self):

cursor = db.cursor()

返回游标

然后在我的线程中需要连接


cursor = db.cursor()

cursor2 = db.cursor()


等等?我可能会在这里打架。我仍然是新课程,

方法和模块。

我在亚马逊的路上确实有基本参考! :)


再次感谢


你在正确的轨道上...创造这样的东西(希望格式化没有去干草电线)


类DB_Connector(对象):


"""简陋的数据库连接类"""


DEF __init __(个体,主机= QUOT;本地主机" ;,用户= QUOT; MYUSER",的passwd = QUOT; MyPassword输入" ;, ** other_db_arguments):


self.host = host

self.user = user

self.passwd = passwd


#解压缩其他数据库参数

self.CreateConnection()


def CreateConnection(个体经营):


self.cursor = MySQLdb.connect(self.host,self.user,self.passwd)

def DestroyConnection(个体经营):


self.cursor.close()


def执行(self,sql_statement):


self.cursor。执行(sql_statement)


返回self.cursor.FetchAll()

然后当你运行你的程序时创建一个对象的实例


db_connection = DB_Connector(''localhost'',''administrator'',''betelgeuse99'',auto_commit = 1,other_keywo rd_arg =" yes")


现在当你将db_connection实例传递给其他类时,副本将自动生成


thread_1_instance = ThreadingClass(db_connection)

thread_2_instance = ThreadingClass(db_connection)

thread_3_instance = ThreadingClass(db_connection)


应该可以工作..


我希望这是有用的

ne ***** @ xit .net 写道:

这太棒了!

好吧,我真的没有太多时间进入ORMS(在你的
之前)帖子,这是我听说过的第一个)我的东西应该在星期一到期。他是。

但是,如果我能够进行全局数据库连接,并且多个游标指向同一个连接对象,我该如何在没有
制作的情况下将其关闭新的数据库连接?

类db(self):
def __init __(self):
db = MySQLdb.connect(host =" localhost" ,user =" MyUser",
passwd =" MyPassword",
db =" Stuff")
def cursor(self):
cursor = db.cursor( )
返回光标

然后在我的线程中需要连接

cursor = db.cursor()
cursor2 = db.cursor()

等等?我可能会在这里打架。我仍然是班级,方法和模块的新手。
我在亚马逊的路上确实有基本参考! :)

再次感谢



hey there,
i have a huge app that connects to MySQL. There are three threads that
are continually connecting and disconnecting to the db. The problem is,
if there is an error, it faults out sometimes without closing the
connection. i connect like this.
db = MySQLdb.connect(host="localhost", user="MyUser",
passwd="MyPassword", db="Stuff")
cursor=db.cursor()

then i use the cursor.execute("SELECT yadda yadda

my question is, is there a way i can set up a global connection so that
when the program loads, it connects once, then stays connected ? maybe
i could assign instances of the cursor ?

please someone let me know if you have any good ideas

sk

解决方案

that''s definitely the way to go ..

-create a database_object
-initialise at start up
-then pass the database object to other classes as needed ...

If you want to get really fancy have a look at some ORM''s ... I think
there is a Python one called SQLObject?

ne*****@xit.net wrote:

hey there,
i have a huge app that connects to MySQL. There are three threads that
are continually connecting and disconnecting to the db. The problem is,
if there is an error, it faults out sometimes without closing the
connection. i connect like this.
db = MySQLdb.connect(host="localhost", user="MyUser",
passwd="MyPassword", db="Stuff")
cursor=db.cursor()

then i use the cursor.execute("SELECT yadda yadda

my question is, is there a way i can set up a global connection so that
when the program loads, it connects once, then stays connected ? maybe
i could assign instances of the cursor ?

please someone let me know if you have any good ideas

sk



This is great !

ok, i dont really have a lot of time to get into the ORMS (before your
post, this is the first i have heard of it) and my stuff is due on
Monday. he he.

but, if i am able to make a global db connection, and multiple cursors
pointing to the same connection object, how do i pull that off without
making new db connections?

something like
class db(self):
def __init__(self):
db = MySQLdb.connect(host="localhost", user="MyUser",
passwd="MyPassword",
db="Stuff")
def cursor(self):
cursor = db.cursor()
return cursor
then have in my threads that need to connect

cursor = db.cursor()
cursor2 = db.cursor()

and so on ? i may be way outta whack here. i am still new at classes,
methods, and modules.
i do have Essential Reference on the way from Amazon though ! :)

thanks again


your on the right track ... create something like this ( hope the formatting doesn''t go to hay wire )

class DB_Connector(object):

""" Humble Database Connection Class """

def __init__(self, host="localhost", user="MyUser",passwd="MyPassword", **other_db_arguments):

self.host = host
self.user = user
self.passwd = passwd

# Unpack Other Database Arguments Here
self.CreateConnection()

def CreateConnection( self ):

self.cursor = MySQLdb.connect(self.host, self.user, self.passwd)

def DestroyConnection( self ):

self.cursor.close()

def Execute( self, sql_statement ):

self.cursor.Execute( sql_statement )

return self.cursor.FetchAll()

Then when you run your program create an instance of the object

db_connection = DB_Connector( ''localhost'', ''administrator'', ''betelgeuse99'', auto_commit=1, other_keyword_arg="yes" )

now when you pass the db_connection instance to other classes, a copy will be made automagically

thread_1_instance = ThreadingClass( db_connection )
thread_2_instance = ThreadingClass( db_connection )
thread_3_instance = ThreadingClass( db_connection )

should work ..

I hope this is useful

ne*****@xit.net wrote:

This is great !

ok, i dont really have a lot of time to get into the ORMS (before your
post, this is the first i have heard of it) and my stuff is due on
Monday. he he.

but, if i am able to make a global db connection, and multiple cursors
pointing to the same connection object, how do i pull that off without
making new db connections?

something like
class db(self):
def __init__(self):
db = MySQLdb.connect(host="localhost", user="MyUser",
passwd="MyPassword",
db="Stuff")
def cursor(self):
cursor = db.cursor()
return cursor
then have in my threads that need to connect

cursor = db.cursor()
cursor2 = db.cursor()

and so on ? i may be way outta whack here. i am still new at classes,
methods, and modules.
i do have Essential Reference on the way from Amazon though ! :)

thanks again



这篇关于我可以将mysql数据库连接设置为类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆