Why do sqlite columns have different names for views and tables? - c++

For this sample database:
create table atable (
textfield varchar(256)
);
create view aview as select T.textfield from atable T;
and these selects:
select T.textfield from atable T;
select T.textfield from aview T;
calling sqlite's sqlite3_column_name(...) yields different results for the select on view and on table. In case of table the the alias is not included, in case of view it is.
Here are the results:
table query - sqlite3_column_name (db, 0) -> "textfield"
view query - sqlite3_column_name (db, 0) -> "T.textfield"
Why are these different?

Surprisingly, SQLITE provides no guarantees to the column name returned by sqlite_3_column_name unless you specifically provide the name using AS keyword.
From sqlite3_column_name function documentation:
The name of a result column is the value of the "AS" clause for that
column, if there is an AS clause. If there is no AS clause then the
name of the column is unspecified and may change from one release of
SQLite to the next.
Therefore you have to manually specify the column names if you want to depend on the value returned. Change your queries to :
select T.textfield as textfield from atable T;
select T.textfield as textfield from aview T;
and in both cases sqlite3_column_name will return textfield.

Related

Oracle APEX - set select list selected value based on page item

I have a select list on a page and its source set to a sql query:
SELECT t.name d,t.id r
FROM Table1 t
WHERE t.status = 'New'
AND( t.id = TO_NUMBER(:P3_MY_ID)
OR :P3_MY_ID IS NULL)
ORDER BY 1
And I see mt P3_MY_ID set in session but the value in my select list is not selected. Can anyone help?
The value of P3_MY_ID is getting set in pre-render before regions
List of Values properties group contains the Cascading LOV Parent Item(s) - put P3_MY_ID in there.
Also, make sure that query you wrote actually returns something (using the P3_MY_ID value you use in Apex) when executed in SQL*Plus, SQL Developer, TOAD or whichever tool you use.
to set the value of a select list you need to create a dynamic action set the event to when change>selection type page item> your item P3_MY_ID.
set the true action to set value> set type sql statment> your query> affected element> type item> your select list

Select DISTINCT Query Corrupts result in record set

I've used recordset to populate combobox. When I write normal select query the output comes out to be correct but when I write select query with DISTINCT the combobox is populated with weird strings. Could anyone tell me as to what I'm doing wrong.
allSqlUsername.Format(_T("SELECT PatientName from PatientDetails;"));
recsetname.Open(CRecordset::forwardOnly, allSqlUsername, CRecordset::readOnly);
while (!recsetname.IsEOF())
{
recsetname.GetFieldValue(L"PatientName", patientNameC);
m_autocompletename.AddString(patientNameC);
recsetname.MoveNext();
}
New select query - allSqlUsername.Format(_T("SELECT DISTINCT PatientName from PatientDetails;"));

How to get ID from LOV?

I'm learning APEX 5
I have a control named X_CONTROL, where I want to populate his content with an SQL query.
To do that, I need the ID primary key from a table, which should be the ID of the row selected on a Select List control named MY_LIST_CONTROL.
MY_LIST_CONTROL has a list of values taken from a column of the table "MyTable", which is not the ID primary key.
I tried to populate X_CONTROL with this SQL
Select ID from MyTable where ColumnName=:MY_LIST_CONTROL
It doesn't work, and should not work because ColumnName is not "unique", like ID is.
So, the question is, how do I recover, with SQL, the ID of the selected row which correspond to the selected value in MY_LIST_CONTROL.
It should be SQL, because APEX 5 demands an SQL query to populate the X_CONTROL.
I have set up a simple example here on apex.oracle.com:
Whenever a Department is selected (item P32_DEPTNO), its Location is copied into the second item (P32_LOC).
This is done by a dynamic action on P32_DEPTNO defined as follows:
Event: Change
Selection Type: Item(s)
Item(s): P32_DEPTNO
TRUE Action:
Action: Set Value
Set Type: SQL Statement
SQL Statement:
select loc
from dept
where deptno = :P32_DEPTNO
Items to Submit: P32_DEPTNO

query builder select the id from leftJoin

I have a select field that fetch from an entity
and I would like to customize completely my select by choosing the table the id is picked from
(here I would like to select t.id instead of tl.id as the select value)
return $er->createQueryBuilder('tl')
->addSelect('l')
->addSelect('t')
->leftJoin('tl.lang', 'l')
->leftJoin('tl.type', 't')
->where('l.isDefault = 1')
->orderBy('tl.name', 'ASC');
Due to my tables, I can't simply fetch the table t, I have to use tl
Your query is not according to the syntax defined in Doctrine 2 QueryBuilder: http://www.doctrine-project.org/docs/orm/2.0/en/reference/query-builder.html
Your query might work in Doctrine 1.2 but in Doctrine 2 you should build your query according to the syntax defined in the link I posted above.
For example ->addSelect('l') is not being used in Doctrine 2 anymore. It has become ->add('select', 'l').
You don't have to set different alias for your column. It'll be hydrated as column of the related entity.

ActiveRecord and GetHashCode fails if int is nullable

There is a problem when the int is nullable on GetHashCode
At the point of GetHashCode on ActiveRecord.tt there is a need for a nullable check.
Something like this.
<#
if(tbl.PK.SysType=="int" && !tbl.PK.Nullable ){
#>
public override int GetHashCode() {
return this.<#=tbl.PK.CleanName #>;
}
<# }#>
(update)
This value can be come null on the Views.
I have include the views using this code I found on inet.
const string TABLE_SQL=#"SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
union
select Table_catalog, table_schema, table_name, 'View' table_type
from information_schema.views";
After that, this error appears.
SubSonic requires a primary key that is not null. You need to modify your view so it returns an Id column that is a non null Integer or Guid.
EDIT: You can add a non null primary key that'll work for SubSonic by changing your view as follows:
CREATE VIEW MyView AS
SELECT NewID() AS Id, *
FROM MyTable
The one drawback is that you won't get a consistent Id. If that's a problem you could replace NewID() with a value computed from the column but the drawback of that is the performance will be slower.