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);
}
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;
}
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.
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;
}
}
}
Bookmarks