I want to only insert like 10 rows into a table at database creation. I do not want them added every time the app is open. Just once as a table with final values. How would I go about doing this? Thanks!
public void insertEvents(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_EVENT, "value1");
values.put(COL_EVENT, "value2");
values.put(COL_EVENT, "value3");
values.put(COL_EVENT, "value4");
db.insert(TABLE_EVENTS, null, values);
}
Since you are using SQLiteOpenHelper, put the inserts in its onCreate(). Use the SQLiteDatabase passed in as argument, do not recursively call getWritableDatabase().
Do it in onCreate method of DatabaseHelper
EDIT: here is the similar question. Or you can try this tutorial
I know this might be too late but I'm just sharing my answers. Your code seems to show that you are trying to add 4 rows. Here are some pointers:
1) Do it in the onCreate method, as mentioned by #Georgiy Shur.
2) The insert method only inserts one row at a time. So you should do this instead:
ContentValues values = new ContentValues();
values.put(COL_EVENT, "value1");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value2");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value3");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value4");
db.insert(TABLE_EVENTS, null, values);
Related
I have to below table structure of my sqlite database in android
I want to update value to "answer" column according to the ques id.
How do I do it?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_Answer, answer);
// Inserting Row
db.insert(TABLE_Question, null, values);
db.close(); // Closing database connection
I want to insert value to "answer" column according to the ques id.
This operation called "update" instead of "insert".
insert: means to add new record in table,
update: means change value of any exist row columns
so required operation is update.use update method as:
String where = "Ques_id=?";
String[] whereArgs = new String[] {String.valueOf(Ques_id)};
ContentValues values = new ContentValues();
values.put(KEY_Answer, answer);
db.update(TABLE_Question, values, where, whereArgs);
insert() inserts a new row. To update a column on an existing row, use update() with a selection like "Ques_id=" + id.
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);
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
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);
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.