Android & SQLite: "Table audios has no column named downloaded"
I get this weird exception when i run my app on both the emulator and a USB connected device. i can see the table inside the shell, but when I try to insert a record from the Activity acton I get this exception:
android.database.sqlite.SQLiteException: table audios has no column named downloaded
I am mostly running on an emulator with Android 1.5. The predominant part of my code is linked to this tutorial - http://www.devx.com/wireless/Article/40842/1954.
This is the create statement I use:
private static final String DATABASE_CREATE =
"create table audios ( _id integer primary key autoincrement, "
+ "user_name text not null, title text not null, file_path text not null, download integer not null, "
+ "created_at integer not null, downloaded_at integer not null );";
This is the insert code I use:
//--- insert a title into the database ---
public long insertTitle(String user_name, String title, String file_path, Integer downloaded, long created_at, String downloaded_at ) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USER_NAME, user_name);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_FILE, file_path);
initialValues.put(KEY_DOWNLOADED, downloaded);
initialValues.put(KEY_CREATED_AT, created_at);
initialValues.put(KEY_DOWNLOADED_AT, downloaded_at);
return db.insert(DATABASE_TABLE, 开发者_开发技巧null, initialValues);
}
I will hazard a guess that you have this in your code:
public static final String KEY_DOWNLOADED = "downloaded";
The reason for the error message is that...
table audios has no column named downloaded
Really. It doesn't, just as the error message says.
If you re-read your table-creation SQL, you'll notice that the column name is actually download
, not download
ed.
So correct your code to use the correct column name and you should be set. Note that correcting the code could mean either:
- changing the column name in your table to be
downloaded
instead of justdownload
. Personally I would go with this one, unless you have lots of other code already usingdownload
, sincedownload
in my mind is eithera download
orto download
, nothas been downloaded
, but it really depends on its actual meaning - changing the column name in the SQL that accesses the table to use the same column name as the table
if you create the table without this column for before ,and then you add this column, just modify the database version ,the eclipse will delete the old database and create the new database .
My case is a little different but the error message is the same. "Table xxx hax no column named yyy". I'm answering here so if someone is also "staring holes in the screeen" may find a tip. If you define your table with placeholders like this:
private static final String SQL_CREATE_VISITA = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s %s, %s %s, %s %s, %s %s, %s %s, %s %s)",
TableVisit.NAME,
TableVisit.COL_NR_VISITA,
TableVisit.COL_TP_CASA,
FIELD_TEXT,
TableVisit.COL_NR_CASA,
FIELD_INTEGER,
TableVisit.COL_BENS_MOVEIS,
FIELD_INTEGER,
TableVisit.COL_DT_VISITA,
FIELD_TEXT,
TableVisit.COL_NR_COMODOS,
FIELD_INTEGER,
TableVisit.COL_NR_ISENCAO,
FIELD_INTEGER,
TableVisit.COL_NR_QUARTOS,
FIELD_INTEGER
);
always count your placeholders carefully. If you miss 2, like in this example code, when adding another field, table may be created giving the message error mentioned only when you try do insert a new record to the table. (my first answer here, so any comment is more than welcome.)
精彩评论