473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Python script and database connection.

Hi,
I wrote a Python scripts, i need to connect this with my database in postgresql, but while running the __init__.py it gives the message that unable to connect to database. Please guide me. my code is as follow...
try:
conn=psycopg2.connect("dbname="+ dbname + "user=" + username + "password=" + password)
print"connected"
except:
print "I am unable to connect to the database, exiting."
sys.exit()
Apr 27 '08 #1
1 2032
jlm699
314 100+
Hi,
I wrote a Python scripts, i need to connect this with my database in postgresql, but while running the __init__.py it gives the message that unable to connect to database. Please guide me. my code is as follow...
try:
conn=psycopg2.connect("dbname="+ dbname + "user=" + username + "password=" + password)
print"connected"
except:
print "I am unable to connect to the database, exiting."
sys.exit()
On first inspection it looks like you are missing spaces between your string concatenation so that inside the connect function you are passing the following:
"dbname=foobaruser=usrfoopassword=passwd".
I find it easier to make the string first so that you can do some sanity-checking and then passing the string to the connect function. Here's a snippet from a postgresql gui that I made:
Expand|Select|Wrap|Line Numbers
  1. dsn = "host=" + dbip + " dbname=" + dbname +\
  2.     " user=" + dbuser + " password=" + dbpass
  3. #Defaults: ip=192.168.100.48 dbname=Tools user=toolsusr password=toolsdb"
  4. try:
  5.     db = psycopg2.connect(dsn)
  6. except:
  7.     #WriteToLogs('Cannot connect to Database, please check your Database Options')
  8.     return
  9.  
Notice the spaces before dbname=, user=, and password= .... HTH
Apr 27 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: writeson | last post by:
Hi all, I helped one of my co-workers put together an XMLRPC Python script that allowed him to get database data from remote machines. This was done because the source of the data could be...
13
by: Graham | last post by:
I need a SQL Server or ODBC package for Python. Can anyone help please? Have search the Python packages under Database and there is no reference to either SQL Server or ODBC. Thanks Graham
2
by: Bobby | last post by:
Hello everyone I have a question. The school I am working for is in the beginning process of having a webpage that will direct students to download there homework and be able to view there info...
5
by: Tim Penhey | last post by:
Are there any python drivers that work with the version 8 beta? The version seven ones didn't. Thanks, Tim ---------------------------(end of broadcast)--------------------------- TIP 7:...
9
by: Jerim79 | last post by:
I am no PHP programmer. At my current job I made it known that I was no PHP programmer during the interview. Still they have given me a script to write with the understanding that it will take me a...
1
by: Andrew Murray | last post by:
I'm a novice at coding and cannot get the script below to work I'm receiving an Error 500 in the web browser when trying to run this script. The site is www.murraywebs.com and the link is...
34
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas...
10
by: Jerim | last post by:
I am attempting to put together one script that pulls data from one database on its own server, and data from another database on its own server, which is off-site. Server 1 - Only allows shared...
2
by: kj | last post by:
I'm sure this is a simple, but recurrent, problem for which I can't hit on a totally satisfactory solution. As an example, suppose that I want write a module X that performs some database...
3
by: Raja | last post by:
Hi, I am trying to write a cgi program which would be executed on browser with Apache server installed. The program would make a connection to a database using cx_Oracle module and display...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.