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
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 #DBMysql is a MySQL access database class
25 from shinken
.db
import DB
28 from MySQLdb
import IntegrityError
29 from MySQLdb
import ProgrammingError
33 def __init__(self
, host
, user
, password
, database
, character_set
, table_prefix
= ''):
36 self
.password
= password
37 self
.database
= database
38 self
.character_set
= character_set
39 self
.table_prefix
= table_prefix
42 #Create the database connexion
43 #TODO : finish (begin :) ) error catch and conf parameters...
44 def connect_database(self
):
45 #self.db = MySQLdb.connect (host = "localhost", user = "root", passwd = "root", db = "merlin")
46 self
.db
= MySQLdb
.connect (host
= self
.host
, user
= self
.user
, \
47 passwd
= self
.password
, db
= self
.database
)
48 self
.db
.set_character_set(self
.character_set
)
49 self
.db_cursor
= self
.db
.cursor ()
50 self
.db_cursor
.execute('SET NAMES %s;' % self
.character_set
)
51 self
.db_cursor
.execute('SET CHARACTER SET %s;' % self
.character_set
)
52 self
.db_cursor
.execute('SET character_set_connection=%s;' % self
.character_set
)
53 #Thanks http://www.dasprids.de/blog/2007/12/17/python-mysqldb-and-utf-8 for utf8 code :)
58 def execute_query(self
, query
):
59 #print "[MysqlDB]I run query", query, "\n"
61 self
.db_cursor
.execute(query
)
63 except IntegrityError
, exp
:
64 print "[MysqlDB] Warning : a query raise an integrity error : %s, %s" % (query
, exp
)
65 except ProgrammingError
, exp
:
66 print "[MysqlDB] Warning : a query raise a programming error : %s, %s" % (query
, exp
)