Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-6239] Android: Database.ResultSet.next() returns undefined

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2013-06-21T07:02:10.000+0000
Affected Version/sRelease 1.7.5, Release 1.8.0
Fix Version/sRelease 3.1.0, 2013 Sprint 06 API, 2013 Sprint 06
ComponentsAndroid
Labelscore, exalture, module_database, qe-testadded
ReporterJon Alter
AssigneeSunila
Created2011-11-18T17:35:05.000+0000
Updated2013-06-21T07:02:10.000+0000

Description

The Docs says that Database.ResultSet.next() returns false if no more results are available. http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Database.ResultSet.next-method.html It is always undefined for android. On iOS it returns true and false. Step 1: run the code below Step 2: click the blue window Step 3: notice the return value output in the console is always undefined.
var win = Ti.UI.createWindow({
	backgroundColor:'blue'
});

win.addEventListener('open',function(e){
	Ti.API.info('WIN OPENED');
	var db = Ti.Database.open('mydb');
	db.execute('DROP TABLE IF EXISTS welcome');
	db.execute("CREATE TABLE IF NOT EXISTS welcome (title TEXT)");
	db.execute("INSERT INTO welcome (title) VALUES (?)",'one');
	db.execute("INSERT INTO welcome (title) VALUES (?)",'two');
	db.close();
});

win.addEventListener('click',function(e){
	Ti.API.info('CLICKED');
	var db = Ti.Database.open('mydb');
	var rows = db.execute("SELECT title FROM welcome");
	if(rows.getRowCount()){
		do{
			Ti.API.info("##### TITLE: "+rows.fieldByName('title'));
			var next = rows.next();
			Ti.API.info('## Next: '+next);
		}while( next );
	}
	
	// Workaround
	// while( rows.isValidRow() ){
		// Ti.API.info("##### TITLE: "+rows.fieldByName('title'));
		// rows.next();
	// }
	
	rows.close();
	db.close();
});

win.open();

Workaround

Use while( rows.isValidRow() ){rows.next()}

Comments

  1. Andrew Bates 2012-07-12

    I ran into this same issue in a situation where I didn't want to loop through everything in the resultset so a while loop wasnt' appropriate. The easiest pattern is to call rows.next() and then test if it's valid with rows.isValidRow() before accessing fields. It requires two method calls to get the same effect on Android as calling if(rows.next()) on iOS but it's an easy workaround.
  2. Sunila 2013-03-16

    Changed method signature to return boolean and made code changes https://github.com/appcelerator/titanium_mobile/pull/3976
  3. Paras Mishra 2013-04-10

    Database.ResultSet.next() returns false in this case, works fine so closing it. Verified on: Device : Google Nexus 7, Android Version: 4.1 SDK: 3.1.0.v20130409124549 CLI version : 3.1.0-cr OS : MAC OSX 10.7.5 Appcelerator Studio, build: 3.1.0.201304052347 XCode : 4.5.1
  4. Dhirendra Jha 2013-06-21

    Reopening to update labels
  5. Dhirendra Jha 2013-06-21

    Tested with: SDK:3.1.2.v20130619101604 Appcelerator Studio: 3.1.1.201306131423 OS: OSX 10.7.5 Device:iPhone simulator(v 6.0), Google nexus(v 4.0.3) Xcode: 4.6

JSON Source