質問

i can't figure out this strange error while i try to insert value into database

ERROR

01-01 15:48:56.065  10056-10056/com.autophone E/SQLiteLog﹕ (1) table mainTable has no column named status_date
01-01 15:48:56.100  10056-10056/com.autophone E/SQLiteDatabase﹕ Error inserting status_date=1 בינו 2000 15:48:56 problem=non model=N9005 phone_number=123 in_date=1 בינו 2000 15:48:56 status=received name=ian
    android.database.sqlite.SQLiteException: table mainTable has no column named status_date (code 1): , while compiling: INSERT INTO mainTable(status_date,problem,model,phone_number,in_date,status,name) VALUES (?,?,?,?,?,?,?)

the main point it says i don't have "status_date" in the main table but how is it possible?

  public static final String KEY_ROWID = "_id";
    public static final int COL_ROWID = 0;

    private static final String KEY_NAME = "name";//1
    private static final String KEY_MODEL = "model";//2
    private static final String KEY_PHONE_NUMBER = "phone_number";//3
    private static final String KEY_PROBLEM = "problem";//4
    private static final String KEY_STATUS = "status";//5
    private static final String KEY_IN_DATE = "in_date";//6
    private static final String KEY_STATUS_DATE = "status_date";//7

    public static final String DATABASE_NAME = "MyDb";
    public static final String DATABASE_TABLE = "mainTable";
    public static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE_SQL =
            "create table " + DATABASE_TABLE
                    + " (" + KEY_ROWID + " integer primary key autoincrement, "
                    + KEY_NAME + " text not null, "
                    + KEY_MODEL + " text not null, "
                    + KEY_PHONE_NUMBER + " text not null, "
                    + KEY_PROBLEM + "text not null, "
                    + KEY_STATUS + "text not null, "
                    + KEY_IN_DATE + "text not null, "
                    + KEY_STATUS_DATE + "text not null "
                    +");";

here it is...

this is the enter part

 long newID=myDB.insertRow(new LogEntery("ian","N9005","123","non"));

logentery is a class i use that also adds the rest for the data

public LogEntery(String name,String model,String phone_number,String problem)
    {
        super();
        this.name=name;
        this.model=model;
        this.phone_number=phone_number;
        this.problem=problem;
        this.in_date = DateFormat.getDateTimeInstance().format(new Date());
        this.status_date = DateFormat.getDateTimeInstance().format(new Date());
        this.status = "received";
    }

by the way... how do i change getDateTimeInstance to get DD/MM/YYYY only and not the full data/time?

役に立ちましたか?

解決

A common source of error is the database version.

If you ran your app at least once without the KEY_STATUS_DATE field in your table, and then added that field and ran your app, the table won't be updated (onUpgrade() will not be called), because the system thinks that the database hasn't changed.

Try raising the version.

public static final int DATABASE_VERSION = 2; 

With this, the system will think "Oh, there's a new version of the database, let's call onUpgrade()". Ideally from this method, you delete your tables an call onCreate(SQLiteDatabase db) manually, or execute some DDL statements to add the missing field.

他のヒント

You can check your database in your phone to see if it is real that no column named status_date.

Solution to your "by the way":

SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd/MM/yyyy"); String dateString = dateFormatGmt.format(new Date());

My advice would be to start with a basic schema that works, then write your abstraction layer. I'd take a step back and make sure that all of the assumptions are correct.

For example, there are 4 arguments to your constructor, while there are 8 columns in your table and 7 setters being called in the constructor. Looks like you might just be off-by-one, at a glance. If not, its still a little confusing. I'd either require all parameters for the statement in the constructor, or mark the optional fields in some way (i.e. overloaded method calls with a no-args constructor, etc).

Hope my input is helpful.

Brandon

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top