java - Update SQLite Database -
i have sqlite database set tutorial changing around own project: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
however tutorial not mention how update field.. have tried this:
db = new notesdatabasehandler(viewnoteactivity.this); ... db.updatenote(new note(identifier, edittexttitle.gettext().tostring(), edittextcontent.gettext().tostring(), "updated on: " + printstandarddate()));
although have had no luck.. run application , note not update.
note class:
package com.timmo.notes; class note { //private variables private int _id; private string _title; private string _content; private string _metadata; // empty constructor public note() { } // constructor public note(int id, string title, string content, string metadata) { this._id = id; this._title = title; this._content = content; this._metadata = metadata; } public int getid() { return this._id; } public void setid(int id) { this._id = id; } public string gettitle() { return this._title; } public void settitle(string title) { this._title = title; } public string getcontent() { return this._content; } public void setcontent(string content) { this._content = content; } public string getmetadata() { return this._metadata; } public void setmetadata(string metadata) { this._metadata = metadata; } }
from notesdatabasehandler():
// updating single note public int updatenote(note note) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_title, note.gettitle()); values.put(key_content, note.getcontent()); values.put(key_metadata, note.getmetadata()); // updating row int ret = db.update(table_notes, values, key_id + " = ?", new string[]{string.valueof(note.getid())}); db.close(); return ret; }
previously, have deleted note, added in again, adds end of database instead of in same position.
so, question, how update 1 of notes (contacts) method using? thanks
update: i've tried using raw update:
public void updatenote(note note) { sqlitedatabase db = this.getwritabledatabase(); string strsql = "update " + table_notes + " set " + key_title + "='" + note.gettitle() + "', " + key_content + "='" + note.getcontent() + "', " + key_metadata + "='" + note.getmetadata() + "'" + " " + key_id + "='" + note.getid() + "'"; log.d("sql: ", strsql); db.execsql(strsql); db.close(); }
yet still nothing happens. log outputs:
update notes set title='hello', content='there', metadata='updated on: 09:48 - 08 aug 2015' id='7'
i can't see what's wrong...
what u want update? update u need pass valid - existing row id (primary key)
- read (get id)
- edit
- update (with old id)
keep in mind
u can try raw:
string strsql = "update mytable set column1 = somevalue columnid = "+ somevalue; mydatabase.execsql(strsql);
or try:
string = "_id=" + note.getid(); mydatabase.update(table,cv,where,null) ;
i've tried raw , yet nothing happens.. "update notes set title='hello fgwejfneqdfjoe', content='there', metadata='updated on: 06:48 - 08 aug 2015' id='7'" should updating..
get rid of apostrophe sql statement
if using sql strings in database operations, chances have come across problem strings in sql statement. 1 apostrophe screws sql string, causing sql statement fail.
building sql statement concatenating strings not advisable. is:
- unscalable -- database "hard parse" every execution of sql -
- prone failure every last name input o'connor (or similar surname), possessive, or contracted input.
- vulnerable sql injection user if input parameter values used as-is.
Comments
Post a Comment