Using Sqlite3 in objective C How to Retrieve the number of rows in a result set - objective-c

after a long search didn't succeed to get the required and easily understandable answer i am putting here a question…..
so please help me out on it….
I just want know that how i can get the number of rows in a result set of a query in objective C using sqlite3.
as we just use the function of SQL Server in PHP.
$Query = "SELECT XXXX FROM XXX";
$rstRow = Sql_Query($Query);
if (sqlsrv_num_rows($rstRow) > 0)
{
/* do something */
}
what is alternate to this in sqlite3.

Sqlite does not provide any function to get the number of rows return by a query. You can use SELECT COUNT(*) FROM table_name to get total number rows.
Reference: link

Related

Big Query Concat String in FROM Clause

I have multiple tables like as follows in BigQuery:
PROJECT_NAME.DATA_SET_NAME.TABLENAME0
PROJECT_NAME.DATA_SET_NAME.TABLENAME1
PROJECT_NAME.DATA_SET_NAME.TABLENAME2
PROJECT_NAME.DATA_SET_NAME.TABLENAME3
PROJECT_NAME.DATA_SET_NAME.TABLENAME4
...
I want to empty some of these tables via a loop but don't know how to call the CONCAT in FROM clause:
DECLARE count INT64 DEFAULT 0;
WHILE count < 1000 Do
DELETE FROM CONCAT('PROJECT_NAME.DATA_SET_NAME.TABLENAME' , count ) WHERE TRUE;
SET count = count + 1;
END WHILE
But it's not working, it says I cannot use CONCAT in FROM part.
Anyone knows how should I do it?
Thanks
This is not currently possible in BigQuery, unless you script it outside BigQuery.
There's an open feature request, that you should subscribe to - to indicate interest and follow any new developments:
https://issuetracker.google.com/issues/142531516

Hibernate createSQLQuery and Toad SQL query return different results - parameter problems?

I'm a newbie at Hibernate so excuse me if some of this is glaringly obvious but it's been a very, very long day. I am trying to create and execute a simple query in Hibernate and Toad/Oracle.
The Toad/Oracle sql reads:
select
count(*)
from
fpps_owner.fee_claim_payment_lines l,
fpps_owner.fee_claim_payments p
where
l.fee_claim_payment_id = p.fee_claim_payment_id and
p.claim_index = 87167895
The above returns 10 records, which is correct
The following Java code returns 0 records, which is NOT correct
String sLinesAvailable =
"select count(*) from " +
"fpps_owner.fee_claim_payment_lines l, fpps_owner.fee_claim_payments p " +
"where " +
"l.fee_claim_payment_id = p.fee_claim_payment_id and p.claim_index = :id";
Query qLinesAvailable = em.createNativeQuery(sLinesAvailable);
qLinesAvailable.setParameter("id", "87167895"); // fails
qLinesAvailable.setParameter("id", 87167895); // fails
List<Object> out = (List<Object>) qLinesAvailable.getResultList();
BigDecimal x = (BigDecimal) out.get(0);
Returns 0 records. Using .getSingleResult() also returns 0 records.
What am I missing here?
Any help would be GREATLY appreciated!
If you are not seeing any malformed query errors, it seems like the parameter is not binding correctly.
To debug, I'd print out that SQL statement the line after you set the parameter. This is the only way you can see the SQL after the parameter is set in order to compare it with Toad.
What does your binding file look like? Maybe you have a different name in there for the ID, so it's not able to find it based on the name. Trying binding with the parameter's order value, just as a test.
This might give some ideas: http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/
Best of luck! We've all been there :)
What happens when you try:
(Number) query.getSingleResult();
Your query isn't returning a list, but rather just a count.
Good luck.

Yii Framework: How to get the num_rows?

