Back to index

scribus-ng  1.3.4.dfsg+svn20071115
sample_db_usage.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 """OK. This is a sample Scribus database connection with Python HOWTO.
00005 
00006 DESCRIPTION:
00007 I've got questions about DB and Scribus in my personal mailbox 3-4
00008 times in a month. So this is an common answer for asking people.
00009 
00010 Even through I'm an Oracle user/developer I choose MySQL for this
00011 example, because of its presence in the Linux distributions and
00012 hosting availability too.
00013 But the DB server doesn't matter due the PEP 249 - standard DB interface
00014 (http://www.python.org/peps/pep-0249.html).
00015 There are various modules for database accessing:
00016 http://www.python.org/topics/database/modules.html
00017 
00018 Anyway - this script provides connection to the database server by the
00019 specified values in the hostname, dbname, username, and password variables.
00020 Then it checks the system for table names in the specified databases
00021 and it displays it in the new document finally. Easy and understandable.
00022 
00023 
00024 CONTACT:
00025 email : petr@yarpen.cz
00026 
00027 
00028 LICENSE:
00029 This program is free software; you can redistribute it and/or modify
00030 it under the terms of the GNU General Public License as published by
00031 the Free Software Foundation; either version 2 of the License, or
00032 (at your option) any later version.
00033 
00034 This program is distributed in the hope that it will be useful,
00035 but WITHOUT ANY WARRANTY; without even the implied warranty of
00036 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00037 GNU General Public License for more details.
00038 
00039 You should have received a copy of the GNU General Public License
00040 along with this program; if not, write to the Free Software
00041 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00042 """
00043 
00044 import sys
00045 
00046 # environment checking
00047 try:
00048     import scribus
00049 except ImportError:
00050     print "This script only runs from within Scribus."
00051     sys.exit(1)
00052 
00053 try:
00054     import MySQLdb
00055 except ImportError:
00056     print "You must have 'MySQLdb' installed."
00057     sys.exit(1)
00058 
00059 
00060 # connection parameters
00061 hostname = 'server.foo.org'
00062 dbname = 'name'
00063 username = 'username'
00064 password = 'password'
00065 
00066 # connection to the network wide server would be time consuming. So get the hint to the user.
00067 scribus.statusMessage('Connecting to the ' + hostname + ' server. Be patient, please...')
00068 
00069 # Database related issues
00070 try:
00071     conn = MySQLdb.connect(passwd=password, db=dbname, host=hostname, user=username)
00072 except:
00073        scribus.messageBox('DB connection example', 'Connection error. You should specify your login in the script')
00074        sys.exit(1)
00075 
00076 cur = conn.cursor()
00077 # get the list of the databases
00078 # it's like 'select * from dba_tables' in Oracle
00079 count = cur.execute('show tables')
00080 # formating the output
00081 result = str(count) + ' table(s) in the ' + dbname + ' database.\n\n'
00082 for i in cur.fetchall():
00083     result = result + i[0] + '\n'
00084 
00085 # Scribus presentation part
00086 scribus.newDoc(scribus.PAPER_A5, (10, 10, 20, 20), scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT)
00087 txtName = scribus.createText(10, 10, 200, 200)
00088 scribus.setText(result, txtName)
00089 
00090 scribus.statusMessage('Script done.')