数据库每次执行时都会更新

时间:2013-03-20 15:43:53

标签: android sqlite sqliteopenhelper

我正在创建一个应用程序,其中数据只需插入数据库一次。问题是,每次我运行我的应用程序时,它会一次又一次地插入数据。任何人都可以帮我解决这个问题吗?

数据库类

public class Database extends SQLiteOpenHelper{

private static final String DB_NAME = "Diseases.db";

private static final int DB_VERSION_NUMBER = 1;

public static final String DB_TABLE_NAME = "Diseaseslist";
private static final String  _ID="id";
public static final String COLUMN_1_NAME = "Name";
private static final String COLUMN_2_Symptom="Symptom";
private static final String COLUMN_3_Symptom1="Symptom1";
private static final String COLUMN_4_Symptom2="Symptom2";
private static final String COLUMN_5_Symptom3="Symptom3";
private static final String COLUMN_6_Discription="Discription";
private static final String COLUMN_7_Precaution="Precaution";
String[] str,str1,str2;

private static final String DB_CREATE_SCRIPT ="create table "
        + DB_TABLE_NAME + " (" + _ID
        + " integer primary key autoincrement, " + COLUMN_1_NAME
        + " , " + COLUMN_2_Symptom + " ," + COLUMN_3_Symptom1 +" ," 
        + COLUMN_4_Symptom2 +" ," + COLUMN_5_Symptom3 +" ," + COLUMN_6_Discription + " ," 
        + COLUMN_7_Precaution + " "+");";

private SQLiteDatabase sqliteDBInstance = null;


public Database(Context context) {
    super(context, DB_NAME, null, DB_VERSION_NUMBER);
}


@Override
public void onCreate(SQLiteDatabase sqliteDBInstance) {
    // TODO Auto-generated method stub
    Log.i("onCreate", "Creating the database...");
    sqliteDBInstance.execSQL(DB_CREATE_SCRIPT);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    sqliteDBInstance.execSQL("DROP TABLE IF EXISTS"+DB_TABLE_NAME);
    onCreate(sqliteDBInstance);
}
 public void openDB() throws SQLException
    {
        Log.i("openDB", "Checking sqliteDBInstance...");
        if(this.sqliteDBInstance == null)
        {
            Log.i("openDB", "Creating sqliteDBInstance...");
            this.sqliteDBInstance = this.getWritableDatabase();
        }
    }

    public void closeDB()
    {
        if(this.sqliteDBInstance != null)
        {
            if(this.sqliteDBInstance.isOpen())
                this.sqliteDBInstance.close();
        }
    }
    public long insertDiseases(String Name,String Symptom,String Symptom1,String Symptom2,String Symptom3,String Description, String Precaution)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_1_NAME, Name);
        contentValues.put(COLUMN_2_Symptom, Symptom);
        contentValues.put(COLUMN_3_Symptom1, Symptom1);
        contentValues.put(COLUMN_4_Symptom2, Symptom2);
        contentValues.put(COLUMN_5_Symptom3, Symptom3);
        contentValues.put(COLUMN_6_Discription, Description);
        contentValues.put(COLUMN_7_Precaution, Precaution);
        Log.i(this.toString() + " - insertCountry", "Inserting: " + Name);
        return this.sqliteDBInstance.insert(DB_TABLE_NAME, null, contentValues);
    }

    public boolean removeDiseases(String Name)
    {
        int result = this.sqliteDBInstance.delete(DB_TABLE_NAME, "Name='" + Name + "'", null);

        if(result > 0)
            return true;
        else
            return false;
    }

    public long updateCountry(String oldName, String newName)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_1_NAME, newName);
        return this.sqliteDBInstance.update(DB_TABLE_NAME, contentValues, "Name='" + oldName + "'", null);
    }

    public String[] getSymptom()
    {
        Cursor cursor = this.sqliteDBInstance.query(DB_TABLE_NAME, new String[] {COLUMN_2_Symptom}, null, null, null, null, null);
 Log.i(COLUMN_2_Symptom, "Symptom"+cursor);
        if(cursor.getCount() >0)
        {
             str = new String[cursor.getCount()];
            int i = 0;

            while (cursor.moveToNext())
            {
                 str[i] = cursor.getString(cursor.getColumnIndex(COLUMN_2_Symptom));
                 i++;
             }

            return str;
        }
        else
        {
            return new String[] {};
        }
    }


    public String[] getSymptom1(String ss) {

    Cursor curs=this.sqliteDBInstance.rawQuery("Select Symptom1 from Diseaseslist where Symptom='" + ss+ "'  ", null);
        if(curs.getCount()>0)
        {
            str1 = new String[curs.getCount()];
            int j= 0;
            if(curs.moveToFirst())
            {
            do
            {
                str1[j] = curs.getString(curs.getColumnIndex(COLUMN_3_Symptom1));
                j++;
            }while(curs.moveToNext());
            }
            return str1;
        }
        else
        {
        return new String[] {};
        }
    }
    public String[] getName(String s1,String s2){

        Cursor cur=sqliteDBInstance.rawQuery("Select Name from Diseaseslist where Symptom='"+s1+"' and Symptom1='" +s2+"'", null);
        Log.d("20", "21");
        if(cur.getCount()>0)
        {
            Log.d("22", "23");
            str2 = new String[cur.getCount()];
            Log.d("24","25");
            int k= 0;
            if(cur.moveToFirst())
            {
            do
            {
                Log.d("26", "27");
                str2[k] = cur.getString(cur.getColumnIndex(COLUMN_1_NAME));
                Log.d("26", str2[k]);
                System.out.println("gate name v "+str2[k]);
                k++;
            }while(cur.moveToNext());
            }
            return str2;
        }
        else
        {
            return new String[] {};
        }

    }


public Cursor getdetail(String dses){
    this.sqliteDBInstance = this.getReadableDatabase();
    return sqliteDBInstance.rawQuery("Select * from Diseaseslist where Name='" +dses+"'",null);

}
}

1 个答案:

答案 0 :(得分:1)

  

我正在创建一个应用程序,其中必须将数据插入到数据库中   只有一次 。问题是每次我运行我的应用程序时插入数据   一次又一次。任何人都可以帮我解决这个问题吗?

这是一个令人困惑的问题。您最有可能以onCreate()方法插入数据。我的第一个想法应该是你第一次将数据保存到数据库中,将一些布尔变量保存到SharedPreferences然后用条件包装你的代码:

<强>伪代码:

// saving value
SharedPreferences s = getSharedPreferences("com.example.someText", Context.MODE_PRIVATE);
s.edit().putBoolean("isInserted", true).commit();

// read value
SharedPreferences s = getSharedPreferences("com.example.someText", Context.MODE_PRIVATE);
boolean isInserted = s.getBoolean("isInserted", false); // second param is default value

另一种方法:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putBoolean("isInserted", true);

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isInserted = preference.getBoolean("isInserted", false);

然后创建简单条件:

if (!isInserted) {
   // perform inserting
}
else {
   // other work
}