简体   繁体   中英

SQLite or JDBC strange behaviour

I have a simple version table in databse. Here is DDL:

CREATE TABLE version (current STRING NOT NULL);
INSERT INTO version (current) VALUES ("1.0");

In this table stored only one row with current database structure version. At the first blush everything is good, but here is a little strange thing:

//... connect to SQLite and another stuff...
ResultSet resultSet = statement.executeQuery("select current from version;");
if(!resultSet.next())
    throw new SQLException("Corrupted version table! Aborting...");
String versionStr = resultSet.getString(1);
System.out.println("Version string: '" + versionStr + "'");
//... close statement, blah-blah-blah

Output is: '1'. Where is '.0' missed? If current value is '0.1', '1.3' and fraction part is not zero - everything is good. Since it zero - '.0' is lost.

So, question is: why it happens and how to fix it?

STRING isn't a SQLite data type, so the column current defaults to the NUMERIC affinity and tries to convert whatever has been inserted into it into a number. Change STRING to TEXT and the column will keep your data as a string.

The Datatypes in SQLite3 documentation page gives a more in-depth explanation:

The affinity of a column is determined by the declared type of the column, according to the following rules in the order shown:

If the declared type contains the string "INT" then it is assigned INTEGER affinity.

If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.

If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity BLOB.

If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity.

Otherwise, the affinity is NUMERIC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM