problem with read data from SQLite database

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

problem with read data from SQLite database

Postby Bruce.lntu » Thu Nov 06, 2008 1:46 pm

Hi all,

when I code a ListActivity to list 3 rows with reading data from the database, it showed me an error saying: with an unexpected result. The application can't run OK.

with the code as below:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package cn.com.child;
  2.  
  3.  
  4.  
  5. import android.content.ContentProvider;
  6.  
  7. import android.content.ContentValues;
  8.  
  9. import android.content.Context;
  10.  
  11. import android.database.Cursor;
  12.  
  13. import android.database.SQLException;
  14.  
  15. import android.database.sqlite.SQLiteDatabase;
  16.  
  17. import android.database.sqlite.SQLiteOpenHelper;
  18.  
  19. import android.net.Uri;
  20.  
  21. import android.util.Log;
  22.  
  23.  
  24.  
  25. public class DataProvider extends ContentProvider {
  26.  
  27.         private static final String DATABASE_NAME="bus.db";
  28.  
  29.         private static final int DATABASE_VERSION=2;
  30.  
  31.         private static final String STATION_TABLE_NAME="station";
  32.  
  33.        
  34.  
  35.         private static class DatabaseHelper extends SQLiteOpenHelper{                  
  36.  
  37.                  DatabaseHelper(Context context) {
  38.  
  39.                         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  40.  
  41.                         // TODO Auto-generated constructor stub
  42.  
  43.                 }      
  44.  
  45.                 @Override
  46.  
  47.                 public void onCreate(SQLiteDatabase db) {
  48.  
  49.                         // TODO Auto-generated method stub
  50.  
  51.                         db.execSQL("CREATE TABLE"+STATION_TABLE_NAME+"("+"s_id"+"TEXT,"+"s_name"+"TEXT"+");");
  52.  
  53.                         String sql_1="insert into"+STATION_TABLE_NAME+"(s_id,s_name)values('1','hcz');";
  54.  
  55.                         String sql_2="insert into"+STATION_TABLE_NAME+"(s_id,s_name)values('2','lsdx');";
  56.  
  57.                         String sql_3="insert into"+STATION_TABLE_NAME+"(s_id,s_name)values('3','xal');";
  58.  
  59.                         try {
  60.  
  61.                                 db.execSQL(sql_1);
  62.  
  63.                                 db.execSQL(sql_2);
  64.  
  65.                                 db.execSQL(sql_3);
  66.  
  67.                         } catch (SQLException e) {
  68.  
  69.                                 Log.e("ERROR",e.toString());
  70.  
  71.                                
  72.  
  73.                         }
  74.  
  75.                 }
  76.  
  77.  
  78.  
  79.                 @Override
  80.  
  81.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  82.  
  83.                         // TODO Auto-generated method stub                     
  84.  
  85.                 }              
  86.  
  87.         }
  88.  
  89.  
  90.  
  91.         private DatabaseHelper mOpenHelper;
  92.  
  93.  
  94.  
  95.         @Override
  96.  
  97.         public int delete(Uri uri, String selection, String[] selectionArgs) {
  98.  
  99.                 // TODO Auto-generated method stub
  100.  
  101.                 return 0;
  102.  
  103.         }
  104.  
  105.  
  106.  
  107.         @Override
  108.  
  109.         public String getType(Uri uri) {
  110.  
  111.                 // TODO Auto-generated method stub     
  112.  
  113.                 return null;
  114.  
  115.         }
  116.  
  117.  
  118.  
  119.         @Override
  120.  
  121.         public Uri insert(Uri uri, ContentValues values) {
  122.  
  123.                 // TODO Auto-generated method stub
  124.  
  125.                 return uri;
  126.  
  127.         }
  128.  
  129.  
  130.  
  131.         @Override
  132.  
  133.         public boolean onCreate() {
  134.  
  135.                 // TODO Auto-generated method stub
  136.  
  137.                 mOpenHelper = new DatabaseHelper(getContext());
  138.  
  139.         return true;
  140.  
  141.         }
  142.  
  143.  
  144.  
  145.         @Override
  146.  
  147.         public Cursor query(Uri uri, String[] projection, String selection,
  148.  
  149.                         String[] selectionArgs, String sortOrder) {            
  150.  
  151.                 // Get the database and run the query
  152.  
  153.                 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
  154.  
  155.                 Cursor c = db.query(STATION_TABLE_NAME,projection, null,null,null,null,null);
  156.  
  157.                 return c;
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.         @Override
  164.  
  165.         public int update(Uri uri, ContentValues values, String selection,
  166.  
  167.                         String[] selectionArgs) {
  168.  
  169.                 // TODO Auto-generated method stub
  170.  
  171.                 return 0;        
  172.  
  173.         }
  174.  
  175. }
  176.  
  177.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


