Android Apps

Convert database cursor result to Json Array – Android App Development

April 12, 2013 by Sarath DR in Android Apps with 2 Comments
 Convert database cursor result to Json Array   Android App Development Convert database cursor result to Json Array   Android App Development

This is a great book to start learning Android java programming with little or no programming language knowledge. I would like to recommend this book for you if you are a beginner in the world of Android programming.

Get it Now
 

This might be use ful when you do some database applications with phonegap. Phonegap storage class could be some times slow and difficult handle. Then you can write a separate plugin to do the querying. If you use database raw queries you will get the results as cursor data structure. The below code converts that to json array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
function JSONArray getResults()
{
 
String myPath = DB_PATH + DB_NAME;// Set path to your database
 
 
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
 
String searchQuery = "SELECT  * FROM <table_name>";
Cursor cursor = myDataBase.rawQuery(searchQuery, null );
 
JSONArray resultSet 	= new JSONArray(); 
JSONObject returnObj 	= new JSONObject();
 
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
 
       	    int totalColumn = cursor.getColumnCount();
       	    JSONObject rowObject = new JSONObject();
 
       	    for( int i=0 ;  i< totalColumn ; i++ )
       	    {
       	    	if( cursor.getColumnName(i) != null ) 
       	    	{
 
       	    		try 
       	    		{
 
	       	    		if( cursor.getString(i) != null )
	           	    	{
	           	    		Log.d("TAG_NAME", cursor.getString(i) );
	           	    		rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
	           	    	}
	           	    	else
	           	    	{
	           	    		rowObject.put( cursor.getColumnName(i) ,  "" ); 
	           	    	}
       	    		}
       	    		catch( Exception e )
       	    		{
       	    			Log.d("TAG_NAME", e.getMessage()  );
       	    		}
       	    	}
 
       	    }
 
       	    resultSet.put(rowObject);
       	    cursor.moveToNext();
        }
 
		cursor.close(); 
		Log.d("TAG_NAME", resultSet.toString() );
		return resultSet; 
 
}

Tagged , , ,

Share this to

Related Posts

2 Comments

  1. QamarFeb 6, 2016 at 5:28 amReply

    Dear i need complete code please send me i am beginner in android please help

  2. nikhilFeb 25, 2016 at 11:46 amReply

    i am using this code but it returns the last row of table multiple times as getColumnCount(); help me to resolve out this problem.
    thank you.

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close