I got the solution. I was happening because cursor adapter should has _id
column in table it is using.

if we use Database(SQLite) in Android Applications, it is better to add a
column named "_id" to all tables to use CursorAdapter.
Or you need to write sql state "select member_id as _id from member _table
where ....." to get Cursor.

You can find this matter at Android Developer
Site<http://developer.android.com/intl/ja/reference/android/widget/CursorAdapter.html>
"Adapter that exposes data from a Cursor to a ListView widget. The Cursor
must include a column named "_id" or this class will not work."

On Tue, Jul 13, 2010 at 3:27 PM, Renuka Deshpande <
[email protected]> wrote:

> Hello All,
>
> I am developing application in which I am using List view and for that list
> I am using a cursor adapter.
> I have an sqlite file in assets/databases which is used for reading data
> and create database.
> I created database. For cursor adapter I am getting cursor from this
> database.
>
> Here is my code
>
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
>
> import android.content.Context;
> import android.database.Cursor;
> import android.database.SQLException;
> import android.database.sqlite.SQLiteDatabase;
> import android.database.sqlite.SQLiteException;
> import android.database.sqlite.SQLiteOpenHelper;
> import android.util.Log;
>
> /**
>  * @author Renuka A.
>  *
>  */
> public class DataBaseHelper extends SQLiteOpenHelper{
>
>     //The Android's default system path of your application database.
>     private static String DB_PATH =
> "/data/data/com.praumtech.names4baby/databases/";
>
>     private static String DB_NAME = "newdata";
>     private static String DB_FILE_NAME = "newdata.sqlite";
>     private SQLiteDatabase _database;
>
>     public static int ALL_NAMES = 0;
>     public static int FAVORITE_NAME = 1;
>     private String FAVORITE_TABLE = "favmaster";
>     private String NAME_MASTER_TABLE = "namemaster";
>
>
>     private final Context _context;
>
>     /**
>      * Constructor
>      * Takes and keeps a reference of the passed context in order to access
> to the application assets and resources.
>      * @param context
>      */
>     public DataBaseHelper(Context context) {
>
>         super(context, DB_NAME, null, 1);
>         _context = context;
>     }
>
>     /**
>      * Creates a empty database on the system and rewrites it with your own
> database.
>      * */
>     public void createDataBase() throws IOException{
>         boolean dbExist = checkDataBase();
>         Log.d("Exception", " checkDataBase  "+dbExist);
>         if(dbExist){
>             //do nothing - database already exist
>             try {
>                 String myPath = DB_PATH + DB_NAME;
>                 _database = SQLiteDatabase.openDatabase(myPath, null,
> SQLiteDatabase.OPEN_READWRITE);
>                 Log.d("Exception", " create database to open databse ");
>             } catch (Exception e) {
>                 e.printStackTrace();
>             }
>         }else{
>
>             //By calling this method and empty database will be created
> into the default system path
>             //of your application so we are gonna be able to overwrite that
> database with our database.
>             this.getReadableDatabase();
>             Log.d("Exception", " in copy database of cretae ");
>             try {
>                 _context.openOrCreateDatabase(DB_NAME, 0, null);
>                 copyDataBase();
>             } catch (IOException e) {
>                 throw new Error("Error copying database");
>             }
>         }
>     }
>
>     /**
>      * Check if the database already exist to avoid re-copying the file
> each time you open the application.
>      * @return true if it exists, false if it doesn't
>      */
>     private boolean checkDataBase(){
>         SQLiteDatabase checkDB = null;
>         try{
>
>             String myPath = DB_PATH + DB_NAME;
>             checkDB = SQLiteDatabase.openDatabase(myPath, null,
> SQLiteDatabase.OPEN_READONLY);
>             //checkDB = _context.openOrCreateDatabase(DB_NAME, null,
> SQLiteDatabase.OPEN_READWRITE);
>         }catch(SQLiteException e){
>             //database does't exist yet.
>         }
>         if(checkDB != null){
>             checkDB.close();
>         }
>         return checkDB != null ? true : false;
>     }
>
>     /**
>      * Copies your database from your local assets-folder to the just
> created empty database in the
>      * system folder, from where it can be accessed and handled.
>      * This is done by transfering bytestream.
>      * */
>     private void copyDataBase() throws IOException{
>
>         try {
>             //Open your local db as the input stream
>             Log.d("Exception", " in copy database
> "+_context.getAssets().list("databases").length);
>             String[] str = _context.getAssets().list("databases");
>             /*    for(int i = 0 ; i < str.length; i++){
>                 Log.d("Exception", " i "+i+"  "+str[i]);
>              }*/
>             InputStream myInput =
> _context.getAssets().open("databases/"+DB_FILE_NAME);
>
>             //    Log.d("Exception", " in copy database
> "+myInput.available());
>             // Path to the just created empty db
>             String outFileName = DB_PATH + DB_NAME;
>
>             //Open the empty db as the output stream
>             OutputStream myOutput = new FileOutputStream(outFileName);
>             //    Log.d("Exception", " in copy database output file
> "+myOutput.toString());
>             //transfer bytes from the inputfile to the outputfile
>             byte[] buffer = new byte[1024];
>             int length;
>             while ((length = myInput.read(buffer))>0){
>                 myOutput.write(buffer, 0, length);
>             }
>             //Close the streams
>             myOutput.flush();
>             myOutput.close();
>             myInput.close();
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>
>     }
>
>     public void openDataBase() throws SQLException{
>         //Open the database
>         try {
>             String myPath = DB_PATH + DB_NAME;
>             _database = _context.openOrCreateDatabase(DB_NAME,
> SQLiteDatabase.OPEN_READWRITE, null);
>         } catch (Exception e) {
>             Log.d("Exception", " error in opening database "+e.toString());
>             e.printStackTrace();
>         }
>     }
>
>     @Override
>     public synchronized void close() {
>         if(_database != null)
>             _database.close();
>         super.close();
>     }
>
>     @Override
>     public void onCreate(SQLiteDatabase db) {
>
>     }
>
>     @Override
>     public void onUpgrade(SQLiteDatabase db, int oldVersion, int
> newVersion) {
>
>     }
>
>     public Cursor getCursorForAllNames(){
>         Cursor c = null;
>         try {
> String selectQuery = "SELECT id, name, surname, gender, designation FROM
> "+NAME_MASTER_TABLE;
>           if(_database != null){
>                 if(_database.isOpen()){
>                     Log.d("NAMES_BABY", " open database ");
>                     c = _database.rawQuery(selectQuery, null);
>                     //c = _database.query(NAME_MASTER_TABLE, strColumns,
> null, null, null, null, null);
>                     int cnt = c.getColumnCount();
>                     Log.d("NAMES_BABY", " getCursorForAllNames column count
> "+cnt);
>                     String[] str1 = new String[cnt];
>                     int[] columnIds = new int[cnt];
>                     for(int i = 0; i < cnt; i++){
>                         str1[i] = c.getColumnName(i);
>                         columnIds[i] = c.getColumnIndex(str1[i]);
>                         Log.d("LOGS", " getCursorForAllNames column "+i +"
> "+str1[i]);
>                     }
>                 }
>             }
>         }catch(Exception ex){
>             ex.printStackTrace();
>         }
>         return c;
>     }
>
>
> I am getting error as java.lang.IllegalArgumentException: column '_id' does
> not exist
>
> Can anybody help me out? How to solve this error.
>
> --
> Thanks & Regards,
> Renuka
>



-- 
Thanks & Regards,
Renuka

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to