I wrote a rssFeedReader Application and use directly sqliteSyntax to modify database.
this is my code:
database.execSQL database= openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
database.execSQL("Create TABLE IF NOT EXISTS Press (press_name
VARCHAR, press_url VARCHAR");
database.execSQL("INSERT INTO Press VALUES ('Tasnim',
'http://www.tasnimnews.com/english/rss/feed/?d=2&c=1&m=6&alt=Recent%20News');");
database.execSQL("UPDATE Press press_name ='Tasnim',
press_url ='http://www.tasnimnews.com/english/rss/feed/?
d=2&c=1&m=6&alt=Recent%20News');");
Cursor cursor = database.rawQuery("SELECT * FROM MyTable", null);
database.close();
and now my question is how can I migrate to SQLiteOpenHelper and ConentValues method to modify database.
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id);
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_CONTENT, content);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
FeedEntry.TABLE_NAME,
FeedEntry.COLUMN_NAME_NULLABLE,
values);
You can find it here http://developer.android.com/training/basics/data-storage/databases.html
Related
I have created a listview from an sqlite db and when i click it, it opens up the corresponding text, I want to add it to a favourites folder by clicking a button which updates a column in the row to "y". Any help would be appreciated
You can use the update query
pssQliteHelper = PSSQliteHelper.getInstance(context);
SQLiteDatabase db = pssQliteHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("y", true);
db.update(TABLE_NAME, contentValues, " id = '" + dbInsertionId + "'",null);
db.close();
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();
}
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.
I'm dealing with a simple app, which basically stores various dates. But when I enter a new date, I have to close the app and open it again. The same thing counts for changing the date values. I can only see the changes by closing the app and reopening it.
Here are the two main functions in my DB class;
public void addBirthday(Person person){ //Add a data to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public int updateBirthday(Person person) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
// 3. updating row
int i = db.update(TABLE_NAME, //table
cv, // column/value
column_id +" = ?", // selections
new String[] { String.valueOf(person.getId()) }); //selection args
// 4. close
db.close();
return i;
}
You will need to refresh the view (probably a ListView) where you display this data.
Get rid of the notifyDataSetChanged() and call requery() on your Cursor
I have got 2 tables. Headers with names and details with texts:
create table Headers (_id integer primary key autoincrement, name string);
create table Details (_id integer primary key autoincrement, id_headers integet, text string);
id_headers is the link to table Headers row (one-to-many).
I want to write a method to upgrade these tables. The first and the least case I know is to create a temp table copy of 1st and 2nd tables, create new structure and insert data into new structure.
But in this case all "id_headers to _id" relations will be lost.
How can I keep them in new structure, and the same time I want them to keep as "autoincrement".
SQLiteDatabase.insert returns the new _id. Insert the Headers table data first, creating a mapping of new _id's against _id's in temp data structure.
Now when you populate the Details table consult your map for the old id_headers value to get the new id_headers value.
private void migrate(SQLiteDatabase db){
ArrayList<Header> oldHeaders = new ArrayList<Header>();
ArrayList<Detail> oldDetails = new ArrayList<Detail>)();
HashMap<Long,Long> idMap = new HashMap<Long,Long>();
Cursor oldHeadersCurs = db.query("Headers", null, null, null, null, null, null);
oldHeadersCurs.moveToFirst();
//store the old header records
while (!oldHeadersCurs.isAfterLast()){
long oldId = oldHeadersCurs.getLong(oldHeadersCurs.getColumnIndex("_id"));
String name = oldHeadersCurs.getString(oldHeadersCurs.getColumnIndex("name"));
oldHeaders.put(new Header(oldId,name));
oldHeadersCurs.moveToNext();
}
//delete the headers table
db.execSQL("DROP TABLE Headers");
//create the new headers table
db.execSQL(CREATE_NEW_HEADERS_TABLE_STMT);
//insert the header records capturing the new id
for (Header header : oldHeaders){
ContentValues cv = new ContentValues();
cv.put("name", header.getName());
long newId = db.insert("Headers", null, cv);
idMap.put(header.getId(), newId); //mapping the old _id to the new
}
//store the old detail records
Cursor oldDetailsCurs = db.query("Details", null, null, null, null, null, null);
oldDetailsCurs.moveToFirst();
while (!oldDetailsCurs.isAfterLast()){
//btw text is a data type in sqlite, you need to rename this column
String text = oldDetailsCurs.getString(oldDetailsCurs.getColumnIndex("text"));
long oldHeaderId = oldDetailsCurs.getLong(oldDetailsCurs.getColumnIndex("id_headers"));
oldDetails.put(new Detail(text,oldHeaderId));
oldDetails.moveToNext();
}
//recreate details table
db.execSQL("DROP TABLE Details");
db.execSQL("CREATE_NEW_DETAILS_TABLE_STMT");
//insert the new detail records using the id map
for (Detail detail : oldDetails){
ContentValues cv = new ContentValues();
cv.put("text",detail.getText());
cv.put("id", idMap.get(detail.getHeaderId())); //retrieving the new _id based on the old
db.insert("Details", null, cv);
}
}