and the data ListActivity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package cn.com.child;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import java.util.ArrayList;
  8.  
  9. import java.util.HashMap;
  10.  
  11. import java.util.Map;
  12.  
  13. import android.app.ListActivity;
  14.  
  15. import android.content.Intent;
  16.  
  17. import android.database.Cursor;
  18.  
  19. import android.net.Uri;
  20.  
  21. import android.os.Bundle;
  22.  
  23. import android.view.View;
  24.  
  25. import android.widget.ListView;
  26.  
  27. import android.widget.SimpleAdapter;
  28.  
  29.  
  30.  
  31. public class data extends ListActivity {
  32.  
  33.         public static final String AUTHORITY="cn.com.provider.child";
  34.  
  35.         public static final Uri CONTENT_URI=Uri.parse("content://"+AUTHORITY+"/station");
  36.  
  37.         private static final String [] PROJECTION=new String []{
  38.  
  39.                 "s_id","s_name"};
  40.  
  41.         @Override
  42.  
  43.         protected void onCreate(Bundle savedInstanceState) {
  44.  
  45.                 // TODO Auto-generated method stub
  46.  
  47.                 super.onCreate(savedInstanceState);
  48.  
  49.                
  50.  
  51. //Return the intent that started this activity.
  52.  
  53.                 Intent intent=getIntent();
  54.  
  55.                
  56.  
  57.                 if(intent.getData()==null)
  58.  
  59.                         intent.setData(CONTENT_URI);
  60.  
  61.                 Cursor cur=getContentResolver().query(getIntent().getData(),PROJECTION,null,null,null);
  62.  
  63.                
  64.  
  65.                 ArrayList<Map<String,Object>> coll =new ArrayList<Map<String,Object>>();
  66.  
  67.                 Map<String,Object>item;
  68.  
  69.                 cur.moveToFirst();
  70.  
  71.                 while(!cur.isAfterLast()){
  72.  
  73.                         item=new HashMap<String, Object>();
  74.  
  75.                         item.put("c1",cur.getString(0)+","+cur.getString(1));
  76.  
  77.                         coll.add(item);
  78.  
  79.                         cur.moveToNext();
  80.  
  81.                 }
  82.  
  83.                 this.setListAdapter(new SimpleAdapter(this,coll,android.R.layout.simple_list_item_1,new String[]{"c1"},new int[]{
  84.  
  85.                                 android.R.id.text1}));                 
  86.  
  87.         }
  88.  
  89.         @Override                              
  90.  
  91.         protected void onListItemClick(ListView l, View v, int position, long id) {
  92.  
  93.                 // TODO Auto-generated method stub
  94.  
  95. //              super.onListItemClick(l, v, position, id);
  96.  
  97.                 finish();      
  98.  
  99.         }
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

with the XML;

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         package="cn.com.child" android:versionCode="1"
  6.  
  7.         android:versionName="1.0.0">
  8.  
  9.         <application android:icon="@drawable/icon"
  10.  
  11.                 android:label="@string/app_name">
  12.  
  13.  
  14.  
  15.                 <activity android:name=".data"
  16.  
  17.                         android:label="@string/app_name">
  18.  
  19.                         <intent-filter>
  20.  
  21.                                 <action android:name="android.intent.action.MAIN" />
  22.  
  23.                                 <category
  24.  
  25.                                         android:name="android.intent.category.LAUNCHER" />
  26.  
  27.                         </intent-filter>
  28.  
  29.                 </activity>
  30.  
  31.                 <provider android:name="DataProvider"
  32.  
  33.                         android:authorities="cn.com.provider.child">
  34.  
  35.                 </provider>
  36.  
  37.  
  38.  
  39.         </application>
  40.  
  41.  
  42.  
  43. </manifest>
  44.  
  45.  
  46.  
  47.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


someone tell me where is the error. I also have read the database tutorials written by plusminus months before, but still can't find the error. please someone help
Attachments
datalist_error.jpg
datalist_error.jpg (12.82 KiB) Viewed 598 times
User avatar
Bruce.lntu
Developer
Developer
 
Posts: 37
Joined: Thu Jan 03, 2008 11:36 am

Top

Postby rmanromga » Thu Nov 06, 2008 5:08 pm

Hello Bruce,

Have you tried using the debugger? This will help you narrow down where or what statement on your code is causing the error..then you can post it back on the forum... without doing that, you kind of force someone to try and run your whole code/project to help you which obviously is not feasible... unfortunately when an Android program crashes, the message shown on the screen "the program has quit with unexpected results" does not help much. I suggest using the debugger and looking at the call stacks and variables, and you will probably be able to narrow down what part of your code is throwing an exception....
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby MrSnowflake » Thu Nov 06, 2008 5:16 pm

Just take a look at your Log, that will make it a lot more clear.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Re: problem with read data from SQLite database

Postby rmanromga » Thu Nov 06, 2008 5:23 pm

Also, I just noticed a couple of things right off the bat that are problematic with your SQL create/insert strings...


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package cn.com.child;
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                 @Override
  8.  
  9.                 public void onCreate(SQLiteDatabase db) {
  10.  
  11.                         // TODO Auto-generated method stub
  12.  
  13.                         db.execSQL("CREATE TABLE"+STATION_TABLE_NAME+"("+"s_id"+"TEXT,"+"s_name"+"TEXT"+");");
  14.  
  15.                         String sql_1="insert into" <img src="http://www.anddev.org/images/smilies/yikes.png" alt=":o" title="Surprised" /> +STATION_TABLE_NAME+"(s_id,s_name)values('1','hcz');";
  16.  
  17.                         String sql_2="insert into" <img src="http://www.anddev.org/images/smilies/yikes.png" alt=":o" title="Surprised" /> +STATION_TABLE_NAME+"(s_id,s_name)values('2','lsdx');";
  18.  
  19.                         String sql_3="insert into" <img src="http://www.anddev.org/images/smilies/yikes.png" alt=":o" title="Surprised" /> +STATION_TABLE_NAME+"(s_id,s_name)values('3','xal');";
  20.  
  21.                         try {
  22.  
  23.                                 db.execSQL(sql_1);
  24.  
  25.                                 db.execSQL(sql_2);
  26.  
  27.                                 db.execSQL(sql_3);
  28.  
  29.                         } catch (SQLException e) {
  30.  
  31.                                 Log.e("ERROR",e.toString());
  32.  
  33.                                
  34.  
  35.                         }
  36.  
  37.                 }
  38.  
  39.  
  40.  
  41.        
  42.  
  43.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


You need to be careful when concatenating your strings for example you have:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String sql_1 = "insert into" + STATION_TABLE_NAME +"(s_id,s_name)values('1', 'hcz');";
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


This line should be:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String sql_1 = "insert into " + STATION_TABLE_NAME + " (s_id, s_name) values('1', 'hcz');";
  2.  
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Note the space that i have after the "into " and the space I have before the " (s_id"...
These are the things I mentioned above, that the debugger would help you catch because if you looked at the variable sql_1 in the debugger while the program is running, the sql statement would not make sense and thus throw an exception... remember the debugger IS YOUR FRIEND :)
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby Bruce.lntu » Fri Nov 07, 2008 2:25 am

