Insert a string number starting at zero in the string column using ContentValues, but the leading zero is always removed

My table was created using

db.execSQL( "CREATE TABLE periods (" + " vehicle_id INTEGER KEY," + " line_id INTEGER KEY," + " period_id INTEGER PRIMARY KEY AUTOINCREMENT," + " line_code STRING," + " sup_code STRING," + " start_date INTEGER," + " end_date INTEGER" + ")" ); 

And the data was inserted using

 ContentValues values = new ContentValues(); values.put("vehicle_id", 1); values.put("line_id", 2); values.put("line_code", "0406"); values.put("sup_code", " "); values.put("start_date", 1); values.put("end_date", 24); db.insert("periods", null, values); 

But when I get data from

 Cursor cPeriods = db.rawQuery("SELECT * FROM periods WHERE vehicle_id=" + vehicleId, null); System.err.printf("SQLite: ### Get line_code = \"%s\"\n", cPeriods.getString(3)); 

The result will always be "406"

How to solve this problem? I don’t want to make an ugly correction, for example, put the character β€œ0406” before the insertion and delete it after extraction.

+4
source share
2 answers

STRING not a valid SQLite type, so line_code uses the default type, INTEGER β€” try changing the CREATE TABLE statement to use the TEXT type instead of STRING .

+2
source

you should examine the registration part of sql, if the string code is registered as an integer type, zero will be deleted.

+1
source

Source: https://habr.com/ru/post/1501125/


All Articles