升级时SQLite中的表重复

时间:2016-05-28 07:08:06

标签: android json sqlite duplicates android-sqlite

我是Android开发的新手 我创建了一个应用程序,使用JSON并存储在SQLite中,以便脱机使用数据 我可以存储到我的数据库,但每次我离线访问它时,该应用程序都显示一个重复的数据库。

我认为升级数据库的问题在于它不会丢弃表。

Myatabase

public class DBHandler extends SQLiteOpenHelper implements CityListener {
private static final int DATABASE_VERSION = 2;
private static final String DB_NAME = "CityDatabase.db";
private static final String TABLE_NAME = "dataku";
private static final String KEY_ID = "_id";
private static final String KEY_JUDUL = "_judul";
private static final String KEY_ISI = "_isi";

String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_JUDUL+" TEXT,"+KEY_ISI+" TEXT)";
String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

public DBHandler(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE);
    onCreate(db);
}

我的片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_posts_list, null);


    String url = "http://wisatake.com/test/dataku.php";

    feedListView= (ListView) v.findViewById(R.id.custom_list);
    feedListView.setVisibility(View.VISIBLE);
    handler = new DBHandler(getActivity());
    NetworkUtils utils = new NetworkUtils(getActivity());
    if(utils.isConnectingToInternet())
    {
        new DownloadFilesTask().execute(url);
    }
    else
    {
        ArrayList<FeedItem> feedList = handler.getAllCity();
        feedListView.setAdapter(new CustomListAdapter(getActivity(), feedList));
        feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = feedListView.getItemAtPosition(position);
                FeedItem newsData = (FeedItem) o;

                Intent intent = new Intent(getActivity(), FeedDetailsActivity.class);
                intent.putExtra("feed", newsData);
                startActivity(intent);
            }
        });
    }
    return v;
}

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。 我没有在db sqlite中添加主键(ID)。如果我不在sqlite中添加主键,它可能重复。