重复数据发布的Andr​​oid源码 [英] Duplicate data issue android sqlite

查看:119
本文介绍了重复数据发布的Andr​​oid源码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序中,人们向其他人给的分数。对于这一点,我想安装的应用程序中插入一些人(比如5)数据库的名称。现在,当我运行应用程序第一次一切顺利。但是,当我运行它第二次,该数据再次插入使其10,即重复数据。现在的问题是,每当我跑这个活动,它增加的数据。该如何解决这个问题?
这里是我的code:

  @TargetApi(Build.VERSION_ codeS.HONEYCOMB)
     公共类游戏扩展ActionBarActivity {
     //有些code -----------@覆盖
保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.gameplay);
    //有些code ----    DB = openOrCreateDatabase(MyGameData,MODE_PRIVATE,NULL);    尝试{
        db.execSQL(CREATE TABLE教师(_id整数主键自动递增,名称VARCHAR(30),性别VARCHAR(2),分数整数));
        Toast.makeText(这一点,表创建Toast.LENGTH_SHORT).show();
    }    赶上(例外五){
        Toast.makeText(这一点,异常1,Toast.LENGTH_SHORT).show();
        Log.i(创建表,e.getMessage());
    }    //插入值表-------
    字符串str_name [] = {阿朗,dhanraj,德codeR,sanjana};
    串str_gender [] = {M,M,M,F};    ContentValues​​ CV =新ContentValues​​();    的for(int i = 0; I< str_name.length;我++){        cv.put(姓名,str_name [I]);
        cv.put(性别,str_gender [I]);
        db.insert(教师,空,CV);    }   //有些code ------


解决方案

您应该有一个包含数据库管理和生命周期的一些方法,如

SQLiteOpenHelper

 的onCreate(SQLiteDatabase DB)//首次创建数据库时调用。
onDowngrade(SQLiteDatabase分贝,INT oldVersion,诠释NEWVERSION)//当数据库需要被降级调用。
onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释NEWVERSION)//当数据库需要升级调用。

和其他的,你可以在这里看看:的http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

按照本教程应该让你的好路径上有关使用该助手:
http://developer.android.com/training/basics/data-storage /databases.html

我的建议是创造一些像Querys将填充和创作应该是有太多的表后执行它们的人。这样,您的应用程序只会让那些插在第一时间,并让他们即使你更新你的数据库(如果你不说出别的东西上onUpgrade()方法)。

这应该解决您的问题。

I am creating an app in which people give scores to other persons. For that, I want to insert names of some person (say 5) to a database during installation of the app. Now the when I run the app first time everything goes fine. But when I run it second time, the data are inserted again making it 10, i.e. duplicate data. The problem is whenever I run this activity, it adds the data. What is the solution to this problem? Here's my code:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
     public class GamePlay extends ActionBarActivity {
     //Some code-----------

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameplay);
    //Some code----

    db=openOrCreateDatabase("MyGameData", MODE_PRIVATE, null);

    try{
        db.execSQL("create table teachers(_id integer primary key autoincrement , name varchar(30) , gender varchar(2) , score integer)");
        Toast.makeText(this, "Table created", Toast.LENGTH_SHORT).show();
    }

    catch(Exception e){
        Toast.makeText(this, "Exception 1", Toast.LENGTH_SHORT).show();
        Log.i("Create table", e.getMessage());
    }

    // Insert values in table-------


    String str_name[]={"arun", "dhanraj", "decoder", "sanjana"};
    String str_gender[]={"m", "m", "m", "f"};

    ContentValues cv = new ContentValues();

    for(int i=0; i<str_name.length; i++){

        cv.put("name", str_name[i]);
        cv.put("gender", str_gender[i]);
        db.insert("teachers", null, cv);

    }

   //Some code------

解决方案

You should have a SQLiteOpenHelper class which contains some methods for database management and lifecycle like

onCreate(SQLiteDatabase db) // Called when the database is created for the first time.
onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) //Called when the database needs to be downgraded.
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) // Called when the database needs to be upgraded.

And other ones, you can have a look here: http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Following this tutorial should keep you on the good path about using this helper: http://developer.android.com/training/basics/data-storage/databases.html

My advice is to create some Querys like the ones that will populate and execute them after the creation of the Tables which should be there too. This way, your app will only make those inserts the first time, and keep them even if you update your database (if you don't state something else on the onUpgrade() method).

This should fix your problem.

这篇关于重复数据发布的Andr​​oid源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