Fix : get back LiveStatus as default.
[shinken.git] / shinken / db_mysql.py
blob791d3eb19f8bd65359729f4286814783fd4d726c
1 #!/usr/bin/env python
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
27 import MySQLdb
28 from MySQLdb import IntegrityError
29 from MySQLdb import ProgrammingError
32 class DBMysql(DB):
33 def __init__(self, host, user, password, database, character_set, table_prefix = ''):
34 self.host = host
35 self.user = user
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 :)
56 #Just run the query
57 #TODO: finish catch
58 def execute_query(self, query):
59 #print "[MysqlDB]I run query", query, "\n"
60 try:
61 self.db_cursor.execute(query)
62 self.db.commit ()
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)