2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 #DBMysql is a MySQL access database class
26 from MySQLdb
import IntegrityError
27 from MySQLdb
import ProgrammingError
31 def __init__(self
, host
, user
, password
, database
, character_set
, table_prefix
= ''):
34 self
.password
= password
35 self
.database
= database
36 self
.character_set
= character_set
37 self
.table_prefix
= table_prefix
40 #Create the database connexion
41 #TODO : finish (begin :) ) error catch and conf parameters...
42 def connect_database(self
):
43 #self.db = MySQLdb.connect (host = "localhost", user = "root", passwd = "root", db = "merlin")
44 self
.db
= MySQLdb
.connect (host
= self
.host
, user
= self
.user
, \
45 passwd
= self
.password
, db
= self
.database
)
46 self
.db
.set_character_set(self
.character_set
)
47 self
.db_cursor
= self
.db
.cursor ()
48 self
.db_cursor
.execute('SET NAMES %s;' % self
.character_set
)
49 self
.db_cursor
.execute('SET CHARACTER SET %s;' % self
.character_set
)
50 self
.db_cursor
.execute('SET character_set_connection=%s;' % self
.character_set
)
51 #Thanks http://www.dasprids.de/blog/2007/12/17/python-mysqldb-and-utf-8 for utf8 code :)
56 def execute_query(self
, query
):
57 #print "[MysqlDB]I run query", query, "\n"
59 self
.db_cursor
.execute(query
)
61 except IntegrityError
, exp
:
62 print "[MysqlDB] Warning : a query raise an integrity error : %s, %s" % (query
, exp
)
63 except ProgrammingError
, exp
:
64 print "[MysqlDB] Warning : a query raise a programming error : %s, %s" % (query
, exp
)