chis54
chis54

Reputation: 477

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.

Upvotes: 2

Views: 1220

Answers (2)

Philipp Reichart
Philipp Reichart

Reputation: 20961

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

Upvotes: 3

user554546
user554546

Reputation:

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.

Upvotes: 2

Related Questions