2010-11-19 65 views
1

我想將行插入到我的數據庫中,但沒有任何東西會持久化。這裏是我的助手類:在SQLite數據庫中插入行的問題

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.getMaximumAge()); 
     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; 

} 

當我在ListAdapter中顯示它時,它顯示正常。只要我殺了我的應用程序。我的更改不會持續。爲什麼是這樣?

+0

顯示如何創建數據庫的代碼。 – dhaag23 2010-11-19 03:58:12

+0

已更新爲顯示所有代碼 – 2010-11-19 04:00:03

+0

您的'public void onCreate(SQLiteDatabase db)'函數是完全空的。看看你是如何在數據庫中實際創建表格會很有趣...... – Nailuj 2010-11-19 12:40:55

回答

1

你在哪裏製作數據庫?另外,您應該使用定義的常量引用數據庫的各個方面。

我在您的文章中沒有看到您創建數據庫的位置。例如,在應用程序我有(鏈接到完整的文件:http://code.devminded.com/score-it/src/56ac2292cfa3/src/com/devminded/scoreit/ScoreDbAdapter.java),你可以看到我的數據庫類爲這樣:

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

在創建播放器,你可以看到我使用的是相同的功能添加,你是。然而,我不知道你在哪裏創建數據庫。你能向我們介紹類似的功能嗎?有些東西顯然是不對的,我們似乎仍然缺少所需信息的某些方面。

編輯:對不起,顯然我的源以某種方式得到了標籤和空格的混合,我想我搞砸了一個eclipse設置。