代码之家  ›  专栏  ›  技术社区  ›  Sheehan Alam

在SQLite数据库中插入行时出现问题

  •  1
  • Sheehan Alam  · 技术社区  · 15 年前

    我试图在数据库中插入行,但没有任何内容被持久化。这是我的助手课程:

        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中显示它时,它看起来很好。一旦我终止我的申请。我的更改没有持久化。这是为什么?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Thomas    15 年前

    你在哪里建立数据库?此外,您应该使用定义的常量引用数据库的各个方面。

    我在你的帖子中没有看到你创建数据库的地方。例如,在我拥有的应用程序中(指向完整文件的链接: 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);
    } 
    

    在create player中,你可以看到我正在使用与你相同的功能添加。不过,我不知道您在哪里创建数据库。你能给我们介绍一下类似的功能吗?显然有些地方不对劲,而且我们似乎仍然缺少所需信息的某些方面。

    编辑:抱歉,显然我的源代码中混合了标签和空格,我想我弄乱了eclipse的设置。