| Author |
Message |
szeldon Freshman

Joined: 14 Feb 2008 Posts: 5
|
Posted: Sun May 18, 2008 11:04 pm Post subject: |
|
|
I'm not completly sure, but I think that you should close your cursor after using it. _________________ szeldon.com |
|
| Back to top |
|
 |
|
|
 |
nithin.warier Experienced Developer

Joined: 28 Feb 2008 Posts: 85 Location: Malappuram Kerala India
|
Posted: Wed May 28, 2008 1:58 pm Post subject: |
|
|
Hi plusminus,
how we can get the maximum value of an attribute in a table, in terminal , i checked and its working,
the query is -> select max(key) from Testing;
but how we can do this in Android.
please reply immediately
Thanks
Nithin |
|
| Back to top |
|
 |
nithin.warier Experienced Developer

Joined: 28 Feb 2008 Posts: 85 Location: Malappuram Kerala India
|
Posted: Wed May 28, 2008 2:59 pm Post subject: |
|
|
Here, the return value is an integer, but for Sqlitedatabase.query() its returning cursor, so how can we get the integer value.
thanks
Nithin |
|
| Back to top |
|
 |
Niketa Developer

Joined: 17 Sep 2008 Posts: 36
|
Posted: Mon Sep 29, 2008 12:54 pm Post subject: Null Pointer Exception while creating table |
|
|
hi,
I have implement this example. but i got an error.
I have write the code just for create database, create table, insert values in it.
When I run application it displays nothing.
But when i tried to open my database from command prompt Like:
ls data/data/MyPackageNm
Then it gives only a Lib folder rather than database and Lib both.
So, i tried to found error via Log cat and put my code in try and catch.
I got an error while creating table.
its gave Null Pointer exception.
Please any one help me.
My Log cat is like that and i have use this sample example as my code.
Thanks.
| Code: |
uid=10032 gids={}
09-29 15:36:09.727: DEBUG/dalvikvm(813): LinearAlloc 0x0 used 538516 of 4194304 (12%)
09-29 15:36:09.837: INFO/jdwp(822): received file descriptor 10 from ADB
09-29 15:36:10.128: INFO/Niketa(822): database created
09-29 15:36:10.137: INFO/Niketa(822): database opened
09-29 15:36:10.137: INFO/Niketa(822): ------null
09-29 15:36:10.147: INFO/Niketa(822): ++++++java.lang.NullPointerException
09-29 15:36:10.337: INFO/ActivityManager(52): Displayed activity com.db/.DataBaseWork: 754 ms
09-29 15:36:15.507: DEBUG/dalvikvm(675): GC freed 44 objects / 1800 bytes in 151ms
09-29 15:36:15.507: DEBUG/dalvikvm(118): GC freed 3 objects / 72 bytes in 143ms
09-29 15:36:15.527: DEBUG/dalvikvm(92): GC freed 1539 objects / 70560 bytes in
|
My java code is:
| Java: |
package com.db;
//import java.io.FileNotFoundException;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
public class DataBaseWork extends Activity {
private final String MY_DATABASE_NAME = "LoginDB";
private final String MY_DATABASE_TABLE = "Login1";
private final String tag="Niketa";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase myDB = null;
try {
/* Create the Database (no Errors if it already exists) */
this.createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
Log.i(tag,"database created");
/* Open the DB and remember it */
myDB = this.openDatabase(MY_DATABASE_NAME, null);
Log.i(tag,"database opened ");
/* Create a Table in the Database. */
try
{
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ "LoginDB.Login1"
+ " (UserNm TEXT, PassWd TEXT)"
+";");
Log.i(tag,"table created ");
}
/* catch(SQLException e1)
{
Log.i(tag,"SQL EXception"+e1);
}*/
catch(Exception e)
{
Log.i(tag,"------"+e.getMessage());
Log.i(tag,"++++++"+e);
}
/* catch(SQLiteException se)
{
Log.i(tag,"SQLite Error--------"+se);
}*/
/* Add two DataSets to the Table. */
myDB.execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (UserNm, PassWd)"
+ " VALUES ('abc', 'def');");
Log.i(tag,"value inserted ");
/* Query for some results with Selection and Projection. */
Cursor c = myDB.rawQuery("SELECT UserNm,PassWd" +
" FROM " + MY_DATABASE_TABLE
+ ";",
null);
Log.i(tag,"value seleted ");
/* Get the indices of the Columns we will need */
int firstNameColumn = c.getColumnIndex("UserNm");
int ageColumn = c.getColumnIndex("PassWd");
/* Check if our result was valid. */
if (c != null) {
/* Check if at least one Result was returned. */
if (c.isFirst()) {
int i = 0;
do {
i++;
String firstName = c.getString(firstNameColumn);
int age = c.getInt(ageColumn);
String ageColumName = c.getColumnName(ageColumn);
results.add("" + i + ": " + firstName
+ " (" + ageColumName + ": " + age + ")");
} while (c!=null);
}
}
}
catch (Exception e)
{
Log.i(tag,"Error----->"+e);
}
finally
{
if (myDB != null)
myDB.close();
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter)
{
}
private SQLiteDatabase openDatabase(String my_database_name2, Object object)
{
return null;
}
private void createDatabase(String my_database_name2, int i,
int modePrivate, Object object)
{
}
}
|
|
|
| Back to top |
|
 |
