简体   繁体   English

Android SQLite:如何更新表

[英]Android SQLite: How to update table

Can anyone confirm whether my approach to updating the database is correct? 谁能确认我更新数据库的方法是否正确? The code is called, no errors are produced, however the database is not updated. 该代码被调用,不会产生任何错误,但是不会更新数据库。 I really would appreciate some help, been working on this for days and days. 一直以来,我都非常感谢您的帮助。

My update code: 我的更新代码:

public int updateContact(CountElement counter) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(TITLE, counter.getTitle());
    values.put(DESC, counter.getDescription());
    values.put(COUNT, counter.getCounterValue());

    Bitmap bmp = counter.getIcon();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    values.put(ICON, byteArray);

    values.put(LabelColor, counter.getLabelColor());

    // updating row
    return db.update(TABLE_NAME, values, KEY_ID + " = ?",
            new String[] { String.valueOf(counter.getID()) });
}

How I add the CountElement: 我如何添加CountElement:

void addContact(CountElement counter) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(TITLE, counter.getTitle()); // Counter title
    values.put(DESC, counter.getDescription()); // Counter desc
    values.put(COUNT, counter.getCounterValue());

    Bitmap bmp = counter.getIcon();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    values.put(ICON, byteArray);

    values.put(C, counter.getLabelColor());

    // Inserting Row
    db.insert(TABLE_NAME, null, values);
    db.close(); // Closing database connection
}

My CountElement class: 我的CountElement类:

public class CountElement {
    private String title;
    private String description;
    public int counterValue;
    private Bitmap icon;
    private int labelColorInt;
    private int _id;

    public CountElement(int id, String title, String description,
            int counterValue, Bitmap iconID, int labelColorInt) {
        this._id = id;
        this.title = title;
        this.description = description;
        this.counterValue = counterValue;
        this.icon = iconID;
        this.labelColorInt = labelColorInt;
    }

    public CountElement(String title, String description, int counterValue,
            Bitmap iconID, int labelColorInt) {
        this.title = title;
        this.description = description;
        this.counterValue = counterValue;
        this.icon = iconID;
        this.labelColorInt = labelColorInt;
    }

    public CountElement() {
    }

    // The getters and setters:
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    // getting ID
    public int getID() {
        return this._id;
    }

    // setting id
    public void setID(int id) {
        this._id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getCounterValue() {
        return counterValue;
    }

    public void setCounterValue(int counterValue) {
        this.counterValue = counterValue;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public int getLabelColor() {
        return labelColorInt;
    }

    public void setLabelColor(int labelColorInt) {
        this.labelColorInt = labelColorInt;
    }
}

Any ideas? 有任何想法吗?

Much appreciated. 非常感激。 Thanks! 谢谢!

The problem is that you forgot to close the database after you finish updating it, your update contact should be like this: 问题是您在完成更新后忘了关闭数据库,更新联系人应如下所示:

public int updateContact(CountElement counter) {
    SQLiteDatabase db = this.getWritableDatabase();

    ...

    // updating row
    int ret = db.update(TABLE_NAME, values, KEY_ID + " = ?",
            new String[] { String.valueOf(counter.getID()) });
    db.close();
    return ret;
}

The easiest way to do operations on Sqlite 在Sqlite上进行操作的最简单方法

use GREENDAO OR ORMSQLITE 使用GREENDAO或ORMSQLITE

for GREENDAO you have green dao generator to generate tables and db easily 对于GREENDAO,您具有green dao生成器,可轻松生成表和数据库

have you try the insert method? 您尝试过insert方法吗? is it works?as @Krypton said you forget to close the db. 如@Krypton所说,您忘记关闭数据库了。 you can debug with raw sql query string in an sqlite client like sqlitemanager,and find if it's work. 您可以在sqlitemanager之类的sqlite客户端中使用原始sql查询字符串进行调试,并确定其是否有效。

Assuming the id field is an auto-generater primary key field, you need to capture the generated value on insert. 假设id字段是自动生成器的主键字段,则需要在插入时捕获生成的值。 In addContact() , replace addContact() ,替换

db.insert(TABLE_NAME, null, values);

with something like 用类似的东西

long id = db.insert(TABLE_NAME, null, values);
counter.setID(id);

Otherwise the id comes out as default value 0 and it won't match any rows to update in the database. 否则,id将作为默认值0出现,它将不匹配数据库中要更新的任何行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM