Android sqlite更新行

2023-12-31

我试图更新表中的一行,但更新功能似乎没有响应。我的功能一切正常,还是我哪里出了问题?

public int editChild(int id, String name, String dob,
            int gender, double weight, double lenght, int color, int status) {
        ContentValues args = new ContentValues();
        args.put("name", name);
        args.put("dob", dob);
        args.put("weight", weight);
        args.put("lenght", lenght);
        args.put("color", color);
        args.put("status", status);
        return database.update(TABLE_CHILDREN, args, "id" + "='" + id
                + "'", null);
    }

现在我看到 logcat,抛出这些异常:

04-03 22:38:32.984: I/dalvikvm(8803): Uncaught exception thrown by finalizer (will be discarded):
04-03 22:38:32.984: I/dalvikvm(8803): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c70c20 on children that has not been deactivated or closed
04-03 22:38:32.984: I/dalvikvm(8803):   at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:32.984: I/dalvikvm(8803):   at dalvik.system.NativeStart.run(Native Method)
04-03 22:38:32.984: I/dalvikvm(8803): Uncaught exception thrown by finalizer (will be discarded):
04-03 22:38:32.984: I/dalvikvm(8803): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c702f8 on children that has not been deactivated or closed
04-03 22:38:32.984: I/dalvikvm(8803):   at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:32.984: I/dalvikvm(8803):   at dalvik.system.NativeStart.run(Native Method)
04-03 22:38:33.004: I/dalvikvm(8803): Uncaught exception thrown by finalizer (will be discarded):
04-03 22:38:33.024: I/dalvikvm(8803): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c2bc20 on children that has not been deactivated or closed
04-03 22:38:33.024: I/dalvikvm(8803):   at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:33.024: I/dalvikvm(8803):   at dalvik.system.NativeStart.run(Native Method)
04-03 22:38:33.064: I/dalvikvm(8803): Uncaught exception thrown by finalizer (will be discarded):
04-03 22:38:33.084: I/dalvikvm(8803): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c82e68 on children that has not been deactivated or closed
04-03 22:38:33.084: I/dalvikvm(8803):   at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:33.084: I/dalvikvm(8803):   at dalvik.system.NativeStart.run(Native Method)

数据库助手类:

package com.app;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String TABLE_SETTINGS = "settings";
    public static final String TABLE_LOCATIONS = "locations";
    public static final String TABLE_LOCATIONCATEGORIES = "locationcategories";
    public static final String TABLE_CATEGORIES = "categories";
    public static final String TABLE_ARTICLES = "articles";
    public static final String TABLE_DEF_CALENDAR = "DefCalendar";
    public static final String TABLE_USERS = "users";
    public static final String TABLE_CHILDREN = "children";
    public static final String TABLE_DIARY = "diary";
    public static final String DROP_TABLE = "drop table if exists";

    private static final String DATABASE_NAME = "BabyApp.db";
    private static final int DATABASE_VERSION = 1;
    private boolean isOpen = false;
    private Context context;
    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;

    // Database creation sql statements
    private static final String CREATE_TABLE_SETTINGS = "create table "
            + TABLE_SETTINGS
            + " ("
            + "id integer primary key autoincrement, value text, timestamp text);";

    private static final String CREATE_TABLE_LOCATIONS = "create table "
            + TABLE_LOCATIONS
            + " (id text primary key, categoryId integer, name text, "
            + "long numeric, lat numeric, description text, timestamp text, status integer);";

    private static final String CREATE_TABLE_LOCATIONCATEGORIES = "create table "
            + TABLE_LOCATIONCATEGORIES
            + " ("
            + "id integer primary key autoincrement, name text, timestamp text, status integer);";

    private static final String CREATE_TABLE_CATEGORIES = "create table "
            + TABLE_CATEGORIES
            + " ("
            + "id integer primary key autoincrement, parent integer, ageFrom integer, ageTO integer,"
            + " dateFrom text, dateTo text, timestamp text);";

    private static final String CREATE_TABLE_ARTICLES = "create table "
            + TABLE_ARTICLES
            + " ("
            + "id integer primary key autoincrement,parent integer not null, foreign key (parent) references "
            + TABLE_CATEGORIES + " (id), name text, ageFrom integer,"
            + " ageTo integer, dateFrom text, dateTo text, status integer);";

    private static final String CREATE_TABLE_DEF_CALENDAR = "create table "
            + TABLE_DEF_CALENDAR
            + " ("
            + "id integer primary key autoincrement, title text, ageFrom integer, ageTo integer,articleId integer, foreign key (articleId) references "
            + TABLE_ARTICLES + "(id), timestamp text, status integer)";

    private static final String CREATE_TABLE_USERS = "create table "
            + TABLE_USERS
            + " (id integer primary key autoincrement, email text, pass text, timestamp text);";

    private static final String CREATE_TABLE_CHILDREN = "create table "
            + TABLE_CHILDREN
            + " ("
            + "id integer primary key autoincrement, name text, gender integer, dob text, weight numeric, lenght numeric, color integer, status integer, timestamp text);";

    private static final String CREATE_TABLE_DIARY = "create table "
            + TABLE_DIARY
            + " ("
            + "id integer primary key autoincrement, foreign key(child) references"
            + TABLE_CHILDREN
            + " (id), date text, text text, timestamp text, status integer);";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SETTINGS);
        // db.execSQL(CREATE_TABLE_ARTICLES);
        db.execSQL(CREATE_TABLE_CATEGORIES);
        db.execSQL(CREATE_TABLE_CHILDREN);
        // db.execSQL(CREATE_TABLE_DEF_CALENDAR);
        // db.execSQL(CREATE_TABLE_DIARY);
        db.execSQL(CREATE_TABLE_LOCATIONCATEGORIES);
        db.execSQL(CREATE_TABLE_LOCATIONS);
        db.execSQL(CREATE_TABLE_USERS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(TABLE_SETTINGS);
        db.execSQL(TABLE_ARTICLES);
        db.execSQL(TABLE_CATEGORIES);
        db.execSQL(TABLE_CHILDREN);
        db.execSQL(TABLE_DEF_CALENDAR);
        db.execSQL(TABLE_DIARY);
        db.execSQL(TABLE_LOCATIONCATEGORIES);
        db.execSQL(TABLE_LOCATIONS);
        db.execSQL(TABLE_USERS);
        onCreate(db);
    }

    public DatabaseHelper open() throws SQLException {
        dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        isOpen = true;
        super.onOpen(db);
    }

    public boolean isOpen() {
        return isOpen;
    }

    public DatabaseHelper openToWrite() throws android.database.SQLException {

        dbHelper = new DatabaseHelper(context);

        database = dbHelper.getWritableDatabase();

        return this;

    }

    public void close() {
        database.close();
    }

    public boolean setDefaultBaby(int currentActiveId, int id) {
        ContentValues args = new ContentValues();
        args.put("status", "0");
        ContentValues newargs = new ContentValues();
        newargs.put("status", "1");
        return (database.update(TABLE_CHILDREN, args, "id" + "='"
                + currentActiveId + "'", null) > 0 && database.update(
                TABLE_CHILDREN, newargs, "id" + "='" + id + "'", null) > 0);

    }

    public int editChild(int id, String name, String dob, int gender,
            double weight, double lenght, int color, int status) {
        ContentValues args = new ContentValues();
        args.put("name", name);
        args.put("dob", dob);
        args.put("weight", weight);
        args.put("lenght", lenght);
        args.put("color", color);
        args.put("status", status);
        return database.update(TABLE_CHILDREN, args, "id" + "='" + id + "'",
                null);
    }

    public String getActiveChild(int id) {
        Cursor Activecur = database.query(true, TABLE_CHILDREN, null, "id = "
                + id, null, null, null, null, null);
        if (Activecur.moveToFirst()) {

            return Activecur.getString(Activecur.getColumnIndex("name"));
        } else
            return null;
    }

    public long createChild(String name, int gender, String dob, double weight,
            double lenght, int color, int status, String timestamp) {
        ContentValues initialValues = new ContentValues();
        initialValues.put("name", name);
        initialValues.put("gender", gender);
        initialValues.put("dob", dob);
        initialValues.put("weight", weight);
        initialValues.put("lenght", lenght);
        initialValues.put("color", color);
        Cursor Createcur = database.query(TABLE_CHILDREN, null, null, null,
                null, null, null);
        if (Createcur.getCount() == 0)
            initialValues.put("status", 1);
        else
            initialValues.put("status", 0);
        initialValues.put("timestamp", timestamp);// yyyy-MM-dd HH:mm:ss
        return database.insert(TABLE_CHILDREN, null, initialValues);
    }

    public boolean deleteChild(int id) {

        String where = "id" + "='" + id + "'";
        database.delete(TABLE_CHILDREN, where, null);
        return database.delete(TABLE_CHILDREN, where, null) > 0;
    }

    public ArrayList<Child> getAllChildren() {
        Cursor getAllCursor = database.query(TABLE_CHILDREN, null, null, null,
                null, null, null);
        ArrayList<Child> arr = new ArrayList<Child>();
        getAllCursor.moveToFirst();
        while (getAllCursor.isAfterLast() == false) {
            // name text, gender integer, dob text, weight numeric, lenght
            // numeric, colod integer, status integer, timestamp text);";
            arr.add(new Child(
                    getAllCursor.getInt(getAllCursor.getColumnIndex("id")),
                    getAllCursor.getString(getAllCursor.getColumnIndex("name")),
                    getAllCursor.getInt(getAllCursor.getColumnIndex("gender")),
                    getAllCursor.getString(getAllCursor.getColumnIndex("dob")),
                    getAllCursor.getString(getAllCursor
                            .getColumnIndex("timestamp")),
                    getAllCursor.getInt(getAllCursor.getColumnIndex("status")),
                    getAllCursor.getInt(getAllCursor.getColumnIndex("color")),
                    getAllCursor.getFloat(getAllCursor.getColumnIndex("weight")),
                    getAllCursor.getFloat(getAllCursor.getColumnIndex("lenght"))));
            getAllCursor.moveToNext();
        }
        // getAllCursor.close();
        return arr;
    }

    public int getDefaultBabyID() {
        String where = "status = 1";
        Cursor cur = database.query(true, TABLE_CHILDREN, null, where, null,
                null, null, null, null);
        int id;
        if (cur.moveToFirst())
            id = cur.getInt(cur.getColumnIndex("id"));
        else
            id = 0;
        cur.close();
        return id;
    }

    public int getId() {
        Cursor cur = database.query(true, TABLE_CHILDREN, null, null, null,
                null, null, null, null);
        cur.moveToLast();
        int id = cur.getInt(cur.getColumnIndex("id"));
        cur.close();
        return id;
    }

    public int getIdForName(String name) {
        String where = "name='" + name + "'";
        Cursor cur = database.query(true, TABLE_CHILDREN, null, where, null,
                null, null, null, null);
        cur.moveToLast();
        cur.close();
        return cur.getInt(cur.getColumnIndex("id"));
    }
}