nithin.warier Experienced Developer

Joined: 28 Feb 2008 Posts: 85 Location: Malappuram Kerala India
|
Posted: Mon Sep 29, 2008 1:12 pm Post subject: |
|
|
hi,
try c.moveToFirst();
before if(c != null) and try
Thanks
nithin |
|
| Back to top |
|
 |
Niketa Developer

Joined: 17 Sep 2008 Posts: 36
|
Posted: Mon Sep 29, 2008 1:45 pm Post subject: its still not working |
|
|
hi,
Thanks for the help.
But its still not working. |
|
| Back to top |
|
 |
|
|
 |
Rahul Experienced Developer

Joined: 01 Oct 2008 Posts: 85
|
Posted: Wed Oct 15, 2008 7:28 am Post subject: |
|
|
When i try running this code
| Java: | package org. anddev. android. databasework;
import java. util. ArrayList;
import android. app. ListActivity;
import android. database. Cursor;
import android. database. sqlite. SQLiteDatabase;
import android. os. Bundle;
import android. widget. ArrayAdapter;
import android. widget. EditText;
public class DataBaseWork extends ListActivity {
private final String MY_DATABASE_NAME = "myCoolDB_2";
private final String MY_DATABASE_TABLE = "Users";
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle icicle ) {
super. onCreate(icicle );
EditText et = new EditText (this);
et. setSelection(et. getText(). length());
/* Will hold the 'Output' we want to display at the end. */
ArrayList<String> results = new ArrayList<String> ();
SQLiteDatabase myDB = null;
try {
/* Create the Database (no Errors if it already exists) */
this. createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
/* Open the DB and remember it */
myDB = this. openDatabase(MY_DATABASE_NAME, null);
/* Create a Table in the Database. */
myDB. execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (LastName VARCHAR, FirstName VARCHAR,"
+ " Country VARCHAR, Age INT(3));");
/* Add two DataSets to the Table. */
myDB. execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Gramlich', 'Nicolas', 'Germany', 20);");
myDB. execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Doe', 'John', 'US', 34);");
/* Query for some results with Selection and Projection. */
Cursor c = myDB. rawQuery("SELECT FirstName,Age" +
" FROM " + MY_DATABASE_TABLE
+ " WHERE Age > 10 LIMIT 7;",
null);
/* Get the indices of the Columns we will need */
int firstNameColumn = c. getColumnIndex("FirstName");
int ageColumn = c. getColumnIndex("Age");
/* Check if our result was valid. */
c. moveToFirst();
if (c != null) {
/* Check if at least one Result was returned. */
if (c. isFirst()) {
int i = 0;
/* Loop through all Results */
do {
i++;
/* Retrieve the values of the Entry
* the Cursor is pointing to. */
String firstName = c. getString(firstNameColumn );
int age = c. getInt(ageColumn );
/* We can also receive the Name
* of a Column by its Index.
* Makes no sense, as we already
* know the Name, but just to show we can */
String ageColumName = c. getColumnName(ageColumn );
/* Add current Entry to results. */
results. add("" + i + ": " + firstName
+ " (" + ageColumName + ": " + age + ")");
} while(c!= null);
}
}
} finally {
if (myDB != null)
myDB. close();
}
this. setListAdapter(new ArrayAdapter<String> (this,
android. R. layout. simple_list_item_1, results ));
}
private SQLiteDatabase openDatabase (String my_database_name2, Object object ) {
// TODO Auto-generated method stub
return null;
}
private void createDatabase (String my_database_name2, int i,
int modePrivate, Object object ) {
// TODO Auto-generated method stub
}
} |
AndroidManifest.xml
| XML: |
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tp.proj.dat">
<application android:icon="@drawable/icon">
<activity android:name=".SQLite" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> |
i get the following error
| Quote: | application org.andev.android.databasework(Processorg.anddev.android.databasework)has stopped unexpectedly.Please try again
|
Please help me.. |
|
| Back to top |
|
 |
