CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    3

    Unhappy Android - SQLite Database help

    I am getting the following error..
    //Cannot make static reference to non-static methos
    //Cannot make static reference to non-static fields

    I have use DBHelper as the object of DatabaseHelper class and db for SQLiteDatabase..
    following is the code

    package database.create;


    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;


    public class DBAdapter extends Activity {

    public static final String KEY_STDID = "_ID";
    public static final String KEY_FNAMEID = "FNAME";
    public static final String KEY_LNAMEID = "LNAME";
    public static final String KEY_DOBID = "DOB";
    public static final String KEY_HEIGHTID = "HEIGHT";
    public static final String KEY_WEIGHTID = "WEIGHT";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "HEALTHCARE";
    private static final String DATABASE_TABLE = "PHYSICAL_FITNESS";
    private static final int DATABASE_VERSION = 1;

    public static final String DATABASE_CREATE = "create table PHYSICAL_FITNESS (_id integer primary key,"
    + "fname text not null, lname text not null," +
    " d.o.b integer not null, height integer not null," + "weight integer not null); ";

    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
    }


    private static class DatabaseHelper extends SQLiteOpenHelper
    {
    DatabaseHelper (Context context)
    {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db)
    {
    db.execSQL(DATABASE_CREATE);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
    int newVersion)
    {
    Log.w(TAG, "Upgrading database from version " + oldVersion
    + " to "
    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS physical_fitness");
    onCreate(db);

    }

    public DatabaseHelper open() throws SQLException
    {
    db = DBHelper.getWritableDatabase();
    return this;
    }

    public void close()
    {
    DBHelper.close();
    }
    public long insertphysical_fitness (String _id, String FNAME, String LNAME, String DOB, String HEIGHT, String WEIGHT)
    {
    ContentValues initialvalues = new ContentValues();
    initialvalues.put(KEY_STDID,_id);
    initialvalues.put(KEY_FNAMEID, FNAME);
    initialvalues.put(KEY_LNAMEID, LNAME);
    initialvalues.put(KEY_DOBID, DOB);
    initialvalues.put(KEY_HEIGHTID, HEIGHT);
    initialvalues.put(KEY_WEIGHTID, WEIGHT);
    return db.insert(DATABASE_TABLE, null, initialvalues);

    }

    public boolean deleteTitle(long rowID){
    return db.delete(DATABASE_TABLE, KEY_STDID + "=" + rowID, null) > 0;

    }
    public Cursor getAllTitles()
    {

    return db.query(DATABASE_TABLE, new String[] {
    KEY_STDID,
    KEY_FNAMEID,
    KEY_LNAMEID,
    KEY_DOBID,
    KEY_HEIGHTID,
    KEY_WEIGHTID},
    null,
    null,
    null,
    null,
    null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long _Id) throws SQLException
    {
    Cursor mCursor =
    db.query(true, DATABASE_TABLE, new String[] {
    KEY_STDID,
    KEY_FNAMEID,
    KEY_LNAMEID,
    KEY_DOBID,
    KEY_HEIGHTID,
    KEY_WEIGHTID
    },
    KEY_STDID + "=" + _Id,
    null,
    null,
    null,
    null,
    null);
    if (mCursor != null) {
    mCursor.moveToFirst();
    }
    return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long _Id, String FNAME,
    String LNAME, String DOB, String HEIGHT, String WEIGHT)
    {
    ContentValues args = new ContentValues();
    args.put(KEY_FNAMEID, FNAME);
    args.put(KEY_LNAMEID, LNAME);
    args.put(KEY_DOBID, DOB);
    return db.update(DATABASE_TABLE, args,
    KEY_STDID + "=" + _Id, null) > 0;
    }
    }
    }

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Android - SQLite Database help

    Please format your code in the "[code]" tags, it makes it a lot easier to read.

    What line are you getting the error at? Is it this one:
    Code:
    Log.w(TAG, "Upgrading database from version " + oldVersion 
                            + " to "
                            + newVersion + ", which will destroy all old data");

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Android - SQLite Database help

    If you get an error, please post the full error message text and stack trace, if present.

    Doing more things faster is no substitute for doing the right things...
    S. R. Covey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Aug 2009
    Posts
    3

    Re: Android - SQLite Database help

    I get the error on the db and DBHelper saying "Cannot make a static reference to the non-static field db" and "Cannot make a static reference to the non-static field DBHelper".

    1 quick fix available
    change the modifier of db to static and change the modifier of DBHelper to static

    Code:
    //DBAdapter.java
    package database.create;
    
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    
    
    public class DBAdapter extends Activity {
    
    		public static final String KEY_STDID = "_ID";
    		public static final String KEY_FNAMEID = "FNAME";
    		public static final String KEY_LNAMEID = "LNAME";
    		public static final String KEY_DOBID = "DOB";
    		public static final String KEY_HEIGHTID = "HEIGHT";
    		public static final String KEY_WEIGHTID = "WEIGHT";
    		private static final String TAG = "DBAdapter";
    		
    		private static final String DATABASE_NAME = "HEALTHCARE";
    		private static final String DATABASE_TABLE = "PHYSICAL_FITNESS";
    		private static final int DATABASE_VERSION = 1;
    		
    		public static final String DATABASE_CREATE = "create table PHYSICAL_FITNESS (_id integer primary key," 
    			+ "fname text not null, lname text not null," +
    			" d.o.b integer not null, height integer not null," + "weight integer not null); ";
    		
    		private final Context context;
    		private DatabaseHelper DBHelper;
    		private SQLiteDatabase db;
    		
    		public DBAdapter(Context ctx) {
    			this.context = ctx;
    			DBHelper = new DatabaseHelper(context);
    					}
    		
    
    		private static class DatabaseHelper extends SQLiteOpenHelper
    		{
    			DatabaseHelper (Context context)
    			{
    				super(context, DATABASE_NAME, null, DATABASE_VERSION);
    			}
    			
    			public void onCreate(SQLiteDatabase db)
    			{
    				db.execSQL(DATABASE_CREATE);
    			}
    			public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                        int newVersion) 
    			{
    				Log.w(TAG, "Upgrading database from version " + oldVersion 
    						+ " to "
    						+ newVersion + ", which will destroy all old data");
    				db.execSQL("DROP TABLE IF EXISTS physical_fitness");
    				onCreate(db);
    
    		}
    			
    			public DatabaseHelper open() throws SQLException 
    			{
    				db = DBHelper.getWritableDatabase();
    				return this;
    			}
    			
    			public void close()
    			{
    				DBHelper.close();
    			}
    			public long insertphysical_fitness (String _id, String FNAME, String LNAME, String DOB, String HEIGHT, String WEIGHT)
    			{
    				ContentValues initialvalues = new ContentValues();
    				initialvalues.put(KEY_STDID,_id);
    				initialvalues.put(KEY_FNAMEID, FNAME);
    				initialvalues.put(KEY_LNAMEID, LNAME);
    				initialvalues.put(KEY_DOBID, DOB);
    				initialvalues.put(KEY_HEIGHTID, HEIGHT);
    				initialvalues.put(KEY_WEIGHTID, WEIGHT);
    				return db.insert(DATABASE_TABLE, null, initialvalues);
    				
    			}
    			
    			public boolean deleteTitle(long rowID){
    				return db.delete(DATABASE_TABLE, KEY_STDID + "=" + rowID, null) > 0;
    				
    			}
    			public Cursor getAllTitles() 
    		    {
    		        		
    		        		return db.query(DATABASE_TABLE, new String[] {
    		        		KEY_STDID, 		        		
    		        		KEY_FNAMEID,
    		        		KEY_LNAMEID,
    		                KEY_DOBID,
    		                KEY_HEIGHTID,
    		                KEY_WEIGHTID}, 
    		                null, 
    		                null, 
    		                null, 
    		                null, 
    		                null);
    		    }
    
    		    //---retrieves a particular title---
    		    public Cursor getTitle(long _Id) throws SQLException 
    		    {
    		        Cursor mCursor =
    		                db.query(true, DATABASE_TABLE, new String[] {
    		                		KEY_STDID,
    		                		KEY_FNAMEID, 
    		                		KEY_LNAMEID,
    		                		KEY_DOBID,
    		                		KEY_HEIGHTID,
    		                		KEY_WEIGHTID
    		                		}, 
    		                		KEY_STDID + "=" + _Id, 
    		                		null,
    		                		null, 
    		                		null, 
    		                		null, 
    		                		null);
    		        if (mCursor != null) {
    		            mCursor.moveToFirst();
    		        }
    		        return mCursor;
    		    }
    
    		    //---updates a title---
    		    public boolean updateTitle(long _Id, String FNAME, 
    		    String LNAME, String DOB, String HEIGHT, String WEIGHT) 
    		    {
    		        ContentValues args = new ContentValues();
    		        args.put(KEY_FNAMEID, FNAME);
    		        args.put(KEY_LNAMEID, LNAME);
    		        args.put(KEY_DOBID, DOB);
    		        return db.update(DATABASE_TABLE, args, 
    		                         KEY_STDID + "=" + _Id, null) > 0;
    		    }
    		}	
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    966

    Re: Android - SQLite Database help

    Your problem is here:
    Code:
    private static class DatabaseHelper extends SQLiteOpenHelper
     {
       ...
    
       public DatabaseHelper open() throws SQLException 
       {
         db = DBHelper.getWritableDatabase();
    You declare DBHelper up in the outter class as:

    private DatabaseHelper DBHelper;

    So you are trying to access a non-static variable, DBHelper, from the static context, which is the "private static class DatabaseHelper"

    Why does your nester inner class need to be static?
    Last edited by ProgramThis; August 20th, 2009 at 01:21 PM.

  6. #6
    Join Date
    Aug 2009
    Posts
    3

    Smile Re: Android - SQLite Database help

    Thanks I could figure that out with some adjustments in my code.

    Thanks a lot.

  7. #7
    Join Date
    Jul 2012
    Posts
    3

    Re: Android - SQLite Database help


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured