2013-03-20 123 views
0

我正在創建一個應用程序,其中數據只能插入數據庫中一次。問題是,每次我運行我的應用程序時,它一次又一次地插入數據。任何人都可以幫助我解決這個問題嗎?數據庫每次執行應用程序時都會更新

數據庫類

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

} 
} 
+0

您是否將數據插入到您的第一個被調用活動的onCreate()中? – staaar 2013-03-20 15:49:32

+0

是的我將數據插入冷杉稱爲活動 – user1922355 2013-03-20 15:52:40

+0

理想情況下,在您預先填充或插入數據之前,請查詢您的數據庫/表以查看記錄是否已存在。如果它存在,則跳過填充例程,如果它不填充數據。 – ApolloSoftware 2013-03-20 15:52:56

回答

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 
} 
+0

我試過他,但它顯示空指針異常我應該添加此代碼從類插入數據或數據庫類@sajmon – user1922355 2013-03-20 16:13:03

+0

@ user1922355 anwer更新,檢查出來。 – Sajmon 2013-03-20 16:15:52

+0

@ user1922355謝謝,你能接受答案嗎? :)謝謝 – Sajmon 2013-03-20 16:50:42

相關問題