简体   繁体   中英

why can't I connect python to firebird in ubuntu 12.04?

Hi I'm novice in python and firebird too, my problem is i'm trying to connect my python program to a database in firebird, I've installed firebird (is currently on /opt/firebird) I have created my database(test.fdb) with a table(it works fine in the firebird)

create table languages
(
  name               varchar(20),
  year_released      integer
);

insert into languages (name, year_released) values ('C',        1972);
insert into languages (name, year_released) values ('Python',   1991);

but when i'm trying to run in pydev occurs a problem.

import fdb

con = fdb.connect(dsn="/tmp/test.fdb", user="fernando", password="root")

# Create a Cursor object that operates in the context of Connection con:
cur = con.cursor()

# Execute the SELECT statement:
cur.execute("select * from languages")

# Retrieve all rows as a sequence and print that sequence:
print cur.fetchall()

http://www.firebirdsql.org/file/documentation/drivers_documentation/python/fdb/getting-started.html

current location of databse is on /tmp, I'm using fdb downloaded from here: https://pypi.python.org/pypi/fdb/ and installed with pip also I used in pydev properties->pydev pythonpath and added the folder of fdb (it seems normal so far with no errors),my username is fernando and password is root, so when I finally run I get this error message:

Traceback (most recent call last):
  File "/home/elfstone/Documents/workspace/NuevosPython/fire.py", line 3, in <module>
    con = fdb.connect(dsn="/tmp/test.fdb", user="fernando", password="root")
  File "/home/elfstone/Downloads/fdb-1.4/fdb/fbcore.py", line 693, in connect
    "Error while connecting to database:")
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)

How can this be fixed? help and thanks.

Checked with Ubuntu 12.04.4 and fdb 1.4.1 all suggested statement types are working:

fdb.connect(dsn="/var/lib/firebird/2.5/data/test.fdb", user="fernando", password="root")
fdb.connect(host="localhost", database="/var/lib/firebird/2.5/data/test.fdb", user="fernando", password="root")
fdb.connect(dsn="localhost:/var/lib/firebird/2.5/data/test.fdb", user="fernando", password="root")

This is untested, but my suspicion is:

The error says you cannot connect to 'localhost', which is a network name for the computer you are using. However, you ask Firebird to connect to '/tmp/test.fbd', which is a file system location. Basically, firebird thinks that the you want to connect to the file '/tmp/test.fbd' as if it were a server.

Try:

con = fdb.connect(host="localhost", database="/tmp/test.fdb", user="fernando", password="root")

or

con = fdb.connect(dsn="localhost:/tmp/test.fdb", user="fernando", password="root")

Assuming of course, that /tmp/fest.fbd is actually on your localhost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM