0

My project include db sqlite. It add data in db when it runs everytime. Here is code of my project. It adds information about pharmacy to the list. When it runs in device, it add this information ever time when it run. It must to stop adding information after one time. How can I do this?

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
import static com.ahmety.eczanezonguldak.Sbtalapli.*;

public class Alapli extends Activity { 
private Vtalapli data;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alapli);

data = new Vtalapli(this);
try{addData("","","","");
addData("17.06.2015","Pharmacy Name ","pharmacy addres","pharmacy tel no");   
Cursor cursor = getData();
showData(cursor);
}
finally{
data.close();
}
}

private void addData(String date, String pharmacy, String address, String tel){

SQLiteDatabase db = data.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATE, date);
values.put(PH,pharmacy);
values.put(ADDRESS,address);
values.put(TEL,tel);
db.insertOrThrow(TABLE_ALAPLI, null, values);
db.isDbLockedByCurrentThread();
}

private static String[] SELECT = {DATE, PH, ADDRESS,TEL};
private Cursor getData(){
SQLiteDatabase db = data.getReadableDatabase();
Cursor cursor = db.query(TABLE_ALAPLI, SELECT, null, null, null, null, null);

startManagingCursor(cursor);
return cursor;
}

private void showData(Cursor cursor){
StringBuilder builder = new StringBuilder("                                                              Nöbetçi Eczane/Alapli\n\n\n "
    + "  Tarih                      Eczane                                            Addres                                           Tel\n");

while(cursor.moveToNext()){

String date = cursor.getString((cursor.getColumnIndex(DATE)));
String ph = cursor.getString((cursor.getColumnIndex(PH)));
String address = cursor.getString((cursor.getColumnIndex(ADDRESS)));
String tel = cursor.getString((cursor.getColumnIndex(TEL)));

builder.append(date).append("    ");
builder.append(ph).append("  ");
builder.append(address).append("  ");
builder.append(tel).append("\n");
}
TextView text = (TextView)findViewById(R.id.textalapli);
text.setText(builder);
}
} 
Ahmet Yılmaz
  • 323
  • 3
  • 12

3 Answers3

2

You can use SharedPreferences to save a boolean value to check if it is the first time you run the app or not.

There are some examples here: Android Shared preferences example

Community
  • 1
  • 1
albertoqa
  • 475
  • 3
  • 9
0

You have addData running in onCreate. onCreate runs every time the class is initialized. Move addData outside of that method to wherever you want it to run instead of running in onCreate all the time.

0

What you could do is only insert the data just after you create the table in your implementation of SQLiteOpenHelper:

public class SomeDatabase extends SQLiteOpenHelper{
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE <MYTABLE> ...");
        sqLiteDatabase.execSQL("INSERT INTO <MYTABLE> ...");
    }
}

Then you can take out this line of code in your Activities onCreate method:

addData("17.06.2015","Pharmacy Name ","pharmacy addres","pharmacy tel no"); 

You will have to either clear the app data, remove the app or manually delete all the existing instances of the duplicated data for this to work.

Just a side note - please format (indent) your code when posting - it makes it much easier to help you.

Quintin Balsdon
  • 4,546
  • 8
  • 45
  • 87