试试这个代码:

这是在您的 DatabaseHelper 类中,

public void updateRow(long rowId, String name, String value1, String value2, String value3, String value4) {
        SQLiteDatabase db = this.getWritableDatabase();

    ContentValues args = new ContentValues();
    args.put("KEY1", name);
    args.put("KEY2", value1);
    args.put("KEY3", value2);
    args.put("KEY4", value3);
    args.put("KEY5", value4);

    db.update(TABLE_DETAILS, args, "id=" + rowId, null);
}

并在您想要更新行值的任何地方使用它。

db.updateRow(id, name, string1, string2, string3, string4);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android sqlite更新行 的相关文章

随机推荐

  • 如何在python中使用scrapy获取直接父节点?

    我是新来的scrapy 我想从网络上抓取一些数据 我得到了如下所示的html文档 dom style1 div class user info p class user name something in p tag p text data
  • 实体框架:多对多关系中的重复记录

    我有以下实体框架代码第一代码 创建表并插入数据 但是 Club 表中有重复的记录 我的操作是 使用俱乐部创建应用程序创建俱乐部 使用人员应用程序创建人员 如何避免重复录入 static void Main string args Datab
  • Linq Select 语句 - 不在的地方

    我正在尝试编写相当于以下内容的 LINQ 语句 select e EmployeeID EmployeeName e FirstName e LastName from Employees e where e EmployeeID not
  • 对 `search_as_you_type` ngram 子字段感到困惑

    我正在尝试将 键入时搜索 功能添加到 Elasticsearch 中名为email address 我的理解从文档 https www elastic co guide en elasticsearch reference 7 7 sear
  • 从本地到 Heroku 服务器的 SCP 文件

    我想将 config yml 文件从本地 django 应用程序目录复制到我的 heroku 服务器 但我不知道如何获取 电子邮件受保护 cdn cgi l email protectionHeroku 的格式 我尝试过运行 heroku
  • Android Room 按别名排序

    我想根据我创建的自定义别名来订购数据集 我尝试过 但它会导致语法错误 我究竟做错了什么 Code Query SELECT a b as ratio FROM dataset where my status myStatus order b
  • WKWebview注入cookie头导致重定向循环

    我试图将我单独获取的会话cookie注入到WKWebview请求中 结果证明这是相当痛苦的 我设法使用注入会话cookie这个解决方案 https stackoverflow com questions 26573137 can i set
  • PCM -> AAC(编码器) -> PCM(解码器)实时且正确优化

    我正在尝试实施 AudioRecord MIC gt PCM gt AAC Encoder AAC gt PCM Decode gt AudioTrack SPEAKER with MediaCodec在 Android 4 1 API16
  • 如何在MySQL中进行批量插入

    我有 1 多条记录需要输入到表中 在查询中执行此操作的最佳方法是什么 我应该创建一个循环并每次迭代插入一条记录吗 或者 还有更好的方法 来自MySQL手册 http dev mysql com doc refman 5 7 en inser
  • Azure 管理 REST API - “身份验证失败。‘授权’标头以无效格式提供。”

    我拼命尝试将 2 个经典存储帐户从旧的 MSDN 订阅移动到 MPN 订阅 但我一直遇到困难 因为仅通过 REST API 支持这些帐户的移动 我已按照此处的说明启用了 API https azure microsoft com en us
  • Eclipse 是否有排列类文件的功能?

    Eclipse 有很多功能 我想知道这个功能是否存在 或者是否存在任何捷径 我想将我的类数据排列到该流程中的变量 构造函数 方法中 从上到下 进一步细化我想按访问级别 pub private protected 和类型 void 或返回的方
  • 使用 GSON 获取 JSON 键名

    我有一个 JSON 数组 其中包含如下对象 bjones fname Betty lname Jones password ababab level manager 我的 User 类有一个用户名 需要使用 JSON 对象的密钥 我如何获取
  • 添加不属于模型一部分的自定义表单字段 (Django)

    我在管理网站上注册了一个模型 它的字段之一是长字符串表达式 我想将自定义表单字段添加到管理员中此模型的添加 更新页面 根据这些字段的值 我将构建长字符串表达式并将其保存在相关的模型字段中 我怎样才能做到这一点 我正在从符号构建数学或字符串表
  • 在elasticbeanstalk中设置NODE_ENV变量

    我创建了一个名为 elasticbeanstalk environment config其中包含以下内容 option settings option name NODE ENV value development 我还将 process
  • 具有多个可选参数的 Spring Data MongoDB AND/OR 查询

    我正在尝试执行具有两个以上可选参数的查询 但没有得到任何结果 对于2个参数我遵循了这个问题的答案spring data mongo 可选查询参数 https stackoverflow com questions 11613464 spri
  • 带有断路器的 Kafka Consumer,使用 Resilience4j 重试模式

    我需要一些帮助来了解如何使用 Spring boot Kafka Resilence4J 提出解决方案 以实现来自 Kafka Consumer 的微服务调用 假设如果微服务关闭 那么我需要使用断路器模式通知我的 Kafka 消费者停止获取
  • 使用 Solr 配置 Tika

    我正在寻找将丰富类型文档 Pdf Doc rtf txt 索引到 Solr 中 我找到了 Tika 作为解决方案 我在网上咆哮 但没有找到任何文档 链接来使其与 ExtractingRequestHandler 一起使用 任何人都可以提供通
  • 为什么对原型的编辑不起作用?

    我想向函数构造函数 类 的原型添加一个常量 但它返回为未定义 为什么 function myClass document ready function myClass prototype age 22 window alert myClas
  • 当在 QTableWidget 中拖动一行时,如何找出它被拖动的行索引 FROM 和 TO?

    我试图保持一些数组数据与 QTableWidget 的内容同步 我想启用拖放重新排序 在表中移动项目 而不是复制 但我不清楚当触发放置事件时 我如何找出该项目是从哪个索引拖动的 因此 我无法知道要在正在同步的列表中移动哪个对象 如何获取被拖
  • Android sqlite更新行

    我试图更新表中的一行 但更新功能似乎没有响应 我的功能一切正常 还是我哪里出了问题 public int editChild int id String name String dob int gender double weight do