I have created a database for my app with 5 columns. Each column only consists of integers.
At first, the table is empty. Now, if I add the values {1, 2, 3} to the first column (column1), I would like it too look like this:
But in reality, it looks like this:
That is, it has inserted unwanted zeros at places to which I didn’t add new values.
I have tried two different ways of inserting values into the database.
First way:
DBAdapter db = new DBAdapter(this);
int[] sdTest = new int[] {1,2,3};
storeIDs(db, sdTest);
public void storeIDs(DBAdapter db, int[] ids)
{
db.open();
long id;
for(int i=0; i<ids.length; i++)
{
id = db.insertIDs(ids[i]);
}
db.close();
}
And this method from the DBAdapter class:
public long insertIDs(int sd)
{
ContentValues initialValues = new ContentValues();
initialValues.put(SD, sd);
return db.insert(DATABASE_TABLE, null, initialValues);
}
(where SD is the column name of the first column.)
Second way:
DBAdapter db = new DBAdapter(this);
int[] sdTest2 = new int[] {1,2,3};
storeIDsExe(db, sdTest2);
public void storeIDsExe(DBAdapter db, int[] ids)
{
db.open();
for(int i=0; i<ids.length; i++)
{
db.insertIDsExe(ids[i]);
}
db.close();
}
And this method from the DBAdapter class:
public void insertIDsExe(int sd)
{
db.execSQL("INSERT INTO "
+ DATABASE_TABLE
+ " (column1)"
+ " VALUES ("+sd+");");
}
Both ways give the same result: unwanted zeros.
How to get rid of them?
edit:
The table is created like so:
private static final String DATABASE_CREATE =
"CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE
+ " (column1 int, column2 int, column3 int, column4 int, column5 int);";
Does your table define default values for the columns? Columns should have attribute DEFAULT NULL :
EDIT
I have just tested it, in-memory sqlite db with Firefox SQLiteManager. It works.
and then