I am working on a C++ Qt program where I include a mysql database.
I want to do some sql queries and show the output on a tableview. Because I also want to filter the results I wrote this kind of code snippet:
query->prepare("SELECT * FROM contacts WHERE contactsName LIKE '%:name%';");
query->bindValue(":name", mUi->searchContactsLine->text());
query->exec();
mModel->setQuery(*query);
mUi->tableContacts->setModel(mModel);
... where
searchContactsLine
is the inputlinebox to search for a string,
mModel
is my QSqlQueryModel and
tableContacts
is my TableView in qt.
As you can see I want to use pattern matching with the dollar sign. But if I run the program I got no row back.. no matter what I am searching for.
In the past I had problems with the inverted commas ('...') while using the bindValue function, but if I delete it from the sql command I get an sql syntax error.
So what do I wrong? Maybe you have an idea of that.
Related
I have a QT application that connects to a sqlite database. To build the queries I use the prepare and bindValue methods. I am able to get this working in an INSERT query. However these methods no longer work when the placeholder is in a WHERE clause.
QSqlQuery query(this->database);
query.prepare("DELETE FROM `Shops` WHERE `id`=:id;");
query.bindValue(":id", this->id);
query.exec();
qDebug() << query.executedQuery();
When I execute this code, the following line will be printed into the debug console:
"DELETE FROM `Shops` WHERE `id`=:id;"
I tried the same with an UPDATE ... WHERE ...=:id query and it didn't worked. In an INSERT ... INTO ... VALUES (:name) it worked however.
I already found out, that it is not possible to use placeholders for columns or table names in qt. But I could not found anything about restrictions with WHERE clauses. Does anyone has an idea?
Ps: I know, that I could just concatenate the string. But I am curious, why it does not work.
I am trying to refresh a report with dynamic action. And get the following errors:
{'dialogue': {'uv': true, 'line': [{'V': "failure of Widget}]}}
ORA-20876: stop the engine of the APEX.
classic_report"}]}}
I think its an issue with string which can't take and ST.ID IN (:P11_ROW_PK) in sql query.
Please suggest a workaround for the same.
This question requires the context you've provided in
https://stackoverflow.com/a/63627447/527513
If P11_ROW_PK is a delimited list of IDs, then you must structure your query accordingly, not expect the IN statement to deconstruct a bind variable containing a string.
Try this instead
select * from your_table
where st.id in (select column_value from apex_string.split(:P11_ROW_PK))
where REGEXP_LIKE(CUSTOMER_ID, '^('|| REPLACE(:P4_SEARCH,',','|') ||')$')
Above code will act same as APEX_STRING only if you are using lower version Apex
I'm seeing very bizarre behavior with the REGEXP_MATCH function in google big query. The function appears to work perfectly fine for public data but is not working on my dataset. I have a dataset imported from the csv with the first two lines (first is header row which all becomes the schema where everything is a string), there's a lot more but the following is the only relevant data for this case.
"id","common_name","botanical_name","low_hardiness_zone","high_hardiness_zone","type","exposure_min","exposure_max","moisture_min","moisture_max"
"plant1","Abelia","Abelia zanderi 'Conti (Confetti)'","5b","9a","Shrub","Partial Sun","Full Sun","Dry","Dry"
When I run the query:
SELECT * FROM [PlantLink_Plant_Types.plant_data_set]
WHERE REGEXP_MATCH('common_name',r'.*')
I get every result.
However, when I run the query:
SELECT * FROM [PlantLink_Plant_Types.plant_data_set]
WHERE REGEXP_MATCH('common_name',r'A.*')
I get no results, which is really weird because the plant common name Abelia starts with an A.
Now my regex magic is not that strong, but I am pretty sure the pattern is not at fault. Additionally I've run the public dataset test queries with REGEXP_MATCH and they run correctly. Does anyone have any clue why REGEXP_MATCH would not always function as advertised?
Note:
REGEXP_MATCH('common_name',r'.*') matches the string 'common_name'
while
REGEXP_MATCH(common_name,r'.*') matches a field in your table that is called common_name
the 1st one is always true and therefore you get all results.
I guess you wanted to refer the content of the field, so you need to use the second one.
REGEXP_MATCH(common_name,r'A.*') should return all records that field common_name contains "A".
hope this helps.
Issue is the string 'common_name' does not start with 'A'.
Check this:
REGEXP_MATCH('common_name',r'.*'): All results.
REGEXP_MATCH('common_name',r'A.*'): No results.
REGEXP_MATCH('common_name',r'c.*'): All results.
REGEXP_MATCH(common_name,r'A.*'): All results that somewhere have an 'A'.
:)
I'm trying to compare 2 similar columns from 2 tables in SQLite. I wrote this which doesn't throw an error but also doesn't return anything and I know it should.
SELECT t1.* from t1, t2 Where t1.col1 Like '%'||t2.col1||'%';
I'm looking for when t2.col1 is embedded inside of t1.col1. Both columns are of type TEXT.
Note: I'm using the C-Interface in C++ with Visual Studio 2010.
Ideas?
Edit:
I've played around with pulling a value out of t2.col1 that matches something in t1.col1 and writing something like this,
SELECT t1.* from t1, t2 Where t1.col1 Like '%ValueInT1%';
which works and returns something.
Is there a bug with SQLite when concatenating the '%' character in a like statement or is there a different syntax I should be using? I'm at a loss for why this isn't working.
I've also seen an SQL function called Locate which people use in different Databases to do this kind of check. Does SQLite have a Locate function?
EDIT 2:
I've run the first SQL statement in SQLite Administrator with some of my data and it DOES find it. Could there be a simpler problem? There are '_' in the data could that be causing a problem with the like?
(V.V) The answer was that I was joining to the wrong column in the real table. 2 columns had very similar names and I was checking the wrong one.
Thanks for the help.
This may seem a odd question as I have a solution, I just dont understand why and that limits me.
I am copying data from various sources into SQL and am using a ADO connection in C++ Builder XE2.
When the data is from MSAccess or MSExcel the code is similar to the following:
//SetupADO..
ADOConn->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/temp/testdb.mdb";
//Then open it..
ADOConn->Connected = true;
//Build SQL
UnicodeString sSQL = "SELECT * INTO [ODBC;DSN=PostgreSQL30;DATABASE=admin_db;SERVER=192.168.1.10;PORT=5432;UID=user1;PWD=pass1;SSLmode=disable;ReadOnly=0;Protocol=7.4;].[table1] FROM [accesstb]";
//And finally I use the EXCEUTE() function of the ADO Connection
ADOConn->Execute(sSQL, iRA, TExecuteOptions() << TExecuteOption::eoExecuteNoRecords);
This works fine for Excel too but not for CSV files. I'm using the same driver must can only get it working by changing the syntax around.
//SetupADO..
ADOConn->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\temp;Extended Properties=\"Text;HDR=Yes;\";Persist Security Info=False";
//Then open it..
ADOConn->Connected = true;
//Build SQL with the IN keyword and start internal ODBC connection with 2 single quotes
UnicodeString sSQL = "SELECT * INTO [table1] IN '' [ODBC;DSN=PostgreSQL30;DATABASE=admin_db;SERVER=192.168.1.10;PORT=5432;UID=user1;PWD=pass1;SSLmode=disable;ReadOnly=0;Protocol=7.4;] FROM [test.csv]";
//And finally EXCEUTE() again
ADOConn->Execute(sSQL, iRA, TExecuteOptions() << TExecuteOption::eoExecuteNoRecords);
When using the same SQL as the Access query the error "Query input must contain at least one table or query" would be returned.
Intrestingly, one escaped quote, i.e. \' fails when used in place of the 2 single ones. I have also tried writing to another Access database in case the problem was with PG but I had the same results.
Can someone tell me why the IN keywork is required and what the single quotes do?
Extended Properties=\"Text;HDR=Yes;\" specifies text as the datasource, so the connection string is different. IN '' tells the database to map table1 to the first column of the CSV file, since there is no relational model in CSV.
References
Importing CSV Data and saving it in database - CodeProject