bino Freshman


Joined: 13 Oct 2008 Posts: 9
|
Posted: Wed Oct 15, 2008 1:52 pm Post subject: |
|
|
To Rahul, you should change some places in your code:
Replace:
| Java: | this.createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
myDB = this.openDatabase(MY_DATABASE_NAME, null); |
By:
| Java: | myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null); |
And, of course, we don't need the two methods (just simply delete them):
| Java: | private SQLiteDatabase openDatabase
private void createDatabase |
Replace:
By:
| Java: | while(c.moveToNext()); |
In the myDB.rawQuery(...), I read from the SDK 1.0 that you should not add the ";" at the end of the query.
By the way, I found somethings look strange in your Manifest file, the package name and the activity name are not the same in your class (please check it again).
I am not very good in expressing in English. |
|
| Back to top |
|
 |
Rahul Experienced Developer

Joined: 01 Oct 2008 Posts: 85
|
Posted: Wed Oct 15, 2008 2:39 pm Post subject: |
|
|
@ Bino
Thank you very much...That solved my problem...
The query works with and without adding ';' i think the ';' does not make any difference as you said |
|
| Back to top |
|
 |
jcook2004 Freshman

Joined: 02 Nov 2008 Posts: 6
|
Posted: Sun Nov 02, 2008 9:04 am Post subject: clarification |
|
|
With this method, what happens when the application is closed? Is the database then delete? It seems like on every start the database gets re-created.
Heres my question, what if I insert some information into the database - i then close the application. What happens to that data? When I reopen it, is that inserted data still there?
Does this method re-create the database every time?
I would like to know an efficient way of creating a database one time (@ the install/initial launch of the app), and then having the ability to append data to that database and having it available for retrieval at later dates.
Thanks for the help! you're the man! |
|
| Back to top |
|
 |
dkkundudolan Experienced Developer

Joined: 03 Mar 2008 Posts: 83
|
Posted: Fri Dec 26, 2008 6:24 am Post subject: |
|
|
Hi bino,
I already changed the options what you had mentioned for the above post.
But still its showing "The application has stopped unexpectedly. Please try again".
I am using sdk v1.0. please can you or anybody can help? waiting for reply....
Thanks. |
|
| Back to top |
|
 |
dkkundudolan Experienced Developer

Joined: 03 Mar 2008 Posts: 83
|
Posted: Fri Dec 26, 2008 8:01 am Post subject: |
|
|
Hi All,
I solved the probs.....
Thanks. |
|
| Back to top |
|
 |
roland909 Freshman

Joined: 28 Dec 2008 Posts: 2 Location: Frankfurt am Main
|
Posted: Sat Jan 03, 2009 5:20 pm Post subject: |
|
|
Hi folks,
as this howto seems to be outdated...I wanted to ask if any of you has a few suggestions/tipps to get it working with the actual SDK...? Just to ask in advance...because I'll start learning/working on it this weekend.
Have a nice weekend!  |
|
| Back to top |
|
 |
kali Experienced Developer

Joined: 27 Jan 2009 Posts: 62
|
Posted: Mon Feb 16, 2009 1:32 pm Post subject: |
|
|
hai friend,
i have dowmload your whole code
but it gives error at
myDB.createDataBase();
&
myDB.openDataBase();
is giving error "the method is undefined"
pls help
thanks in advance, |
|
| Back to top |
|
 |
dhaiwat Developer

Joined: 10 Feb 2009 Posts: 26
|
Posted: Wed Feb 18, 2009 8:07 am Post subject: error |
|
|
hello +- i write above cod and i create my database also
but i get error at show the record on screen when statement like
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
hope u will get my problem and relpy best... |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
© 2007, Android Development Community
All rights reserved.
Powered by phpBB.
|