有趣的错误与我的数据库 [英] funny error with my database

查看:148
本文介绍了有趣的错误与我的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用是联系人的书,终于一切都工作正常,但是当我,用户,添加新的联系人,我得到强制关闭..但应用程序没有关闭,而是刚刚重启!当我检查联系人列表中我看到了我所要补充的是真正加入!
 logcat中说,错误是在这里:

 返回cursorToContact(光标);

这是这个附加功能的一部分:

  / **添加新的联系人到数据库中。 * /
公开联系方式的createContact(联系方式联系我们){
    //用来存储数据,如:COLUMN_NAME,VALUE。
    ContentValues​​值=新ContentValues​​();
    values​​.put(MySQLiteHelper.COLUMN_FIRST_NAME,contact.getFirstName());
    values​​.put(MySQLiteHelper.COLUMN_LAST_NAME,contact.getLastName());
    //插入新的接触,并获得它的ID。
    长insertId = database.insert(MySQLiteHelper.TABLE_NAME_FTS,空,
            值);
    //为了展示如何查询(要获取它的id接触)。
    光标光标= database.query(MySQLiteHelper.TABLE_NAME_FTS,
            allColumns,MySQLiteHelper.COLUMN_ID +=+ insertId,
                             空值,
            NULL,NULL,NULL);
    //将光标移动到第一行。
    cursor.moveToFirst();
    返回cursorToContact(光标);
}

那么,为什么这种情况发生> plz帮助我
 请注意,我期运用的SQLite数据库FTS3

在cursorToContact:

  / **用于获取联系人数据从光标到备忘录对象。 * /
私人联系cursorToContact(光标光标){
    联系方式联系我们=新的联系人();
    contact.setId(cursor.getLong(0));
    contact.setFirstName(cursor.getString(1));
    contact.setLastName(cursor.getString(2));
    返回接触;
}


解决方案

这行实际上将数据添加到您的数据库:

 长insertId = database.insert(MySQLiteHelper.TABLE_NAME_FTS,空,
            值);

任何之后发生的意外(大部分)。

如果您的查询搞砸了,你的其他部分将失败。您的疑问:

 光标光标= database.query(MySQLiteHelper.TABLE_NAME_FTS,
        allColumns,MySQLiteHelper.COLUMN_ID +=+ insertId,
                         空值,
        NULL,NULL,NULL);

看起来合适。该allColumns可以搞砸了,虽然。

另外,我不知道为什么你去设立在CursorToContact新联系人的所有的麻烦,只返回一些其他的价值。

  / **用于获取联系人数据从光标到备忘录对象。 * /
私人联系cursorToContact(光标光标){
    联系方式联系我们=新的联系人();
    contact.setId(cursor.getLong(0));
    contact.setFirstName(cursor.getString(1));
    contact.setLastName(cursor.getString(2));
    返回接触;
}

您从来不用接触。我本来期望你回来吧。

my app is contacts book , finally everything's working fine but when I, "the user", add new contact I get force close .. but the app didn't close, but rather just restarted ! and when i check the contact list i see that what i tried to add is really been added !!! logcat say the error is here :

return cursorToContact(cursor);

it is part of this add function:

    /** Add new Contact to Database. */
public Contact createContact(Contact contact) {
    //Used to store data like : COLUMN_NAME , VALUE.
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_FIRST_NAME, contact.getFirstName());
    values.put(MySQLiteHelper.COLUMN_LAST_NAME, contact.getLastName());
    //Insert new contact and get the id of it.
    long insertId = database.insert(MySQLiteHelper.TABLE_NAME_FTS, null,
            values);
    //To show how to query (To get contact by it id).
    Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME_FTS,
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, 
                             null,
            null, null, null);
    //Move Cursor to the first row.
    cursor.moveToFirst();
    return cursorToContact(cursor);
}

so why is that happen > plz help me note that i'm useing sqlite fts3 database

the cursorToContact :

    /** Used to get Contact data from Cursor to Memo Object. */
private Contact cursorToContact(Cursor cursor) {
    Contact contact = new Contact();
    contact.setId(cursor.getLong(0));
    contact.setFirstName(cursor.getString(1));
    contact.setLastName(cursor.getString(2));
    return contact;
}

解决方案

This line actually adds the data to your database:

   long insertId = database.insert(MySQLiteHelper.TABLE_NAME_FTS, null,
            values);

Anything that happens after that is incidental (mostly).

If your query is messed up, your other parts will fail. Your query:

Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME_FTS,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, 
                         null,
        null, null, null);

looks appropriate. The allColumns could be messed up, though.

Also, I'm not sure why you go to all the trouble of setting up a new contact in CursorToContact, only to return some other value.

    /** Used to get Contact data from Cursor to Memo Object. */
private Contact cursorToContact(Cursor cursor) {
    Contact contact = new Contact();
    contact.setId(cursor.getLong(0));
    contact.setFirstName(cursor.getString(1));
    contact.setLastName(cursor.getString(2));
    return contact;
}

You never use contact. I would have expected for you to return it.

这篇关于有趣的错误与我的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