Rev 9672 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2437 | subik | 1 | #!/usr/bin/env python |
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | """OK. This is a sample Scribus database connection with Python HOWTO. |
||
5 | |||
6 | DESCRIPTION: |
||
7 | I've got questions about DB and Scribus in my personal mailbox 3-4 |
||
8 | times in a month. So this is an common answer for asking people. |
||
9 | |||
10 | Even through I'm an Oracle user/developer I choose MySQL for this |
||
11 | example, because of its presence in the Linux distributions and |
||
12 | hosting availability too. |
||
13 | But the DB server doesn't matter due the PEP 249 - standard DB interface |
||
14 | (http://www.python.org/peps/pep-0249.html). |
||
15 | There are various modules for database accessing: |
||
16 | http://www.python.org/topics/database/modules.html |
||
17 | |||
18 | Anyway - this script provides connection to the database server by the |
||
19 | specified values in the hostname, dbname, username, and password variables. |
||
20 | Then it checks the system for table names in the specified databases |
||
21 | and it displays it in the new document finally. Easy and understandable. |
||
22 | |||
23 | |||
24 | CONTACT: |
||
25 | email : petr@yarpen.cz |
||
26 | |||
27 | |||
28 | LICENSE: |
||
29 | This program is free software; you can redistribute it and/or modify |
||
30 | it under the terms of the GNU General Public License as published by |
||
31 | the Free Software Foundation; either version 2 of the License, or |
||
32 | (at your option) any later version. |
||
33 | |||
34 | This program is distributed in the hope that it will be useful, |
||
35 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
36 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
37 | GNU General Public License for more details. |
||
38 | |||
39 | You should have received a copy of the GNU General Public License |
||
40 | along with this program; if not, write to the Free Software |
||
18122 | mrdocs | 41 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
2437 | subik | 42 | """ |
43 | |||
44 | import sys |
||
45 | |||
46 | # environment checking |
||
47 | try: |
||
48 | import scribus |
||
49 | except ImportError: |
||
50 | print "This script only runs from within Scribus." |
||
51 | sys.exit(1) |
||
52 | |||
53 | try: |
||
54 | import MySQLdb |
||
55 | except ImportError: |
||
56 | print "You must have 'MySQLdb' installed." |
||
57 | sys.exit(1) |
||
58 | |||
59 | |||
60 | # connection parameters |
||
61 | hostname = 'server.foo.org' |
||
62 | dbname = 'name' |
||
63 | username = 'username' |
||
64 | password = 'password' |
||
65 | |||
66 | # connection to the network wide server would be time consuming. So get the hint to the user. |
||
67 | scribus.statusMessage('Connecting to the ' + hostname + ' server. Be patient, please...') |
||
68 | |||
69 | # Database related issues |
||
70 | try: |
||
71 | conn = MySQLdb.connect(passwd=password, db=dbname, host=hostname, user=username) |
||
72 | except: |
||
73 | scribus.messageBox('DB connection example', 'Connection error. You should specify your login in the script') |
||
74 | sys.exit(1) |
||
75 | |||
76 | cur = conn.cursor() |
||
77 | # get the list of the databases |
||
78 | # it's like 'select * from dba_tables' in Oracle |
||
79 | count = cur.execute('show tables') |
||
80 | # formating the output |
||
81 | result = str(count) + ' table(s) in the ' + dbname + ' database.\n\n' |
||
82 | for i in cur.fetchall(): |
||
83 | result = result + i[0] + '\n' |
||
84 | |||
85 | # Scribus presentation part |
||
86 | scribus.newDoc(scribus.PAPER_A5, (10, 10, 20, 20), scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT) |
||
87 | txtName = scribus.createText(10, 10, 200, 200) |
||
88 | scribus.setText(result, txtName) |
||
89 | |||
90 | scribus.statusMessage('Script done.') |