1

I'm trying to read the data from a Firebird database, created by Firebird version 2.5, by using the FDB module from Python. Unsuccessfully. I have a Python v3.7.3 on a Windows10 64 bits system.

I am able to connect to the database and read the names of its tables:

con_1 = fdb.connect(dsn='C:/Prova_Archi.eft', user='sysdba', password='masterkey')  
schema_1 = fdb.schema.Schema()  
schema_1.bind(con2)  
print (schema_1.tables[57].name)  ===>  TUtenti

I am also able to read, for a given table, the name of its columns and indices:

print ([i.name for i in con_1.schema.get_table(schema_1.tables[57].name).columns][0]) ===> ...  
print ([i.name for i in con_1.schema.get_table(schema_1.tables[n].name).indices][r]) ===> ...  

However, I am not able to extract the data!

When I try this:

cur_1 = con_1.cursor()  
cur_1.execute("select * from 'TUtenti'")  

I am given the following error:

===> DatabaseError: ("Error while preparing SQL statement:\n- SQLCODE: -104\n- Dynamic SQL Error\n- SQL error code = -104\n- Token unknown - line 1, column 15\n- 'TUtenti'", -104, 335544569)

Suggestions are highly welcome.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
GgF
  • 15
  • 4

1 Answers1

1

The problem is that single quotes delimit a string literal, not an object name (e.g. a table name). To quote an object name you need to use double quotes, so use:

cur_1.execute('select * from "TUtenti"')
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Little bit frustrating discovering to have spent hours trying to fix an issue which was so simple to fix ...... However that simple detail is not mentioned (at least any clearly) in all the documents I read. So thanks again. – GgF Nov 14 '20 at 08:29
  • @GgF Section [Identifiers](https://firebirdsql.org/file/documentation/html/en/refdocs/fblangref25/firebird-25-language-reference.html#fblangref25-structure-identifiers) in the Firebird 2.5 Language Reference, and specifically [Rules for Delimited Object Identifiers](https://firebirdsql.org/file/documentation/html/en/refdocs/fblangref25/firebird-25-language-reference.html#fblangref25-structure-identifiers-delim). BTW: Using double quotes for delimited (quoted) identifiers is standard SQL syntax, it is not specific to Firebird. – Mark Rotteveel Nov 14 '20 at 09:14
  • @GgF there is a common approach: take the thing that works and look how it was done. 1) Take any database IDE (IBExpert, FlameRobin, dbWeaver, etc), create the table with that strange a name, use the IDE tools to make filtered query, read the query source text; 2) take FBExport ( or was it FBExtract ? ) tool, read it sources, find the query generating function, borrow the correct way to do it – Arioch 'The Nov 14 '20 at 19:58
  • So, once you've got here. What do you do to read the data. I followed all the steps but when I run cur_1.execute('select * from "TUtenti"') , I just get the follwing . ¿What should I do to get the info? thanks – Gustavo Zárate Jun 03 '21 at 19:57
  • @GustavoZárate The cursor object allows you to fetch the data of the query. See [Python DBAPI](https://www.python.org/dev/peps/pep-0249/), and [FDB documentation](https://fdb.readthedocs.io/en/latest/), or [pyfirebirdsql documentation](https://pyfirebirdsql.readthedocs.io/en/latest/), or [firebird-driver documentation](https://firebird-driver.readthedocs.io/en/latest/index.html) – Mark Rotteveel Jun 04 '21 at 09:17