Fetching database content

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

Fetching database content

Postby immanueln2005 » Wed Jan 07, 2009 5:41 am

Hi,

I really need help with this.. I have trouble accessing a database.

The application hangs everytime i step into the fetchProfile() method.
Every other way i know produced no results.

I am attaching the 2 classes in a txt file.

Tried everythin i possibly knew, and now i'm saturated..

Just tel me wat mistake am I doin here, will be very greatful to you...

Thanks,
Immanuel
Attachments
code.txt
(6.91 KiB) Downloaded 60 times
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Postby MrSnowflake » Wed Jan 07, 2009 12:59 pm

And what's the error the application generates?

instead of:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         if(result.getCount() > 0)
  2.  
  3.                         {
  4.  
  5.                                 result.moveToFirst();
  6.  
  7.                                 return result;
  8.  
  9.                                
  10.  
  11.                         }
  12.  
  13.                         else
  14.  
  15.                         {
  16.  
  17.                                 SwapCard s = new SwapCard();
  18.  
  19.                                 s.showAlert("Error printing");
  20.  
  21.                                 return null;
  22.  
  23.                         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
you should do:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         if(result.moveToFirst())
  2.  
  3.                         {
  4.  
  5.                                 return result;
  6.  
  7.                                
  8.  
  9.                         }
  10.  
  11.                         else
  12.  
  13.                         {
  14.  
  15.                                 SwapCard s = new SwapCard();
  16.  
  17.                                 s.showAlert("Error printing");
  18.  
  19.                                 return null;
  20.  
  21.                         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Well, you _SHOULDN'T_ do this, but as it has the same effect you are probably doing less operations (this means more speed, wooot :)).

As far as my eye can see for now, I'd say there are a lot of 'bad' coding practises (well, they aren't really bad, they just cost a lot of overhead). For example: in fetchProfile() you findViewById() all views you need, which costs a lot, while you could/should do this in the onCreate, as the views don't change in you application. So you only should find them once!
2nd, I'm not sure if this is needed, but as you test against result != null, you could also start managing the cursor if it isn't null, because a null cursor never can be closed (maybe this even throws an exception).

But now I see your mistake (well the real probematic one :): in DB.fetch() you define String[] columns as {"KEY_NAME", ... wheras this should be {KEY_NAME, KEY_COMPANY, ... because you want the DB constants to be used and not the literals! This will make you fetchProfile throw as you use result.getColumnIndexOrThrow(SwapCardDB.KEY_COMPANY))) which would be working if you were defining the column names correctly. Now I think about it , the result.getColumnIndexOrThrow(SwapCardDB.KEY_COMPANY))) will not generate the exception but the mDb.query() in DB.fetch() will, because you want to fetch columns which don't exist.

I guess that if you had looked in your log, you would have see the exception and found this problem by yourself, as this would have thrown some exceptions a long the line of "column not found" or something like that.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby immanueln2005 » Wed Jan 07, 2009 2:27 pm

Thanks for the reply, :)

No error is generated in both the cases.
Sorry if this's a stupid question, but where am i supposed to view the log? There's no error displayed in the console.

I realized the problem with String[] columns, and corrected it. But my problem still doesn't seem to solve.

Here is the flow of my application.

> I insert a new record.

> I try to retrieve it. Not possible. Database helper is null.

> I try to insert another record, and it gets inserted as the first record.

> I try to retrieve it. Again not possible.

Thanks again,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby MrSnowflake » Wed Jan 07, 2009 4:01 pm

In eclipse use the DDMS view to see the log(cat) or use the commandline tool 'adb logcat'.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby immanueln2005 » Mon Jan 12, 2009 2:21 pm

I tried my best to try out every possibility again. :cry:

I dropped the table. And now it now says no column with "name" exists.

I tired out all possible logic with KEY_NAME and name... No luck..
Attached both classes.

I'm missing out something somewhere.. Just wanna know where.

Thanks,
Immanuel
Attachments
SwapCardDB.java
(4.99 KiB) Downloaded 52 times
MyProfile.java
(6.13 KiB) Downloaded 53 times
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby immanueln2005 » Mon Jan 12, 2009 2:31 pm

I replaced the INSERT code posted before this with,


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ContentValues initialValues = new ContentValues();
  2.  
  3.                 initialValues.put("name", name);
  4.  
  5.                 initialValues.put("company", company);
  6.  
  7.                 initialValues.put("email", email);
  8.  
  9.  
  10.  
  11.                 if(mDb != null)
  12.  
  13.                 {
  14.  
  15.                         if(mDb.insert(DATABASE_TABLE, null, initialValues) > 0)
  16.  
  17.                                 return true;
  18.  
  19.                         else
  20.  
  21.                                 return false;
  22.  
  23.                 }
  24.  
  25.                 else
  26.  
  27.                         return false;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


[syntax="java"]

It says no table Profile found

Uffffffffffffffff...............
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Postby immanueln2005 » Tue Jan 13, 2009 1:37 pm

The control never reaches
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mDbHelper = new DatabaseHelper(mCtx);
  3.  
  4.                 mDb = mDbHelper.getWritableDatabase();
  5.  
  6.                 //mDbHelper.onOpen(mDb);
  7.  
  8.                 //mDb.execSQL(DATABASE_CREATE);
  9.  
  10.                 return this;
  11.  
  12.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
[syntax="java"]
:(
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby MrSnowflake » Tue Jan 13, 2009 2:16 pm

You should post your log too, as there probably will be an SQLException or something else.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby immanueln2005 » Wed Jan 14, 2009 8:42 am

Here is the log..

It says "No such table: Profile"
Attachments
log.txt
(4.6 KiB) Downloaded 56 times
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby immanueln2005 » Thu Jan 15, 2009 12:33 pm

Problem solved.. .

I had problems in passing Extras from the root directory..
None of those codes are posted here..

Thanks for the help snowflakes..
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Return to Networking & Database Problems

Who is online

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