本文介绍了不添加Ormlite DatabaseConfigUtil.java产生原始的空文件和数据库中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse赫利俄斯在Win 7平台。
我有这样的DAO类

I'm using Eclipse Helios on Win 7 platform.I have this Dao class

package com.example.hello;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "accounts")
public class Account {

        // for QueryBuilder to be able to find the fields
        public static final String NAME_FIELD_NAME = "name";
        public static final String PASSWORD_FIELD_NAME = "passwd";

        @DatabaseField(generatedId = true)
        private int id;

        @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false)
        private String name;

        @DatabaseField(columnName = PASSWORD_FIELD_NAME)
        private String password;

        Account() {
                // all persisted classes must define a no-arg constructor with at least package visibility
        }

        public Account(String name) {
                this.name = name;
        }

        public Account(String name, String password) {
                this.name = name;
                this.password = password;
        }

        public int getId() {
                return id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getPassword() {
                return password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        @Override
        public int hashCode() {
                return name.hashCode();
        }

        @Override
        public boolean equals(Object other) {
                if (other == null || other.getClass() != getClass()) {
                        return false;
                }
                return name.equals(((Account) other).name);
        }
}

和我DatabaseConfigUtil如下,

and my DatabaseConfigUtil is as follows,

package com.example.hello;

import java.io.IOException;
import java.sql.SQLException;

import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;

/**
 * Database helper class used to manage the creation and upgrading of your database. This class also usually provides
 * the DAOs used by the other classes.
 */
public class DatabaseConfigUtil extends OrmLiteConfigUtil {

    public static void main(String[] args) throws SQLException, IOException {
        writeConfigFile("ormlite_config.txt");
    }
}

我的问题是,如果我尝试生成的原始数据库配置文件它RES /原始文件夹,但没有在该文件中添加成功生成除了

My problem is if I try to generate raw database config file it generated successfully in res/raw folder but nothing added in the file except

#
# generated on 2013/06/26 05:18:40
#

为什么我的数据库字段这里没有自动生成的?

Why my database fields are not auto generated here ?

推荐答案

据为 writeConfigFile(字符串文件名)

查找在当前目录或下方的注释类和
  在原始文件夹中写入一个配置文件的文件名。

假设你的类和 DatabaseConfigUtil 在同一个目录/包,它应该工作。至于你提到的,它没有。

Assuming your classes and DatabaseConfigUtil are in the same directory/package, it should work. As you mention, it doesn't.

我不知道为什么你的 RES /生/ ormlite_config.txt 是空的,但是我对的建议修复另一种解决方案。我做了这样的(与4.45版):

I don't know why your res/raw/ormlite_config.txt is empty, but I have a suggestion for another solution. I did it like this (with version 4.45):

public class DatabaseConfigUtil extends OrmLiteConfigUtil {

    // The logger. We cannot use Android's Log class since this is a standalone command line app.
    private static final Logger logger = Logger.getLogger(DatabaseConfigUtil.class.getName());

    /**
     * The name of the generated ORMLite config file.
     */
    public static final String CONFIG_FILE_NAME = "ormlite_config.txt";

    /**
     * An array of Class-es which will be stored in the local DB.
     */
    public static final Class<?>[] CLASSES = new Class[]{
            Alarm.class,
            Helper.class
    };

    /**
     * A main method that needs to be executed when a new model class is
     * introduced to the code base.
     *
     * @param args command line parameters (which are ignored).
     *
     * @throws IOException  when the config file cannot be written to `res/raw/`.
     * @throws SQLException when one of the Class-es in `CLASSES` contains invalid
     *                      SQL annotations.
     */
    public static void main(String[] args) throws IOException, SQLException {

        File rawFolder = new File("res/raw");

        // Check is `res/raw` exists ...
        if(!rawFolder.exists()) {

            // ... if not create it.
            boolean rawCreated = rawFolder.mkdirs();

            if(!rawCreated) {
                logger.warning("could not create a 'raw' folder inside 'res/'" +
                        " from DatabaseConfigUtil: no DB-config file created!");
                System.exit(1);
            }
            else {
                logger.info("created folder `res/raw/`");
            }
        }

        writeConfigFile(CONFIG_FILE_NAME, CLASSES);
    }
}

这篇关于不添加Ormlite DatabaseConfigUtil.java产生原始的空文件和数据库中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:31