As the official documentation does not say how to do a simply "num_rows" with their system, i need some help here: How to get the amount of rows in the result set ?
Assuming:
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
This will work for insert, update and delete:
$rowCount=$command->execute();
execute(): performs a non-query SQL statement, such as INSERT, UPDATE and DELETE. If successful, it returns the number of rows that are affected by the execution.
For select, you could do the following:
$dataReader=$command->query();
This generates the CDbDataReader instance and CDbDataReader provides a rowCount property
http://www.yiiframework.com/doc/api/1.1/CDbDataReader#rowCount-detail
$rowCount = $dataReader->rowCount;
About rowCount => Returns the number of rows in the result set. Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
ActiveRecord has count method which can be used.
$cntCriteria = new CDbCriteria();
$cntCriteria->condition = "categoryId = :categoryId";
$cntCriteria->params[':categoryId'] = $categoryRow->categoryId;
$articleCount = Article::model()->count($cntCriteria);
There is one more way to do this. When we execute a sql query it will return the result as array only. So we can able get the count of the rows using count() function like below.
$output=User::model()->findAllBySql("select * from user");//User is a model belongs to the user table
$count_val=count($output);//$count_val has the value of number of rows in the output.

MS Access count(*) query in Ruby script not returning with a number

I have a script I want to determine the number of records in a Microsoft Access database table and assign that to a variable. I have the code below (snippet) which runs without error but the result is not a number but instead #. Is this a number in some other format? Can I convert it into a number? When I run the SQL in Access, I get 43 (for example); that's what I was expecting.
...
connection = WIN32OLE.new('ADODB.Connection')
connection.Open('Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Database.mdb')
recordset = WIN32OLE.new('ADODB.Recordset')
number_of_sites = connection.Execute("SELECT count(*)FROM Test;")
puts number_of_sites
...
Thanks in advance! :)
Andrew is close - connection.Execute returns a recordset, not a scalar. To get the count, you want 'recordset.Fields(0).value).
connection.Execute doesn't return data, it just executes the sql. You might use it to do an INSERT. To fetch data, you need a recordset.
recordset = WIN32OLE.new('ADODB.Recordset')
recordset.Open("SELECT count(*) FROM Test", connection)
count = recordset[0]
I'm not positive about the index on recordset- it might be recordset[1]?

SELECT MAX query returns only 1 variable + codeigniter

I use codeigniter and have an issue about SELECT MAX ... I couldnot find any solution at google search...
it looks like it returns only id :/ it's giving error for other columns of table :/
Appreciate helps, thanks!
Model:
function get_default()
{
$this->db->select_max('id');
$query = $this->db->getwhere('gallery', array('cat' => "1"));
if($query->num_rows() > 0) {
return $query->row_array(); //return the row as an associative array
}
}
Controller:
$default_img = $this->blabla_model->get_default();
$data['default_id'] = $default_img['id']; // it returns this
$data['default_name'] = $default_img['gname']; // it gives error for gname although it is at table
To achieve your goal, your desire SQL can look something like:
SELECT *
FROM gallery
WHERE cat = '1'
ORDER BY id
LIMIT 1
And to utilise CodeIgniter database class:
$this->db->select('*');
$this->db->where('cat', '1');
$this->db->order_by('id', 'DESC');
$this->db->limit(1);
$query = $this->db->get('gallery');
That is correct: select_max returns only the value, and no other column. From the specs:
$this->db->select_max('age');
$query = $this->db->get('members');
// Produces: SELECT MAX(age) as age FROM members
You may want to read the value first, and run another query.
For an id, you can also use $id = $this->db->insert_id();
See also: http://www.hostfree.com/user_guide/database/active_record.html#select
CodeIgniter will select * if nothing else is selected. By setting select_max() you are populating the select property and therefore saying you ONLY want that value.
To solve this, just combine select_max() and select():
$this->db->select('somefield, another_field');
$this->db->select_max('age');
or even:
$this->db->select('sometable.*', FALSE);
$this->db->select_max('age');
Should do the trick.
It should be noted that you may of course also utilize your own "custom" sql statements in CodeIgniter, you're not limited to the active record sql functions you've outlined thus far. Another active record function that CodeIgniter provides is $this->db->query(); Which allows you to submit your own SQL queries (including variables) like so:
function foo_bar()
{
$cat = 1;
$limit = 1;
$sql = "
SELECT *
FROM gallery
WHERE cat = $cat
ORDER BY id
LIMIT $limit
";
$data['query'] = $this->db->query($sql);
return $data['query'];
}
Recently I have been utilizing this quite a bit as I've been doing some queries that are difficult (if not annoying or impossible) to pull off with CI's explicit active record functions.
I realize you may know this already, just thought it would help to include for posterity.
2 helpful links are:
http://codeigniter.com/user_guide/database/results.html
http://codeigniter.com/user_guide/database/examples.html