how to do a select * from table in jython and get the result for each row into a list or string.
i know how to do for select counmn_name1 ,column_name2 from table1 but not able to figure out for select *
Please suggest .thanks
If you use JDBC then you can use JDBC ResultSetMetaData interface:
rs = c.executeQuery("SELECT * FROM a_tmp_table")
while (rs.next()):
rsmd = rs.getMetaData()
print('columnCnt: %d' % (rsmd.getColumnCount()))
for i in range(rsmd.getColumnCount()):
print(rs.getString(i + 1))
If you use zxJDBC (comes with Jython) then you can follow the cross-implementation DB-API protocol to execute queries and retrieve results.
Related
I've seen may answers to the same kind of question but I still doubt.
UPDATE in SQL should be something like :
UPDATE *Table*
SET *choose value*
WHERE *what do we change*
I would like to know if there is possibilites to use a select instead of TABLE (an so instead of WHERE)
Like
UPDATE *Select conditions and rows*
SET *What do we change (the where is implicit)
I know UPDATE/SET/WHERE works well, but I'm exploring other possibilites :)
Thanks,
Nicolas
EXAMPLE :
Have to do :
update produits
set `NO_FOURNISSEUR` = "30"
where `NO_FOURNISSEUR` = "3"
would like to try something like :
update select * from produits where produits.`no_fournisseur`= "30"
set `NO_FOURNISSEUR`= "3"
MariaDB has a with expression, like so:
https://mariadb.com/kb/en/library/with/
so you would have:
WITH t AS (
select *
from produits
where produits.no_fournisseur= '30')
UPDATE t SET t.no_fournisseur = '3';
Yes. The ANSI SQL standard way to do this is using a Common Table Expression:
with dt as
(
select *
from produits
where produits.no_fournisseur= '30'
)
update dt set NO_FOURNISSEUR = '3'
This standard syntax supports joins and other query constructs in the SELECT part, and gives you a simple way to examine the rows before applying the update.
I'm generating SQL programmatically so that, based on certain parameters, the query that needs to be executed could be different (i.e., tables used, unions, etc). How can I insert a string like this: "select * from table", into a %%sql block? I know that using :variable inserts variable into the %%sql block, but it does so as a string, rather than sql code.
The answer was staring me in the face:
query="""
select
*
from
sometable
"""
%sql $query
If you want to templatize your queries, you can use string.Template:
from string import Template
template = Template("""
SELECT *
FROM my_data
LIMIT $limit
""")
limit_one = template.substitute(limit=1)
limit_two = template.substitute(limit=2)
%sql $limit_one
Source: JupySQL documentation.
Important: If you use this approach, ensure you trust/sanitize the input!
Ive tried many queries to find... just one word and I can´t even make that.
Its a DB2 database Im using com.ibm.db2.jcc.DB2Driver
This brings me info:
select *
from JL_ENR
where id_ws = '002'
and dc_dy_bsn = '2014-08-25'
and ai_trn = 2331
the JL_TPE column is the CLOB column where I want to find two strings in that search result ( and dc_dy_bsn = '2014-08-25'
and ai_trn = 2331 ).
So first I tried with one:
select
dbms_lob.substr(clob_column,dbms_lob_instr(JL_TPE,'CEMENTO'),1)
from
JL_ENR
where
dbms_lob.instr(JL_TPE,'CEMENTO')>0;
didnt work
SELECT * FROM JL_ENR WHERE dbms_lob.instr(JL_TPE,'CEMENTO')>0
and ai_trn = 2331
and dc_dy_bsn = '2014-08-25'
didnt work
Select *
From JL_ENR
Where NOT
DBMS_LOB.INSTR(JL_TPE, 'CEMENTO', 1, 1) = 0;
didn´t work
Could someone explain me how to find two strings please?
Or a tutorial link where it is explained how to make it work...
Thanks.
Can you provide some sample data and the version you are using? Your example should work (tested on v10.5.0.1):
db2 "create table test ( x int, y clob(1M) )"
db2 "insert into test (x,y) values (1,cast('The string to find is CEMENTO, how do we do that?')"
db2 "insert into test (x,y) values (2,cast('The string to find is CEMENT, how do we do that?' as clob))"
db2 "select x, DBMS_LOB.INSTR(y, 'CEMENTO', 1) from test where DBMS_LOB.INSTR(y, 'CEMENTO', 1) > 0"
X 2
----------- -----------
1 23
1 record(s) selected.
I had to search for a specific value in the where clause. I used TEXTBLOB LIKE '%Search value%' and it worked! This was for db2 in a CLOB(536870912) column.
Could anyone tell me how to use an sql 'AS' statement in a CDBCriteria query?
Whatever i have tired just brings back the columns names of my table in an array and no sign of my sql alias.
I think i may have to add it into my model class but im not sure where to declare it after several attempts to add it in.
Here is mine, as you can see 'distance' is my alias:
$criteria= new CDbCriteria;
$criteria->select='*, 3963 * acos(cos(radians('.$distanceString['latitude'].')) * cos(radians(latitude)) * cos(radians('.$distanceString['longitude'].') - radians(longitude)) + sin(radians('.$distanceString['latitude'].')) * sin(radians(latitude))) AS distance';
$criteria->condition=$sqlCondition;
$criteria->params=$sqlVariables;
$criteria->order='distance';
Thanks for any tips or help in advance! :)
Do you use this with a CActiveDataProvider for an active record class? If so, try defining distance in that class.
Use CDbCommand instead
$rawData=Yii::app()->db->createCommand()
->select('*, 3963 * acos(cos(radians('.$distanceString['latitude'].')) * cos(radians(latitude)) * cos(radians('.$distanceString['longitude'].') - radians(longitude)) + sin(radians('.$distanceString['latitude'].')) * sin(radians(latitude))) AS distance')
->from('TableName')
->where($sqlCondition)
->order('distance')->queryAll(true,$sqlVariables);
eg. together with CArrayDataProvider
$dataProvider=new CArrayDataProvider($rawData);
// $dataProvider->getData() will return a list of arrays.
I am running the following query, but no rows are returned even though a record exists that should match the query.
SELECT
*
FROM
tblsignup
WHERE
usr_email='amir#gmail.com'
AND
(status=1 or status=2)
You should try by simplifying the query (yeah...even if it's so simple)
try this
Select * from tblsignup
then
Select * from tblsignup where
usr_email = 'amir#gmail.com'
then
Select * from tblsignup where
usr_email='amir#gmail.com' and
status > 0
//I know you won't use > 0 at the end, but we want to eliminate the most cause of error we simplify by > 0 only to be easier to read
Tell us from where you start getting 0 line, this could lead us to the problem, I know I already had a problem like that with a field named "date", because date is already used by MySQL, funny MySQL still let me use that fieldname tho.
Try this:
select * from `tblsignup` where `usr_email`='amir#gmail.com' and (`status`=1 or `status`=2)
I have a feeling "status" might be reserved for something special. It might be worth a shot changing it to `status`.
Try wrapping brackets around the status column name:
SELECT *
FROM tblsignup
WHERE usr_email = 'amir#gmail.com'
AND ([status] = 1
OR [status] = 2);
EDIT
After reading your comment, why not use:
SELECT *
FROM tblsignup
WHERE usr_email = 'amir#gmail.com'
AND [status] > 0;
May it be that your column or table has case sensitive collation and the address is typed different ('Amir...')? As your query is correct SQL. You can find that with:
EXEC sp_help DatabaseName