2012-04-04 59 views
3

我试图更新表中的一行,但更新函数似乎没有响应。一切都好,我的功能,或者我是我的地方错了?Android的SQLite更新行

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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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) 

的DatabaseHelper类:

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")); 
    } 
} 
+0

你可以显示创建表的代码,问题可以从那里也。 – Bhavin 2012-04-04 12:41:44

+0

是否确定在所有操作后关闭了游标或数据库? – 2012-04-04 12:46:10

+0

@imrankhan如何关闭游标即时关闭它们 – Darko 2012-04-04 12:51:06

回答

1

两件事。

1)您需要关闭光标。就我个人而言,我通过使用startManagingCursor(mYourCursor);而不是尝试和记住自己来执行该框架。

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

+1

“_id”是什么意思? – 2014-02-18 06:54:38

+0

只需使用“_id”来代替传入的“主键”(主键)。使用“id”,某些功能将无法工作。在你的代码中使用例如“..._ id integer primary key autoincrement ...” – 2015-09-03 11:45:00

0

尝试检查this。你似乎还没有关闭你的光标。

+0

问题已更新 – Darko 2012-04-04 12:35:43

0

您需要使用database.update(TABLE_CHILDREN,指定参数时, “ID =?”,新的String {将String.valueOf(ID)});

+0

您是否在getWriteableDatabase模式下打开了sqlitedb?在实际激发更新语句之前,先执行sqldb.getWriteableDatabase()。 – 2012-04-04 12:38:21

+0

类型SQLiteDatabase中的方法update(String,ContentValues,String,String [])不适用于参数(String,ContentValues,String,String) – Darko 2012-04-04 12:39:06

+0

最后一个参数应该是String [],而不仅仅是String。你现在试图运行的最新代码是什么? – 2012-04-04 12:49:43

6

试试这个代码:

这是你的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); 
1

您的功能editChild完全可以。

现在问题可能来自光标方。或关于数据库打开或关闭。