Android的SQLite的更新行

Android的SQLite的更新行

本文介绍了Android的SQLite的更新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在我的表更新一行,但更新的功能似乎没有响应。一切都ok了我的功能,或者说我我什么地方错了?

 公众诠释editChild(INT ID,字符串名称,字符串出生日期,
            INT性别,体重双,双lenght,诠释色彩,诠释状态){
        ContentValues​​的args =新ContentValues​​();
        args.put(姓名,名);
        args.put(DOB,DOB);
        args.put(权重,重);
        args.put(lenght,lenght);
        args.put(色,颜色);
        args.put(状态,状态);
        返回database.update(TABLE_CHILDREN,指定参数时,身份证+=+ ID
                +',NULL);
    }
 

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

  04-03 22:38:32.984:I / dalvikvm(8803):未捕获的异常被抛出的终结(将被丢弃):
04-03 22:38:32.984:I / dalvikvm(8803):Ljava /朗/ IllegalStateException异常;:最终化光标android.database.sqlite.SQLiteCursor@44c70c20对儿童尚未停用或关闭
04-03 22:38:32.984:I / dalvikvm(8803):在android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:32.984:I / dalvikvm(8803):在dalvik.system.NativeStart.run(本机方法)
04-03 22:38:32.984:I / dalvikvm(8803):未捕获的异常被抛出的终结(将被丢弃):
04-03 22:38:32.984:I / dalvikvm(8803):Ljava /朗/ IllegalStateException异常;:最终化光标android.database.sqlite.SQLiteCursor@44c702f8对儿童尚未停用或关闭
04-03 22:38:32.984:I / dalvikvm(8803):在android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:32.984:I / dalvikvm(8803):在dalvik.system.NativeStart.run(本机方法)
04-03 22:38:33.004:I / dalvikvm(8803):未捕获的异常被抛出的终结(将被丢弃):
04-03 22:38:33.024:I / dalvikvm(8803):Ljava /朗/ IllegalStateException异常;:最终化光标android.database.sqlite.SQLiteCursor@44c2bc20对儿童尚未停用或关闭
04-03 22:38:33.024:I / dalvikvm(8803):在android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:33.024:I / dalvikvm(8803):在dalvik.system.NativeStart.run(本机方法)
04-03 22:38:33.064:I / dalvikvm(8803):未捕获的异常被抛出的终结(将被丢弃):
04-03 22:38:33.084:I / dalvikvm(8803):Ljava /朗/ IllegalStateException异常;:最终化光标android.database.sqlite.SQLiteCursor@44c82e68对儿童尚未停用或关闭
04-03 22:38:33.084:I / dalvikvm(8803):在android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
04-03 22:38:33.084:I / dalvikvm(8803):在dalvik.system.NativeStart.run(本机方法)
 

在DatabaseHelper类:

 包com.app;

进口的java.util.ArrayList;

进口android.content.ContentValues​​;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;

公共类DatabaseHelper扩展SQLiteOpenHelper {

    公共静态最后弦乐TABLE_SETTINGS =设置;
    公共静态最后弦乐TABLE_LOCATIONS =位置;
    公共静态最后弦乐TABLE_LOCATIONCATEGORIES =locationcategories;
    公共静态最后弦乐TABLE_CATEGORIES =类;
    公共静态最后弦乐TABLE_ARTICLES =文章;
    公共静态最后弦乐TABLE_DEF_CALENDAR =DefCalendar;
    公共静态最后弦乐TABLE_USERS =用户;
    公共静态最后弦乐TABLE_CHILDREN =儿童;
    公共静态最后弦乐TABLE_DIARY =日记;
    公共静态最后弦乐DROP_TABLE =如果存在删除表;

    私有静态最后弦乐DATABASE_NAME =BabyApp.db;
    私有静态最终诠释DATABASE_VERSION = 1;
    私人布尔isOpen会= FALSE;
    私人上下文的背景下;
    私人SQLiteDatabase数据库;
    私人DatabaseHelper dbHelper;

    //数据库创建SQL语句
    私有静态最后弦乐CREATE_TABLE_SETTINGS =创建表
            + TABLE_SETTINGS
            +(
            +ID整数主键自动增量,价值的文字,时间戳文本);

    私有静态最后弦乐CREATE_TABLE_LOCATIONS =创建表
            + TABLE_LOCATIONS
            +(ID文本主键,的categoryId整数,名文
            +长的数字,纬度数值,说明文字,时间戳文字,状态整数);;

    私有静态最后弦乐CREATE_TABLE_LOCATIONCATEGORIES =创建表
            + TABLE_LOCATIONCATEGORIES
            +(
            +ID整数主键自动增量,名文,时间戳文字,状态整数);;

    私有静态最后弦乐CREATE_TABLE_CATEGORIES =创建表
            + TABLE_CATEGORIES
            +(
            +ID整数主键自动递增,母公司整数,ageFrom整数,ageTO整数,
            +dateFrom文字,dateTo文本,时间戳文本);

    私有静态最后弦乐CREATE_TABLE_ARTICLES =创建表
            + TABLE_ARTICLES
            +(
            +ID整数主键自动递增,母公司整数不为空,外键(母公司)引用
            + TABLE_CATEGORIES +(ID),名文,ageFrom整数,
            +ageTo整数,dateFrom文字,dateTo文字,状态整数);;

    私有静态最后弦乐CREATE_TABLE_DEF_CALENDAR =创建表
            + TABLE_DEF_CALENDAR
            +(
            +ID整数主键自动增量,标题文字,ageFrom整数,ageTo整数条款ArticleID整数,外键(条款ArticleID)引用
            + TABLE_ARTICLES +(ID),时间标记的文本,状态整数);

    私有静态最后弦乐CREATE_TABLE_USERS =创建表
            + TABLE_USERS
            +(ID整数主键自动增量,电子邮件文本,通过文字,时间戳文本);

    私有静态最后弦乐CREATE_TABLE_CHILDREN =创建表
            + TABLE_CHILDREN
            +(
            +ID整数主键自动增量,名文,性别整数,DOB文字,体重数字,lenght数字,颜色整,状态整数,时间戳文本);

    私有静态最后弦乐CREATE_TABLE_DIARY =创建表
            + TABLE_DIARY
            +(
            +ID整数主键自动增量,外键(子)引用
            + TABLE_CHILDREN
            +(ID),日期的文字,文本文字,时间戳文本,状态整数);;

    公共DatabaseHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        this.context =背景;
    }

    @覆盖
    公共无效的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);
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        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);
    }

    公共DatabaseHelper的open()抛出的SQLException {
        dbHelper =新DatabaseHelper(上下文);
        数据库= dbHelper.getWritableDatabase();
        回到这一点;
    }

    @覆盖
    公共无效的OnOpen(SQLiteDatabase DB){
        isOpen会= TRUE;
        super.onOpen(DB);
    }

    公共布尔isOpen()函数{
        返回isOpen会;
    }

    公共DatabaseHelper openToWrite()抛出android.database.SQLException {

        dbHelper =新DatabaseHelper(上下文);

        数据库= dbHelper.getWritableDatabase();

        回到这一点;

    }

    公共无效的close(){
        database.close();
    }

    公共布尔setDefaultBaby(INT currentActiveId,INT ID){
        ContentValues​​的args =新ContentValues​​();
        args.put(状态,0);
        ContentValues​​ newargs =新ContentValues​​();
        newargs.put(状态,1);
        返程(database.update(TABLE_CHILDREN,指定参数时,身份证+=
                + currentActiveId +',NULL)> 0安培;&安培; database.update(
                TABLE_CHILDREN,newargs,身份证+=+编号+',NULL)> 0);

    }

    公众诠释editChild(INT ID,字符串名称,字符串出生日期,性别INT,
            双重量,双lenght,诠释色彩,诠释状态){
        ContentValues​​的args =新ContentValues​​();
        args.put(姓名,名);
        args.put(DOB,DOB);
        args.put(权重,重);
        args.put(lenght,lenght);
        args.put(色,颜色);
        args.put(状态,状态);
        返回database.update(TABLE_CHILDREN,指定参数时,身份证+=+编号+',
                空值);
    }

    公共字符串getActiveChild(INT ID){
        光标Activecur = database.query(真,TABLE_CHILDREN,空,ID =
                +号,NULL,NULL,NULL,NULL,NULL);
        如果(Activecur.moveToFirst()){

            返回Activecur.getString(Activecur.getColumnIndex(名字));
        } 其他
            返回null;
    }

    众长createChild所(字符串名称,诠释性别,出生日期字符串,双重量,
            双lenght,诠释色彩,诠释状态,串时间戳){
        ContentValues​​ initialValues​​ =新ContentValues​​();
        initialValues​​.put(姓名,名);
        initialValues​​.put(性别,性别);
        initialValues​​.put(DOB,DOB);
        initialValues​​.put(权重,重);
        initialValues​​.put(lenght,lenght);
        initialValues​​.put(色,颜色);
        光标Createcur = database.query(TABLE_CHILDREN,NULL,NULL,NULL,
                NULL,NULL,NULL);
        如果(Createcur.getCount()== 0)
            initialValues​​.put(状态,1);
        其他
            initialValues​​.put(状态,0);
        initialValues​​.put(时间戳,时间戳); // YYYY-MM-DD HH:MM:SS
        返回database.insert(TABLE_CHILDREN,空,initialValues​​);
    }

    公共布尔deleteChild(INT ID){

        字符串,其中=ID+=+编号+';
        database.delete(TABLE_CHILDREN,其中,NULL);
        返回database.delete(TABLE_CHILDREN,其中,NULL)> 0;
    }

    公众的ArrayList<儿童> getAllChildren(){
        光标getAllCursor = database.query(TABLE_CHILDREN,NULL,NULL,NULL,
                NULL,NULL,NULL);
        ArrayList的<儿童> ARR =新的ArrayList<儿童>();
        getAllCursor.moveToFirst();
        而(getAllCursor.isAfterLast()== FALSE){
            // name文本,性别整数,DOB文字,体重数字,lenght
            //数字,colod整,状态整数,时间戳文本);
            arr.add(新的儿童(
                    getAllCursor.getInt(getAllCursor.getColumnIndex(ID)),
                    getAllCursor.getString(getAllCursor.getColumnIndex(名字)),
                    getAllCursor.getInt(getAllCursor.getColumnIndex(性别)),
                    getAllCursor.getString(getAllCursor.getColumnIndex(DOB)),
                    getAllCursor.getString(getAllCursor
                            .getColumnIndex(时间戳)),
                    getAllCursor.getInt(getAllCursor.getColumnIndex(状态)),
                    getAllCursor.getInt(getAllCursor.getColumnIndex(颜色)),
                    getAllCursor.getFloat(getAllCursor.getColumnIndex(权重)),
                    getAllCursor.getFloat(getAllCursor.getColumnIndex(lenght))));
            getAllCursor.moveToNext();
        }
        // getAllCursor.close();
        返回ARR;
    }

    公众诠释getDefaultBabyID(){
        字符串,其中=状态= 1;
        光标CUR = database.query(真,TABLE_CHILDREN,空,其中,空,
                NULL,NULL,NULL,NULL);
        INT ID;
        如果(cur.moveToFirst())
            ID = cur.getInt(cur.getColumnIndex(ID));
        其他
            的id = 0;
        cur.close();
        返回ID;
    }

    公众诠释的getId(){
        光标CUR = database.query(真,TABLE_CHILDREN,NULL,NULL,NULL,
                NULL,NULL,NULL,NULL);
        cur.moveToLast();
        INT ID = cur.getInt(cur.getColumnIndex(ID));
        cur.close();
        返回ID;
    }

    公众诠释getIdForName(字符串名称){
        字符串,其中=NAME ='+姓名+';
        光标CUR = database.query(真,TABLE_CHILDREN,空,其中,空,
                NULL,NULL,NULL,NULL);
        cur.moveToLast();
        cur.close();
        返回cur.getInt(cur.getColumnIndex(ID));
    }
}
 

解决方案

两件事情。

1)你需要关闭你的光标。就个人而言,我让framwork处理,通过使用 startManagingCursor(mYourCursor); ,而不是试图记住要做自己

2)Android的期待您的行ID为_id,所以你可能会尝试改变所有的表使用的。

Im trying to update a row in my table but the update function seems not responding. Is everything ok with my function, or i'm I somewhere wrong?

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);
    }

Now i saw logcat, throws these exceptions:

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)

The DatabaseHelper Class:

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"));
    }
}
解决方案

Two things.

1) You need to close your cursor. Personally, I let the framwork handle that by using startManagingCursor(mYourCursor); rather than having to try and remember to do it myself.

2) Android expects your row id to be "_id", so you might try changing all your tables to use that.

这篇关于Android的SQLite的更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:34