Android SQLite column int to float possible?
I have an app out on the Android market that user data is stored in SQLite. I created a database tab开发者_StackOverflowle with a column of ints. I would like to store floats there now. How can I do this? When I try to insert a float, my app seems to throw an exception.
As SQlite doesn't implement full support for ALTER TABLE
(only renaming a table and adding a column), you'll have to:
- create a temporary table with the new column format, i.e. float
- copy over the data from the original table into the temporary table (the ints will be "widened" into floats, no data loss here), e.g.
INSERT INTO tmp SELECT * FROM orig
- drop the original table
- rename temporary table to original table
Unfortunately, SQLite does not support the use of alter table
to change the datatype of a column. So, the only solution that I know of is to create a new table (with the particular column not specified as an int
datatype), fill in the new table, drop the old table, and then rename the new table (via the alter table
command) to the same name as the old table.
精彩评论