我正在创建一个应用程序,其中数据只需插入一次数据库。问题是每次我运行我的应用程序时,它都会一次又一次地插入数据。谁能帮我解决这个问题?
数据库类
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);
}
}