Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert string number that start with zero into string column using ContentValues, but leading zero is always removed [duplicate]

My table was created with

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 data was inserted with

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 when I retrieve data with

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 always be "406"

How to fix this problem? I don't want to do an ugly fix such as put some symbol in front of "0406" before insert and removed it out after retrieved.

like image 926
Pongthep Dokyaem Avatar asked Oct 03 '22 00:10

Pongthep Dokyaem


1 Answers

STRING is not a valid SQLite type, so line_code is using the default type, INTEGER - try changing your CREATE TABLE statement to use the TEXT type in place of STRING.

like image 100
ianhanniballake Avatar answered Oct 12 '22 11:10

ianhanniballake