📜 ⬆️ ⬇️

When a column name in the result of a query in SQLite3 is not defined

First, the result, and then debriefing. Suppose you have created a query of type select [document].[id], [document].[name] from [SomeDocuments][document] , but instead of the expected
  id = 1 name = d1 
are getting
  document = 1 document = d1 

And now, if we are interested, we will understand.

In general, everything is described in the documentation for the functions sqlite3_column_name and sqlite3_column_name16
If you do not use “as”, then no one guarantees that you will receive the corresponding column name.
If you are a clause, it is a clause. If there is no sign of it, it’s not.

How to get the result in the command line (version 3.7.16.2 was on hand, in the product 3.8.1 )
 .mode line create table [Documents]([id] integer, [name] text); insert into [Documents]([id], [name]) values (1, "d1"); select [d].[id], [d].[name] from [Documents][d]; 
  id = 1 name = d1 
Works as expected. Continue
 create view [DocView1] as select * from [Documents]; select [d].[id], [d].[name] from [DocView1][d]; 
And now
  d = 1 d = d1 
The result given psyX in the comments (on my versions repeated)
 select d.id, d.name from DocView1 d; d.id = 1 d.name = d1 
It is natural to expect the same behavior in recent versions of the functions sqlite3_column_name.

Lyrical digression
In previous work, I wrote another ORM bike that works with sqlite3 and c mysql. Of course, I ran into this problem, but did not give it attention, because aliases were used for all the columns there. And now I decided that it was worth the article. Aliases in that library were needed almost immediately, due to the lack of possibility in sqlite3 to understand from which table this result and, accordingly, to which object to save it, in the case of queries with joines. In general, the uncertainty in linking result columns and real tables was a bit disturbing, but I didn’t dig deeper, it still worked.
And here at the new work, where quite a lot of hardcodes in terms of working with the database, such a feature sqlite3 has surfaced. Perhaps this is described in some books and a long and well-known fact, but for my colleagues and once again for me this was a rather unexpected discovery.
Supplement for Qt programmers
Since I am now programming with Qt, I want to share a bit of experience here. Qt is so “kyut” that if there is a period in the column name of the query result, then Qt, without any warning or ability to configure it, removes everything up to and including the point. Those. if you have a select [document].[id] as [document.id] ... query of type select [document].[id] as [document.id] ... , then at the output in QSqlRecord you will have fields without “ document. ", I.e. just id .


Thank you for your attention, have a nice day.

')

Source: https://habr.com/ru/post/206010/


All Articles