MrSnowflake wrote:Just take a look at your Log, that will make it a lot more clear.


Hi, Snow

when I run the debugger, it shows me the log, I found the error "Failure 1 (near "TABLEstation": syntax error) on 0x7c310 when preparing 'CREATE TABLEstation(s_idTEXTs_nameTEXT);'.",then I check out and discover the error ,and then it works well.

really thank you. :)

Regards
Bruce
User avatar
Bruce.lntu
Developer
Developer
 
Posts: 37
Joined: Thu Jan 03, 2008 11:36 am

Postby Bruce.lntu » Fri Nov 07, 2008 2:29 am

rmanromga wrote:Hello Bruce,

Have you tried using the debugger? This will help you narrow down where or what statement on your code is causing the error..then you can post it back on the forum... without doing that, you kind of force someone to try and run your whole code/project to help you which obviously is not feasible... unfortunately when an Android program crashes, the message shown on the screen "the program has quit with unexpected results" does not help much. I suggest using the debugger and looking at the call stacks and variables, and you will probably be able to narrow down what part of your code is throwing an exception....


Hi rmanromga,

it's the time really thank you for mentioning me code and debug wieh debugger, this really help me a lot, better teach than give. thank you once more time. :)

Regards
Bruce
User avatar
Bruce.lntu
Developer
Developer
 
