5

我一直试图在数据库中插入一条记录,但即使我以前做过,它也不能在 Android 中工作。这是我第一次在 ORMLite 中处理多个表。

这就是我想要做的:将数据从 XML 存储到模型中,然后尝试从模型中插入数据。

使用的数据库:SQLite ORM:ORMLite

活动类

TbDriverInfoBO tbDriverInfoBO = new TbDriverInfoBO();
Document doc = XMLfunctions.XMLfromString(strResult);
NodeList nodes = doc.getElementsByTagName("root");
Element root = (Element) nodes.item(0);
jabongModel.setLoginId(XMLfunctions.getValue(root, "UserId"));
jabongModel.setPassword(XMLfunctions.getValue(root, "UserPwd"));
jabongModel.setDrsno(XMLfunctions.getValue(root, "DRSNo"));
jabongModel.setBrancode(XMLfunctions.getValue(root, "BranchCode"));
jabongModel.setLoginDate(XMLfunctions.getValue(root, "LoginDate"));
//clsDriverInfo obj = new clsDriverInfo(loginId, pswd, drsno,
/*tbDriverInfoBO.setColUserId(jabongModel.getLoginId());
tbDriverInfoBO.setColPwd(jabongModel.getPassword());
tbDriverInfoBO.setColDrs(jabongModel.getDrsno());
tbDriverInfoBO.setColBranch(jabongModel.getBrancode());*/

tbDriverInfoBO.fillBo(jabongModel);

dbHelperTbDriverInfo.addData(tbDriverInfoBO);

BO类:

public class TbDriverInfoBO {

    @DatabaseField(generatedId = true)
    int id;
    @DatabaseField
    String colUserId;
        ...

        // getters and setters

    public TbDriverInfoBO(String colUserId, String colPwd, String colDrs, String colBranch)
    {
        this.colBranch = colBranch;
        this.colDrs = colDrs;
        this.colPwd = colPwd;
        this.colUserId = colUserId;
    }

    public TbDriverInfoBO() {
        // TODO Auto-generated constructor stub
    }
    public void fillBo(JabongModel jabongModel)
    {
       this.colBranch = jabongModel.getBrancode();
       this.colDrs = jabongModel.getDrsno();
       this.colPwd = jabongModel.getPassword();
       this.colUserId = jabongModel.getLoginId();
       //this.id = jabongModel.getId();

    }
}



DBHelperClass
public int addData(TbDriverInfoBO tbDriverInfoBO) {
    int i=0;
    RuntimeExceptionDao<TbDriverInfoBO, String> dao = getTbDriverInfoDaO();
    try{
     i = dao.create(tbDriverInfoBO);
    }
    catch (Exception e) {
        e.getStackTrace();// TODO: handle exception
    }
    return i;
}

我无法弄清楚我哪里出错了。有人请帮忙!!!

这是堆栈跟踪(获得日志猫):

01-28 15:43:46.251: D/dalvikvm(368): threadid=1: still suspended after undo (sc=1 dc=1 s=Y)
01-28 15:43:46.251: D/dalvikvm(368): GC_FOR_MALLOC freed 8148 objects / 498080 bytes in 72ms
01-28 15:43:49.514: D/dalvikvm(368): threadid=1: still suspended after undo (sc=1 dc=1 s=Y)
01-28 15:43:50.790: D/dalvikvm(368): threadid=1: still suspended after undo (sc=1 dc=1 s=Y)
01-28 15:43:50.900: I/Database(368): sqlite returned: error code = 1, msg = no such table: tbdriverinfobo
01-28 15:43:58.681: D/dalvikvm(368): threadid=1: still suspended after undo (sc=1 dc=1 s=Y)
01-28 15:44:01.956: D/dalvikvm(368): threadid=1: still suspended after undo (sc=1 dc=1 s=Y)
4

2 回答 2

11

在我看来,您没有tbdriverinfobo.

sqlite returned: error code = 1, msg = no such table: tbdriverinfobo

你能onCreate()在助手中展示你的方法吗?您的应用程序是否有一个现有的数据库,并且您后来添加了此表?你需要增加你的数据库版本号吗?

于 2013-01-29T14:28:16.643 回答
0

您收到此错误是因为您在想要的新表之前创建了数据库和表

tbDriverInfoBO(这是你的新表)

并且 ORMLite 库正在抛出类似“找不到表tbDriverInfoBO ....”之类的异常。所以您需要从手机中卸载您的移动应用程序,然后您应该将所有表类写入 onCreate 方法,如下面的代码。 ..

@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {
        TableUtils.createTable(connectionSource, Table1.class);
        TableUtils.createTable(connectionSource, Table2.class);
        TableUtils.createTable(connectionSource, Table3.class);
        TableUtils.createTable(connectionSource, Table4.class);
        TableUtils.createTable(connectionSource, Table5.class);//you must add all your table class 
        TableUtils.createTable(connectionSource, TableN.class);
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

添加所有表类后,您可以将数据添加到本地表...

我希望我的回答对某人有所帮助......快乐的编码

于 2017-12-14T13:07:46.063 回答