android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Whenever you are dealing with Cursors, ALWAYS check for null and check for moveToFirst() without fail. if( cursor != null && cursor.moveToFirst() ){ num = cursor.getString(cursor.getColumnIndex(“ContactNumber”)); cursor.close(); } Place logs appropriately to see whether it is returning null or an empty cursor. According to that check your query. Update Put both the checks in a … Read more

Using a cursor with dynamic SQL in a stored procedure

A cursor will only accept a select statement, so if the SQL really needs to be dynamic make the declare cursor part of the statement you are executing. For the below to work your server will have to be using global cursors. Declare @UserID varchar(100) declare @sqlstatement nvarchar(4000) –move declare cursor into sql to be … Read more

Viewing an Android database cursor

Android has provided a specific class just for debugging Cursors. It is called DatabaseUtils. Call the method DatabaseUtils.dumpCursorToString(cursor) to view the contents of your cursor. This helper loops through and prints out the content of the Cursor for you, and returns the cursor to its original position so that it doesn’t mess up your iterating … Read more

Why is it considered bad practice to use cursors in SQL Server?

Because cursors take up memory and create locks. What you are really doing is attempting to force set-based technology into non-set based functionality. And, in all fairness, I should point out that cursors do have a use, but they are frowned upon because many folks who are not used to using set-based solutions use cursors … Read more

SQL Server: how to add new identity column and populate column with ids?

Just do it like this: ALTER TABLE dbo.YourTable ADD ID INT IDENTITY(1,1) and the column will be created and automatically populated with the integer values (as Aaron Bertrand points out in his comment – you don’t have any control over which row gets what value – SQL Server handles that on its own and you … Read more

Looping Over Result Sets in MySQL

Something like this should do the trick (However, read after the snippet for more info) CREATE PROCEDURE GetFilteredData() BEGIN DECLARE bDone INT; DECLARE var1 CHAR(16); — or approriate type DECLARE var2 INT; DECLARE var3 VARCHAR(50); DECLARE curs CURSOR FOR SELECT something FROM somewhere WHERE some stuff; DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = … Read more

Can I loop through a table variable in T-SQL?

Add an identity to your table variable, and do an easy loop from 1 to the @@ROWCOUNT of the INSERT-SELECT. Try this: DECLARE @RowsToProcess int DECLARE @CurrentRow int DECLARE @SelectCol1 int DECLARE @table1 TABLE (RowID int not null primary key identity(1,1), col1 int ) INSERT into @table1 (col1) SELECT col1 FROM table2 SET @RowsToProcess=@@ROWCOUNT SET … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)