首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更新我的数据库

如何更新我的数据库
EN

Stack Overflow用户
提问于 2012-01-21 07:05:45
回答 1查看 525关注 0票数 0

我已经创建了一个数据库,用于存储来自有互联网连接的internet.When的数据,我调用createEntry并将数据写入数据库。我的问题是,第二次使用有互联网,新的数据没有覆盖旧的,但创建新的entries.So,我认为解决方案将是更新我的数据库从第二次,用户有互联网连接,所以每次有相同数量的data.My问题是,我不知道如何做it.This是我的第一次尝试在SQLite和数据库。

这是我的createEntry:

代码语言:javascript
复制
public void createEntry(String title, String getagonistiki, String getskor,
            String getgipedo, String date, String getgoal1, String getgoal2,
            String teliko_skor) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(DBHelper.TITLE, title);
        cv.put(DBHelper.AGONISTIKI, getagonistiki);
        cv.put(DBHelper.SKOR, getskor);
        cv.put(DBHelper.GIPEDO, getgipedo);
        cv.put(DBHelper.DATE, date);
        cv.put(DBHelper.GOALA, getgoal1);
        cv.put(DBHelper.GOALB, getgoal2);
        cv.put(DBHelper.DESCRIPTION, teliko_skor);

        try {
            ourDatabase.insert("osfpDB", null, cv);
        } // ourDatabase.update("osfpDB",cv,DBHelper.ROWID,null);

        catch (Exception e) {
            Log.e("DB ERROR ON .INSERT", e.toString()); // prints the error
                                                        // message to the log
            e.printStackTrace(); // prints the stack trace to the log
        }
    }

这就是当有互联网连接时我所说的:

代码语言:javascript
复制
HotOrNot entry = new HotOrNot(agones.this);

            entry.open();

            entry.createEntry(msg.getTitle(), msg.getagonistiki(), msg
                    .getskor(), msg.getgipedo(), msg.getDate(),msg.getgoal1(),msg.getgoal2(),msg.getDescription());

            // entry.update(msg.getTitle(),msg.getagonistiki(),msg.getskor(),msg.getgipedo(),msg.getDate());

            entry.close();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-21 07:23:52

尝试此函数::

代码语言:javascript
复制
public void createEntry(String title, String getagonistiki, String getskor, String getgipedo, String date, String getgoal1, String getgoal2,
        String teliko_skor) {

    Cursor c = null;
    boolean isInserted = false;
    try {       
        c = ourDatabase.rawQuery("select "+DBHelper.TITLE+" from osfpDB", null);
        if(c != null){
            for(int i=0; i<c.getCount();i++){
                c.moveToPosition(i);
                if(c.getString(0).equals(title)){
                    Log.e("same title text means duplicate :",title+" : "+c.getString(0));
                    isInserted = true;
                    break;
                }
            }
        }

        ContentValues cv = new ContentValues();
        cv.put(DBHelper.TITLE, title);
        cv.put(DBHelper.AGONISTIKI, getagonistiki);
        cv.put(DBHelper.SKOR, getskor);
        cv.put(DBHelper.GIPEDO, getgipedo);
        cv.put(DBHelper.DATE, date);
        cv.put(DBHelper.GOALA, getgoal1);
        cv.put(DBHelper.GOALB, getgoal2);
        cv.put(DBHelper.DESCRIPTION, teliko_skor);

        Log.e("ourDatabase", "" + ourDatabase);

        if (ourDatabase == null) {
            ourDatabase = getWritableDatabase();
        }

        if(isInserted){
            ourDatabase.update("osfpDB", cv, null, null);
        }else{
            ourDatabase.insert("osfpDB", null, cv); 
        }
    } catch (Exception e) {
        Log.e("Exception in insert :", e.toString());
        e.printStackTrace();
    }
}

在您的问题中,我注意到您只想丢弃重复的记录,

为此,您不需要更新记录或不更改您的代码函数,只需将主键设置为您的表'title‘列-这将不允许插入重复记录。(如果您有其他查询,请告诉我)

参考:Naming conventions

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8948985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档