Reputation: 803
I'm not entirely sure which part of my code is making this happen. This is my first attempt at coding in SQLite with Android and I'm using ContentValues to input records into the database. I get no errors or anything and data is inserted properly, however I'm getting 5 ListView entries for each record when I pull them from the database. It's either that I misunderstand how data is entered when using ContentValues and .insert to place the data in the database or I'm doing something wrong when retrieving data from the database.
Here is the code I'm using to insert data into the database.
DatabaseHelper helper = new DatabaseHelper(this);
database = helper.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(myName, titleText);
cv.put(myFileName, filenameText);
cv.put(myRecDate, recordedText);
cv.put(myPubDate, publishedText);
cv.put(myDescription, DescriptionText);
database.insert(myTable, castName, cv);
database.insert(myTable, castFileName, cv);
database.insert(myTable, castRecDate, cv);
database.insert(myTable, castPubDate, cv);
database.insert(myTable, castDescription, cv);
database.close();
Here is my code for populating my ListView.
DatabaseHelper helper = new DatabaseHelper(this);
database = helper.getWritableDatabase();
Cursor data = database.query("myDb", fields, null, null, null, null, null);
dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[] {R.id.idText, R.id.name, R.id.description});
final ListView view = getListView();
view.setHeaderDividersEnabled(true);
view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));
setListAdapter(dataSource);
database.close();
Since data is successfully being inserted, I don't think there's anything wrong with my DatabaseHelper class. Any advice is greatly appreciated.
Upvotes: 0
Views: 759
Reputation: 4705
Looks like you are calling insert() for each column. Put the column values into the ContentValues object with the ID of the column name. Then call
database.insert(myTable, null, cv);
only once.
Upvotes: 1
Reputation: 1184
ContentValues is supposed to represent a single record. So you have this part where you are creating the record from your object. These should be a mapping of column name to column value.
cv.put(myName, titleText);
cv.put(myFileName, filenameText);
cv.put(myRecDate, recordedText);
cv.put(myPubDate, publishedText);
cv.put(myDescription, DescriptionText);
And then here you are inserting the same record 5 times. I'm assuming your second parameters here are the column names.
database.insert(myTable, castName, cv);
database.insert(myTable, castFileName, cv);
database.insert(myTable, castRecDate, cv);
database.insert(myTable, castPubDate, cv);
database.insert(myTable, castDescription, cv);
In Android, you just need to specify the table name, myTable in this case, the second parameter should be null
, and the final parameter should be the ContentValues object.
So the correct way to do this would be:
ContentValues cv = new ContentValues();
cv.put(nameColumn, nameValue);
cv.put(fileNameColumn, fileNameValue);
... etc ...
database.insert(tableName, null, cv);
Upvotes: 1