chis54 chis54 - 28 days ago 9
Android Question

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 table 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.

Answer

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