I try to update database table and this is my code:
public void updatefiletable(String filename, String v1, String v2){
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(AndroidOpenDbHelper.COLLUMN_NAME_FILE_CLOUD, v1);
values.put(AndroidOpenDbHelper.COLLUMN_NAME_FILE_DATE_UPLOADING, v2);
sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values, AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"="+filename, null);
sqliteDatabase.close();
}
when I call my method with file_name values egal priv_priv_secondfile_2012-06-15.pdf I get that in the logcat:
android.database.sqlite.SQLiteException: unrecognized token: "15.pdf": ,
while compiling: UPDATE file_table SET file_cloud_column=?,
file_date_upload_column=?
WHERE file_name_column=priv_priv_secondfile_2012-06-15.pdf
how can I fix it?
You need to escape the filename parameter. The punctuation in the filename is confusing SQLite. You could do it by surrounding the filename in 'single quotes' in the string you pass in to SQLite, but it's cleaner and safer to pass it as a separate argument, like this:
sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values,
AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"=?", new String[] {filename});
Related
I want to update one row based on multiple row value, After run the application getting error like = ( FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: UPDATE Tablename SET sFollowed=?,Code=?,Name=? WHERE Vessel_Name= )
Here is my update method in MyDbhelper class
public boolean update_isFollowed(String strVesselName , String strVesselNotationNumber, String strIsFollowed) {
SQLiteDatabase db1 = getReadableDatabase();
ContentValues values=new ContentValues();
values.put(COL_VesselName,strVesselName );
values.put(COL_ClassCodeNumber , strVesselNotationNumber);
values.put(COL_IsFollowedVessels, strIsFollowed);
int id=db1.update(Table_VesselList,values,COL_ClassCodeNumber + "=" +strVesselNotationNumber +" and "+ COL_VesselName +"="+strVesselName,null);
return id > 0;
}
Do something like this
In your activity
private UpdateDataInDB mUpdateDataInDb;
mUpdateDataInDb = new UpdateDataInDB(activity);
// call the update method with updated strings
mUpdateDataInDb.updateCommentDetails(_name, heading, comment);
In Database class
public void updateCommentDetails(String name, String updatedHeading,
String updatedComment) {
// TODO Auto-generated method stub
mDatabase = mDatabase_handler.getReadableDatabase();
ContentValues args = new ContentValues();
args.put("Heading", updatedHeading);
args.put("Comment", updatedComment);
mDatabase.update("AddCommentTable", args,"Name" + "= ?", new String[]{ name });
args.clear();
}
Use proper where condition string with values as argument. like:
String where = COL_ClassCodeNumber+"=? AND "+COL_VesselName="?";
String[] whereArgs = new String[] {String.valueOf(strVesselNotationNumber),
String.valueOf(strVesselName)};
int id=db1.update(Table_VesselList,values,where,whereArgs);
Hi in my database i have a string like "computer's" , i am fetching it from database and displaying listview, when i click on the list item it should store to one of my database table. I am doing it as below but it is getting crashed
public void updateFav(String key, int value) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
C_FAV=C_FAV.replaceAll("'","''");
values.put(C_FAV, value);
db.update(tableName, values, C_KEY + "='" + key.trim() + "'", null);
db.close();
}
Below is my trace
12-12 17:55:49.291: E/AndroidRuntime(18184): android.database.sqlite.SQLiteException: near "s":
syntax error (code 1): , while compiling: UPDATE fav SET favorite=? WHERE key='computer's'
You're escaping the apostrophes in C_FAV which seems to be a column name but not in key.
Consider using ? placeholder and bind arguments instead, e.g.
db.update(tableName, values, C_KEY + "=?", new String[] { key.trim() });
i'm trying to insert some data into a SQLite3 Database on an Android system.
This method should takes three strings and insert them into a database:
public void createEntry(String description, String reps, String weight)
{
ContentValues cv = new ContentValues();
cv.put(KEY_DESCRIPTION, description);
cv.put(KEY_REPS, reps);
cv.put(KEY_WEIGHT, weight);
sqlDB.insert(DATABASE_TABLE, null, cv);
}
The columns(KEY_DESCRIPTION,..) are all defined as TEXT NOT NULL and I also added an auto incrementing column.
In my main activity I'm using this code to get input from EditText Widgets
String description = sqlDescription.getText().toString();
then I'm creating an instance of my DB class:
DB entry = new DB(MainActivity.this);
entry.open();
entry.createEntry(description, reps, weight);
entry.close();
open() uses the SQLiteOpenHelper and sets up database:
public DB open()
{
sqlHelper = new dbHelper(sqlContext);
sqlDB = sqlHelper.getWritableDatabase();
return this;
}
But when I call this method:
entry.createEntry(description, reps, weight);
I get following error:
(1) near "Table": syntax error
E/SQLiteDatabase(19344): Error inserting Description=test Weight=tesz Reps=test
E/SQLiteDatabase(19344): android.database.sqlite.SQLiteException: near "Table": syntax error
(code 1): , while compiling: INSERT INTO Table(Description,Weight,Reps) VALUES (?,?,?)
TABLE is a reserved word in SQL; you should not use it as the name of the table.
Try changing it to:
private static final String DATABASE_TABLE = "myTable";
I am new to android , as I am trying to update data using context menu item "EDIT". Using EDIT I will go to EditText layout then I will press update Note, But it's not updating the EditText Data.
My Code Database:
public void updateNote(String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
db.update(Table_Notepad, updatenote, Col_ID + "=?", null);
db.close();
}
Activity:
case R.id.Save:
String get_title = Title_Edit.getText().toString();
String get_content = Content_Edit.getText().toString();
String get_duedate = duedatetext.getText().toString();
if(!get_title.equals("") || !get_content.equals(""))
{
if (!isEdit) {
//if it isn't edit mode we just add a new note to db
Database_Notepad db = new Database_Notepad(Add_Task.this);
db.Create_Note(get_title, get_content, get_duedate);
} else {
//if this is edit mode, we just update the old note
Database_Notepad db = new Database_Notepad(Add_Task.this);
db.updateNote(get_title, get_content, get_duedate);
db.close();
}
}
Please Suggest, I think I need to correct database code. Please suggest. Thanks
Your function updateNote need Record ID which you want to update.
Like
public void updateNote(int id, String filename, String filedata, String duedate){
instead
public void updateNote(String filename, String filedata, String duedate){
and compare that id which your entire record, it will check and update your particular record, change your function function something like below:
public void updateNote(int id, String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
db.update(Table_Notepad, updatenote, "rowid= "+id,, null);// rowid or name of your id column
db.close();
}
and pass the id too along with others parameter.
Use
db.updateNote(id, get_title, get_content, get_duedate);
instead
db.updateNote(get_title, get_content, get_duedate);
In updateNode(), you need to change this update or insert values:
public void updateNote(long rowId, String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
if (rowId >=0)
db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[] {""+rowId});
else
db.insert(Table_Notepad, null, updatenote);
db.close();
}
Then, if you are creating a new note you can call:
updateNote(-1, filename, filedata, duedate);
and if you are updating an existing one, you will have to pass in the ID:
updateNote(noteRowId, filename, filedata, duedate);.
Source: check these Android docs.
Need to pass row id to update in last argument
db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[]{id});
Happy coding
I have application, which downloads data from internet and is supposed to write it to DB using content provider.
private void addCurrency(Currency _currency){
ContentResolver cr=getContentResolver();
ContentValues values = new ContentValues();
values.put(CurrencyProvider.KEY_DATE, _currency.getDate().getTime());
values.put(CurrencyProvider.KEY_NAME, _currency.getName());
values.put(CurrencyProvider.KEY_NOMINAL, _currency.getNominal());
values.put(CurrencyProvider.KEY_VALUE, _currency.getValue());
String w=CurrencyProvider.KEY_NAME+" = "+_currency.getName();
if (cr.query(CurrencyProvider.CONTENT_URI, null, w,null , null).getCount()==0){
cr.insert(CurrencyProvider.CONTENT_URI, values);
}else
cr.update(CurrencyProvider.CONTENT_URI, values, w, null);
}
So, I'm trying either to update record if it exists, or to add new.
But I'm getting SQLite exception:
near "доллар": syntax error: , while compiling: SELECT * FROM currency WHERE (name = Австралийский доллар)
Try surrounding your name with quote
String w=CurrencyProvider.KEY_NAME+" = '"+_currency.getName() + "'";