Posts: 37
Joined: Thu Jan 03, 2008 11:36 am

Top

Hi..Can U plz.... post your entire executable application

Postby yasmin » Tue Nov 18, 2008 5:17 am

Bruce.lntu wrote:
rmanromga wrote:Hello Bruce,

Have you tried using the debugger? This will help you narrow down where or what statement on your code is causing the error..then you can post it back on the forum... without doing that, you kind of force someone to try and run your whole code/project to help you which obviously is not feasible... unfortunately when an Android program crashes, the message shown on the screen "the program has quit with unexpected results" does not help much. I suggest using the debugger and looking at the call stacks and variables, and you will probably be able to narrow down what part of your code is throwing an exception....


Hi rmanromga,

it's the time really thank you for mentioning me code and debug wieh debugger, this really help me a lot, better teach than give. thank you once more time. :)

Regards
Bruce



Hi Bruce,

Nice to meet you here. I'm learning android. I want to know about Content Provider.

I think your entire project code will help me to know about the COntent Provider..

So plz... post ur entire source code to my mail id..

yasminussain@gmail.com

Thanks,
Yasmin
yasmin
yasmin
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 31, 2008 7:12 am

Postby Bruce.lntu » Tue Nov 18, 2008 8:14 am

Hi Yasmin,
The code is just above, pay attention to the "space" .
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  db.execSQL("CREATE TABLE "+STATION_TABLE_NAME+"(" + " s_id " + " TEXT, " + " s_name " + " TEXT " + ");");
  2.  
  3.                String sql_1="insert into "+STATION_TABLE_NAME+"(s_id,s_name)values('1','hcz');";
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

wish it can help you

Bruce
User avatar
Bruce.lntu
Developer
Developer
 
Posts: 37
Joined: Thu Jan 03, 2008 11:36 am

Thanks for Ur Reply...

Postby yasmin » Tue Nov 18, 2008 12:59 pm

Bruce.lntu wrote:Hi Yasmin,
The code is just above, pay attention to the "space" .
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  db.execSQL("CREATE TABLE "+STATION_TABLE_NAME+"(" + " s_id " + " TEXT, " + " s_name " + " TEXT " + ");");
  2.                String sql_1="insert into "+STATION_TABLE_NAME+"(s_id,s_name)values('1','hcz');";
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

wish it can help you

Bruce


Hi,

I got error "Source not found"

In the following line " Cursor cur = managedQuery(uri,PROJECTION, null,null, null); " within Data.java

I've included the Data.java & DataProvider.java under the package "com.aspire.android.dataprovider".

So, what I need to include in "AUTHORITY".

I didn't change anything except the package in your code.

Please help..or send ur application as zip file.

Thanks for your time.

Thanks Again!!!
Yasmin
yasmin
yasmin
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 31, 2008 7:12 am

Re: Thanks for Ur Reply...

Postby yasmin » Tue Nov 18, 2008 1:27 pm

yasmin wrote:
Bruce.lntu wrote:Hi Yasmin,
The code is just above, pay attention to the "space" .
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  db.execSQL("CREATE TABLE "+STATION_TABLE_NAME+"(" + " s_id " + " TEXT, " + " s_name " + " TEXT " + ");");
  2.                String sql_1="insert into "+STATION_TABLE_NAME+"(s_id,s_name)values('1','hcz');";
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

wish it can help you

Bruce


Hi,

I got error "Source not found"

In the following line " Cursor cur = managedQuery(uri,PROJECTION, null,null, null); " within Data.java

I've included the Data.java & DataProvider.java under the package "com.aspire.android.dataprovider".

So, what I need to include in "AUTHORITY".

I didn't change anything except the package in your code.

Please help..or send ur application as zip file.

Thanks for your time.

Thanks Again!!!
Yasmin


Hi Bruce,

Reason for my error is cur = null;

I hope I'm mentioning the AUTHORITY worngly. What AUTHORITY refers....

How can I check the table "station" was created & 3 records are inserted?

If I know this then I can solve the issue easily.

Thanks for your time.
yasmin
yasmin
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 31, 2008 7:12 am

Postby Bruce.lntu » Wed Nov 19, 2008 10:46 am

Hi,

just have a look this page,it may help you

http://www.anddev.org/tinytut_-_pull-pu ... -t113.html

or you can check it by shell commands.

Bruce
User avatar
Bruce.lntu
Developer
Developer
 
Posts: 37
Joined: Thu Jan 03, 2008 11:36 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests