Difference Between “Text” and “String” datatype in SQLite

前端 未结 2 1218
暖寄归人
暖寄归人 2020-12-10 10:40

I have a SQLite database with my Android application. I have noticed I accidentally defined a table using a \"String\" datatype instead of \"Text\" datatype. Here is the c

2条回答
  •  死守一世寂寞
    2020-12-10 11:06

    The subtle thing to note here is that SQLite does not enforce the data type of values you put into columns. That means that you can put text into a numeric field, and so on.

    To understand the difference between your two SQL statements, check out section 2.1 Determination Of Column Affinity, which maps the column types you provide to the storage classes SQLite uses.

    In this case, the type string gets mapped to storage class NUMERIC via rule 5. Declaring the field as text in code would tell the DBMS to use the TEXT storage class. Again, since SQLite does not enforce the types of columns, your code will probably run fine when storing Strings as a NUMERIC column, as you note.

    As an alternative example, you could define a column with type INTERESTING STUFF, and that would be mapped to the INTEGER storage class, via rule 1.

    Overall, it's probably a good idea to just use text for your table definition.

提交回复
热议问题