Names are getting overlapped in SQLite - android

In my Android App, I am trying to insert names in a column forcefully into database but In database only last name is showing i.e. Myself because names are overlapping. Actually My database portion is weak.
This is what I am doing in my Database Handler Class,
public void addPoliticianNames()
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
values.put(KEY_POLITICIANNAMES, "Me");
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
}
What to do for getting these names sequentially into the column (Here I have to insert names forcefully as I have mentioned above). So, Is there any method for this ? Is there any role of cursor in doing this job (like movetonext method or something like that) ?
Please help me,
Thanks.

you have to make one loop for inserting data into database. it will pick one by one value from array and put it into database.
Put this method in your database class
public void deleteTable()
{
database.delete(TABLE_POLLINGVOTES, null, null);
}
db.deleteTable(); // call it with this line for delete all records of your table
// for inserting data into table.
String[] a = {"I","Me","MySelf"};
ContentValues values = new ContentValues();
for (int i = 0; i < a.length; i++)
{
values.put(KEY_POLITICIANNAMES, a[i]);
db.insert(TABLE_POLLINGVOTES, null, values);
}
Hope it will help you.

If you want to insert three records, you have to use three insert calls.
For example:
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Me");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);

#Shiv change the column name for every value
public void addPoliticianNames(String ss)
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, ss);
}
ss contain value firts time "I",second time "me" like that

Related

Updating rows in Android SQlite

I'm using SQlite in my Android app and the task is - how can I update all the rows in one table?
I have a 1st column with name "cl_id" (integer numbers 1-2-3-4..) and after deleting some rows, I wan't to make a cycle to fill this 1st column with a new values to keep them in right order.
I was trying to execute:
data.put("cl_id",index);
db.update("mdb_table_contactList", data, null, null);
but it's updates all the values in the first column :(
To update the field in specific row, try this;
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id="+_id, null);
db.close();
It's good practice to have a primary key for each row data.
Here _id would be your primary key.
"just give rowId and type of data that is going to be update in ContentValues."
public void updateStatus(String id , int status){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("status", status);
db.update(TableName, data, "columnName" + " = "+id , null);
}
Try this it must work
void updateDatail(String id) {
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id=?", new String[]{id});
db.close();
}

Method db.delete some rows?

In the table I have 4 fields (note1, note2, note3, Note4). I want to delete all the data in note1 and note2. I have to add arguments to the method db.delete, but I have trouble.
public void delete() {
SQLiteDatabase db= mHelper.getWritableDatabase();
db.delete(noteTable.TABLE_NAME, null, null);
}
Delete always deletes entire rows.
To clear values in specific columns, use an update query, for example
ContentValues cv = new ContentValues();
cv.putNull("note1");
cv.putNull("note2");
db.update(noteTable.TABLE_NAME, cv, null, null);

Android split function behavior

There is a txt file that looks like:
It is being splitted by a delimiter ":" well:
temp = strLine.split(":");
ContentValues initialValues = new ContentValues();
initialValues.put(TITLE, temp[0].trim());
initialValues.put(DESCRIPTION, temp[1].trim());
initialValues.put(GROUP, temp[2].trim());
initialValues.put(COL1, temp[3].trim());
initialValues.put(COL2, temp[4].trim());
initialValues.put(COL3, temp[5].trim());
initialValues.put(ADDRESS, temp[6].trim());
db.insert(DATABASE_TABLE, null, initialValues);
}
As you can see this script fills the database. After that I'm displaying a list of data retrieved from database.
But when the .txt does not contain data for ADDRESS field:
Then inserting function returns "ArrayIndexOutOfBoundsException" and the cursor becomes null.
It's OK for database to have null in ADDRESS column, so how to overcome this issue in initialValues.put(ADDRESS, temp[6].trim());
What can you suggest? Thanks
// try to replace this line
initialValues.put(ADDRESS, temp.length>6 ? temp[6].trim():"");
You can put condition for Address
temp = strLine.split(":");
ContentValues initialValues = new ContentValues();
initialValues.put(TITLE, temp[0].trim());
initialValues.put(DESCRIPTION, temp[1].trim());
initialValues.put(GROUP, temp[2].trim());
initialValues.put(COL1, temp[3].trim());
initialValues.put(COL2, temp[4].trim());
initialValues.put(COL3, temp[5].trim());
if(temp.length==7)
{
initialValues.put(ADDRESS, temp[6].trim());
}
else
{
initialValues.put(ADDRESS, "");
}
db.insert(DATABASE_TABLE, null, initialValues);

Android:Sqlite insertion of hardcoded data

I have insert two fields namely truck_id and employee_id in a table.
Sorry to ask this question. But as I am new to android,please help me out.
public void insert(String[] employeeName,int[] truck_id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(COLUMN_TRUCKID, "CM-3456");
vals.put(COLUMN_NAME,"Sachin");
//vals.put(KEY_PIN, "12345");
vals.put(COLUMN_TRUCKID, "HR-6788");
vals.put(COLUMN_NAME,"Sameer");
//vals.put(KEY_PIN, "54321");
vals.put(COLUMN_ID, "AM-6123");
vals.put(COLUMN_NAME,"Sahu");
//vals.put(KEY_PIN, "3452");
db.insert("Login", "John",
vals);
db.insert("Login", "Sameer",
vals);
db.execSQL(COLUMN_NAME);
//myDataBase.close();
}
ContentValues is a Map, so it can only contain one entry at a time for each column name. So, you need to insert each item separately after settings its values.
vals.put(COLUMN_TRUCKID, "CM-3456");
vals.put(COLUMN_NAME,"Sachin");
db.insert("Login", null, vals);
vals.put(COLUMN_TRUCKID, "HR-6788");
vals.put(COLUMN_NAME,"Sameer");
db.insert("Login", null, vals);
Remove this line, or else bad things will happen:
db.execSQL(COLUMN_NAME);

ListView returning multiple entries for same record from SQLite

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.
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);
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.

Categories

Resources