Problem inserting rows in SQLite database
I am trying to insert rows into my database, but nothing gets persisted. Here is my helper class:
package com.android.cancertrials;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
public DataBaseHelper(Context context, String name) {
super(context, name, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("the database in onCreate: "+db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onOpen(SQLiteDatabase db) {
System.out.println("the database: "+db);
this.db=db;
super.onOpen(db);
}
public void insertRecordBean(RecordBean recordbean){
ContentValues content=new ContentValues();
content.put("TrialPhase", AdvanceSearchTab2.phaseOptions.toString());
content.put("TrialStatus", AdvanceSearchTab2.typeOptions.toString());
content.put("StudyType", AdvanceSearchTab2.statusOptions.toString());
content.put("ContactType", AdvanceSearchTab2.contactSpinner.toString());
content.put("MileRange", AdvanceSearchTab2.rangeSpinner.toString());
content.put("Location", AdvanceSearchTab2.locationSpinner.toString());
db.insert("tblClinicalTrial", null, content);
}
/*public void insertRecordResult(RecordBean recordbean){
ContentValues content1=new ContentValues();
content1.put("NCTID", recordbean.getNctId());
content1.put("Title", recordbean.getTitle());
content1.put("TrialPhase", recordbean.getTrialPhase());
content1.put("TrialStaus", recordbean.getTrialStatus());
content1.put("Summary", recordbean.getSummary());
content1.put("Conditions", recordbean.getConditions());
content1.put("Interventions", recordbean.getIntervention());
content1.put("Description", recordbean.getDescription());
content1.put("SponserName", recordbean.getSponsor());
content1.put("Inclusion", recordbean.getInclusionCriteria());
content1.put("Exclusion", recordbean.getExclusionCriteria());
content1.put("StudyPop", recordbean.getStudyPop());
content1.put("MinAge", recordbean.getMinimumAge());
content1.put("MaxAge", recordbean.getMa开发者_运维技巧ximumAge());
content1.put("Gender", recordbean.getGender());
content1.put("Hvolunteer", recordbean.getHealthyVolunteers());
content1.put("EnrollmentType", recordbean.getEnrollment());
content1.put("EnrollmentNum", recordbean.getEnrollment());
content1.put("VerifiedOn", recordbean.getVerifiedOn());
db.insert("tblResultItem", null, content1);
}*/
public void insertRecordBookmark(RecordBean recordbean){
ContentValues content2=new ContentValues();
content2.put("NCTID", recordbean.getNctId());
content2.put("Title", recordbean.getTitle());
content2.put("TrialPhase", recordbean.getTrialPhase());
content2.put("TrialStatus", recordbean.getTrialStatus());
content2.put("Summary", recordbean.getSummary());
content2.put("Conditions", recordbean.getConditions());
content2.put("Interventions", recordbean.getIntervention());
content2.put("Description", recordbean.getDescription());
content2.put("SponserName", recordbean.getSponsor());
content2.put("Inclusion", recordbean.getInclusionCriteria());
content2.put("Exclusion", recordbean.getExclusionCriteria());
content2.put("StudyPop", recordbean.getStudyPop());
content2.put("MinAge", recordbean.getMinimumAge());
content2.put("MaxAge", recordbean.getMaximumAge());
content2.put("Gender", recordbean.getGender());
content2.put("Hvolunteer", recordbean.getHealthyVolunteers());
content2.put("EnrollmentType", recordbean.getEnrollment());
content2.put("EnrollmentNum", recordbean.getEnrollment());
content2.put("VerifiedOn", recordbean.getVerifiedOn());
content2.put("UpdatedOn", recordbean.getUpdatedOn());
content2.put("LocationName", recordbean.getLocationName());
content2.put("LocationStatus", recordbean.getLocationStatus());
content2.put("City", recordbean.getCity());
content2.put("State", recordbean.getState());
content2.put("Country", recordbean.getCountry());
content2.put("Distance", recordbean.getLocationDist());
content2.put("Long", recordbean.getLongitude());
content2.put("lat", recordbean.getLatitude());
content2.put("Url", recordbean.getSourceUrl());
content2.put("Location_id", recordbean.getLocationId());
content2.put("Zip_code", recordbean.getZip());
db.insert("tblBookMarkItem", null, content2);
}
public Cursor getBookMarks(){
String[] columns={"_id","Title","Gender","TrialPhase","Zip_code","Location_id","NCTID"};
return db.query("tblBookMarkItem", columns, null, null, null, null, null);
}
public Cursor getRecent(){
String[] columns={"SearchString","ProfileDate","_id","years","Gender","TrialPhase","StudyType","TrialStatus","ContactType","MileRange","LocationZip","UrlParameter"};
return db.query("tblProfile", columns, null, null, null, null, null);
}
public void insertRecordProfile(String search,String url){
ContentValues content4=new ContentValues();
GregorianCalendar c=new GregorianCalendar();
content4.put("ProfileDate",c.get(GregorianCalendar.DATE)+"/"+(c.get(GregorianCalendar.MONTH)+1)+"/"+ c.get(GregorianCalendar.YEAR));
// content4.put("years", age);
//content3.put("ProfileType", AdvanceSearchTab2.typeOptions.toString());
//content4.put("months", AdvanceSearchTab1.ageEdit.getText().toString());
content4.put("SearchString", search);
// content4.put("Gender", gender);
// content4.put("TrialPhase", phase);
// content4.put("StudyType", study);
// content4.put("TrialStatus", status);
// content4.put("ContactType", contact);
// content4.put("MileRange", mile);
// content4.put("LocationZip", zip);
// //content4.put("searchString", AdvanceSearchTab1.edit_se.toString());
content4.put("UrlParameter", url);
db.insert("tblProfile", null, content4);
}
public void insertRecordMedical(RecordBean recordbean){
ContentValues content3=new ContentValues();
content3.put("years", AdvanceSearchTab1.ageEdit.toString());
content3.put("months", AdvanceSearchTab1.ageEdit.toString());
content3.put("searchString", AdvanceSearchTab1.edit_se.toString());
content3.put("Gender", AdvanceSearchTab1.hubSpinner.toString());
db.insert("tblMedicalProfile", null, content3);
}
public void deleteRecord(int id){
//_id=1;
db.delete("tblBookMarkItem", "_id="+id, null);
}
public void deleteRecent(int id){
//_id=1;
db.delete("tblProfile", "_id="+id, null);
}
private SQLiteDatabase db;
}
When I display it in my ListAdapter, it appears fine. As soon as I kill my application. My changes are not persisted. Why is this?
Where do you make your database? Also, you should be referencing aspects of your database with defined constants.
I don't see in your post where it is that you create your database. For example, in an app I have (link to the full file: http://code.devminded.com/score-it/src/56ac2292cfa3/src/com/devminded/scoreit/ScoreDbAdapter.java ) you can see my database class as such:
public class ScoreDbAdapter {
public static final String TAG = "com.devminded.scoreit.ScoreDbAdapter";
/**
* Keys for the player table. ID is incrementing value to
* reference the players.
*/
public static final String KEY_PLAYERS_ID = "_id";
public static final String KEY_PLAYERS_NAME = "name";
public static final String KEY_PLAYERS_ICON = "icon";
public static final String KEY_PLAYERS_SCORE = "score";
public static final String KEY_PLAYERS_ORDER = "turn";
/**
* Keys for the History table. Sequence is an increase value. Their order
* is the order the transactions were made.
*/
public static final String KEY_HISTORY_ID = "_id";
public static final String KEY_HISTORY_PLAYER = "player";
public static final String KEY_HISTORY_VALUE = "value";
public static final String KEY_HISTORY_NOTE = "note";
public static final String DATABASE_NAME = "scoreitdb";
public static final String TABLE_PLAYERS = "players";
public static final String TABLE_HISTORY = "history";
public static final String TRIGGER_UPDATE_NEW_SCORE = "update_new_score";
public static final String TRIGGER_UPDATE_SCORE = "update_score";
public static final String TRIGGER_DELETE_SCORE = "delete_score";
public static final String TRIGGER_DELETE_HISTORY = "delete_history";
public static final int DATABASE_VERSION = 25;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private int mNumPlayers;
private static final String CREATE_TABLE_PLAYERS =
"create table " + TABLE_PLAYERS + " (" + KEY_PLAYERS_ID + " integer primary key autoincrement, "
+ KEY_PLAYERS_NAME + " text not null, " + KEY_PLAYERS_ICON + " long not null, " + KEY_PLAYERS_SCORE + " long not null, "
+ KEY_PLAYERS_ORDER + " long not null);";
private static final String CREATE_TABLE_HISTORY =
"create table " + TABLE_HISTORY + " (" + KEY_HISTORY_ID + " integer primary key autoincrement, "
+ KEY_HISTORY_PLAYER + " integer not null, " + KEY_HISTORY_NOTE + " text not null, "
+ KEY_HISTORY_VALUE + " integer not null);";
private static final String CREATE_TRIGGER_NEW_SCORE =
"create trigger " + TRIGGER_UPDATE_NEW_SCORE + " after insert on " + TABLE_HISTORY
+ " begin"
+ " update " + TABLE_PLAYERS + " set " + KEY_PLAYERS_SCORE + " = " + KEY_PLAYERS_SCORE + " + new." + KEY_HISTORY_VALUE + " where " + KEY_PLAYERS_ID + " = new. " + KEY_HISTORY_PLAYER + ";"
+ " end;";
private static final String CREATE_TRIGGER_UPDATE_SCORE =
"create trigger " + TRIGGER_UPDATE_SCORE + " after update on " + TABLE_HISTORY
+ " begin"
+ " update " + TABLE_PLAYERS + " set " + KEY_PLAYERS_SCORE + " = " + KEY_PLAYERS_SCORE + " + (new." + KEY_HISTORY_VALUE + " - old." + KEY_HISTORY_VALUE + ")"
+ " where " + KEY_PLAYERS_ID + " = new. " + KEY_HISTORY_PLAYER + ";"
+ " end;";
private static final String CREATE_TRIGGER_DELETE_SCORE =
"create trigger " + TRIGGER_DELETE_SCORE + " after delete on " + TABLE_HISTORY
+ " begin"
+ " update " + TABLE_PLAYERS + " set " + KEY_PLAYERS_SCORE + " = " + KEY_PLAYERS_SCORE + " - old." + KEY_HISTORY_VALUE
+ " where " + KEY_PLAYERS_ID + " = old." + KEY_HISTORY_PLAYER + ";"
+ " end;";
private static final String CREATE_TRIGGER_DELETE_HISTORY =
"create trigger " + TRIGGER_DELETE_HISTORY + " delete on " + TABLE_PLAYERS
+ " begin"
+ " delete from " + TABLE_HISTORY + " where " + KEY_HISTORY_PLAYER + " = old." + KEY_PLAYERS_ID + ";"
+ " end;";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_PLAYERS);
db.execSQL(CREATE_TABLE_HISTORY);
db.execSQL(CREATE_TRIGGER_NEW_SCORE);
db.execSQL(CREATE_TRIGGER_DELETE_HISTORY);
db.execSQL(CREATE_TRIGGER_UPDATE_SCORE);
db.execSQL(CREATE_TRIGGER_DELETE_SCORE);
}
@Override
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 TRIGGER IF EXISTS " + TRIGGER_DELETE_HISTORY);
db.execSQL("DROP TRIGGER IF EXISTS " + TRIGGER_UPDATE_NEW_SCORE);
db.execSQL("DROP TRIGGER IF EXISTS " + TRIGGER_UPDATE_SCORE);
db.execSQL("DROP TRIGGER IF EXISTS " + TRIGGER_DELETE_SCORE);
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PLAYERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
onCreate(db);
}
}
public ScoreDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public ScoreDbAdapter open() throws SQLException {
if (mDbHelper == null) {
mDbHelper = new DatabaseHelper(mCtx);
}
mDb = mDbHelper.getWritableDatabase();
countPlayers();
return this;
}
public void close() {
mDbHelper.close();
}
public long createPlayer(String name, int icon, long score) {
countPlayers();
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PLAYERS_NAME, name);
initialValues.put(KEY_PLAYERS_ICON, icon);
initialValues.put(KEY_PLAYERS_SCORE, score);
initialValues.put(KEY_PLAYERS_ORDER, mNumPlayers);
return mDb.insert(TABLE_PLAYERS, null, initialValues);
}
In create player you can see I am using the same functionality for adding that you are. I do not however see where you are creating the database. Can you present to us that similar functionality? Something is obviously amiss and we seem to be missing some aspect of the required information still.
Edit: Sorry, apparently my source somehow got a mixture of tabs and spaces in it, I guess I messed up an eclipse setting.
精彩评论