Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
//The code for DBAdapter.java which maintains the database
 
package com.example.databases;
 
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
{
	public static final String KEY_ROWID="_id";
	public static final String KEY_NAME="name";
	public static final String KEY_EMAIL="email";
	private static final String TAG="DBAdapter";
	private static final String DATABASE_NAME="MyDB";
	private static final String DATABASE_TABLE="contacts";
	private static final int DATABASE_VERSION=1;
	private static final String DATABASE_CREATE="create table contacts(_id integer primary key autoincrement,"+
												"name text not null,email text 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);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			try
			{
				db.execSQL(DATABASE_CREATE);
			}
			catch(SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
		{
			Log.w(TAG,"Updating database from version"+oldVersion+"to"+newVersion+" which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
		}
		
		//opens the database
		public DBAdapter open() throws SQLException
		{
			db=DBHelper.getWritableDatabase();
			return this;
		}
		
		//closes the database
		public void close()
		{
			DBHelper.close();
		}
		
		//insert a contact into the database
		public long insertContact(String name,String email)
		{
			ContentValues initialValues=new ContentValues();
			initialValues.put(KEY_NAME,name);
			initialValues.put(KEY_EMAIL,email);
			return db.insert(DATABASE_TABLE,null,initialValues);
		}
		
		//deletes a particular contact
		public boolean deleteContact(long rowId)
		{
			return db.delete(DATABASE_TABLE,KEY_ROWID+"="+rowId,null)>0;
		}
		
		//retrieves all contacts
		public Cursor getAllContacts()
		{
			return db.query(DATABASE_TABLE,new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL},null,null,null,null,null);
		}
		
		//retrieves a particular contact
		public Cursor getContact(long rowId) throws SQLException
		{
			Cursor mCursor=db.query(true,DATABASE_TABLE,new String[] {KEY_ROWID,KEY_NAME,KEY_EMAIL},KEY_ROWID+"="+rowId,
					null,null,null,null,null);
			if(mCursor!=null)
			{
				mCursor.moveToFirst();
			}
			return mCursor;
		}
		
		//updates a contact
		public boolean updateContact(long rowId,String name,String email)
		{
			ContentValues args=new ContentValues();
			args.put(KEY_NAME,name);
			args.put(KEY_EMAIL,email);
			return db.update(DATABASE_TABLE,args,KEY_ROWID+"="+rowId,null);
		}
	}
}
Posted 4-Jun-13 6:04am
Comments
Richard MacCutchan at 4-Jun-13 12:07pm
   
Which line gives the error?
Sergey Alexandrovich Kryukov at 4-Jun-13 16:25pm
   
...and if Brady answers, we will need to ask what is he trying to achieve, because is the instance is not accessible, it won't be made accessible from static.
This is why I decided to give a very general answer without further detail... Not sure if it helps though, just wanted to give it a try...
—SA
Brady Bar at 4-Jun-13 23:57pm
   
it is showing errors at the where DBhelper and SQLiteDatabase db are being declared and also in the methods which use DBHelper and db objects.
Richard MacCutchan at 5-Jun-13 3:44am
   
That does not make sense, given the above listing. Please edit yor question and show the exact line of code where the error occurs, and the exact error message produced by the compiler.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No matter where the error is, this is just impossible, no matter what you do. Any static member, by definition, is the one which has no access to anything related to an instance. Naturally, it's accessed via the class, without a need to have any instance.
 
What to do? It depends on what you want to achieve. Perhaps you can turn a static member to instance one (remove "static"), or you don't actually need and access to the instance, than you can leave it static, don't address instance or turn something else "static" (this is usually worse than having all instance; the only fully save use of static is some purely functional function, those based only on parameters, return value, and stack). So, first, you should come to perfect understanding of static vs. instance member, then decide how you should design your code based on that understanding.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 4 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100