Click here to Skip to main content
11,931,104 members (58,333 online)
Rate this:
Please Sign up or sign in to vote.
See more: Mobile Android
//The code for 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)
		DBHelper=new DatabaseHelper(context);
	private static class DatabaseHelper extends SQLiteOpenHelper
		DatabaseHelper(Context context)
		public void onCreate(SQLiteDatabase db)
			catch(SQLException e)
		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");
		//opens the database
		public DBAdapter open() throws SQLException
			return this;
		//closes the database
		public void close()
		//insert a contact into the database
		public long insertContact(String name,String email)
			ContentValues initialValues=new ContentValues();
			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,
			return mCursor;
		//updates a contact
		public boolean updateContact(long rowId,String name,String email)
			ContentValues args=new ContentValues();
			return db.update(DATABASE_TABLE,args,KEY_ROWID+"="+rowId,null);
Posted 4-Jun-13 6:04am
Richard MacCutchan 4-Jun-13 12:07pm
Which line gives the error?
...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...
Brady Bar 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 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
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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 4 Jun 2013
Copyright © CodeProject, 1999-2015
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