This is my android SQL statement
I try to update the allergy table, I encounter a lot of problem
stmt1.executeUpdate("UPDATE [dbo].[allergy]
SET [allergy]= "+allergyname+ ",[reaction] = "+reaction+ ",[notes] =" +notes+ " Where patientID ="+patientId);
no matter what I enter into my "allergyname"
It will say Invalid column name
what is happening?
Your values should be in single quotes. Try below query it will work:
stmt1.executeUpdate("UPDATE [dbo].[allergy]
SET [allergy]= '"+allergyname+ "',[reaction] = '"+reaction+ "',[notes] ='" +notes+ "' Where patientID ="+patientId);
Happy coding!!!!!!!!!!
Related
I am trying to delete a row from my table if 2 columns equal to what the user entered.
E.g. I have 2 textfields in which the user entered something in both e.g. "chicken" and in the other textfield "car". I want to delete the row in which those 2 values are in a row. I think it will be something like: delete from ~tablename~ where food = chicken AND vehicle = car.
Im not sure how to write that in sqlite in android.
I have my SQLitedatabase object and have called the delete method on it, but not sure what to put in the parameters
EDIT = I've managed to do it. Thanks for the below answers but this is how I've done it:
sqlitedb.delete("Random", "food =? AND vehicle=? ", new String[]{tv.getText.toString(),tv1.getText.toString()});
tv and tv1 are textfields in my case. Random is my table's name.
The sql query will look like -
String sqlQuery = "DELETE FROM <table_name> WHERE food = '"+ <food_name> + "' AND vehicle = '" + <vehicle_name> + "'";
You want something like:
String table_name=~tablename~;
String table_column_one=food;
String table_column_two=vehicle;
database.delete(table_name,
table_column_one + " = ? AND " + table_column_two + " = ?",
new String[] {"chicken", "car"});
Check SQLiteDatabase's documentation on delete function for more info.
SQLite accepts conditionals in the WHERE clause as regular SQL.
String updateQuery = "UPDATE Bookdetails SET lastchapter = " + test + " WHERE bookpath=" +sentFilename;
db.execSQL(updateQuery);
Error:
03-04 13:36:23.997: I/System.out(9722): android.database.sqlite.SQLiteException:
near "/": syntax error: , while compiling: UPDATE Bookdetails SET lastchapter =
mukund WHERE bookpath=/mnt/sdcard/Download/What's so great about the doctrine of
grace.epub errors happens
the error is posted above
My table contains the field id, bookpath and lastchapter, book path contains the values
/mnt/sdcard/Download/What's so great about the doctrine of grace.epub
/mnt/sdcard/Download/1b4fdaac-f31d-41e8-9d15-26c9078d891f.epub
/mnt/sdcard/Download/Commentary on Romans.epub
and lastchapter contains the values nothing nothing nothing
id contains 1 2 3
why is the error occurring at "/" there is no hash in my update query it is only there at string which stores bookpath? Is this an error?
String literals in SQL need to be in '' quotes.
However, it's better to use ? placeholders for literals like this:
String updateQuery = "UPDATE Bookdetails SET lastchapter=? WHERE bookpath=?";
db.execSQL(updateQuery, new String[] { test, sentFilename });
I believe your lastchapter & bookpath is of type String (TEXT). Hence when you are adding or updating it's value you should always use ' ( Single cot ) around it. Change your query to this,
String updateQuery = "UPDATE Bookdetails SET lastchapter ='" + test + "' WHERE bookpath='" +sentFilename + "'";
db.execSQL(updateQuery);
However, Direct Execution of SQL query is not advisable at developer.android.com hence you can use alternative way like below code,
String updateQuery = "UPDATE Bookdetails SET lastchapter=? WHERE bookpath=?";
db.execSQL(updateQuery, new String[] { test, sentFilename });
I am developing an android app where I want to delete the last row in one of my database table. I have tried the code below, but its throwing a syntax error.
public void deletelatestprofilefromsystemsettings()
{
String maxid = System_id + "="+"SELECT MAX ("+System_id+") FROM" +TABLE_SYSTEM_SETTINGS;
getWritableDatabase().delete(TABLE_SYSTEM_SETTINGS, maxid ,null);
}
Please help! Thanks!
You are lacking a space after the FROM, and subqueries must be written in parentheses:
String maxid = System_id + "=" +
"(SELECT MAX("+System_id+") FROM " + TABLE_SYSTEM_SETTINGS + ")";
You are trying to execute a DELETE with a SELECT in the same query. AFAIK you shouldn't do it. You have to execute the SELECT query first, in order to retrieve the desired id, then execute the deletion. In other words, execute Cursor c = getWritableDatabase().query(), read the id from the cursor, then use it in getWritableDatabase().delete().
Also, add a space after ") FROM", so it becomes ") FROM " in order to avoid a syntax error.
in a table SQLite. I have a list of strings that I see is in a listview by button I delete each record.
But if for example in the written record and the word "caffè" everything works fine, but if it is written the word " caffe' "the app crashes why?
thanks
String nome = tv.getText().toString();
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(NomeTable.TABLE_NAME, NomeTable.NOME_CAT + "='" + nome_cat + "'", null);
db.close();
finish();
This is because the ' is a special character in SQL.
So you end up with ='caffe'' which is invalid due to double ''.
You want to instead use the whereArgs param as well.
db.delete(NomeTable.TABLE_NAME, NomeTable.NOME_CAT + "= ?", new String[]{nome_cat});
That will escape your ' character for you and shouldn't mess you the SQL.
I would think nome_cat contains some symbols not alowed by the SQL syntax. You might want to use SQLiteQueryBuilder to build your query.
I'm trying to delete a row in a table where I know the row exists. I tried:
final String s = "DELETE from %s where Name = '%s'";
String sql = String.format(s, GetTableName(), sListName);
Cursor cr= GetUserDb().GetSQLiteDb().rawQuery(sql, null);
and I tried:
String sWhere = String.format("Name = '%s'", sListName);
long lRowsUpdated = GetUserDb().GetSQLiteDb().delete(GetTableName(), sWhere, null);
** sWhere >> Name = 'groceries' **
** sql >> DELETE from Lists where Name = 'groceries' **
There is no crash or logcat exception but the row is still not deleted. Is there something I'm missing? Maybe I have a lock on it somewhere else or I need to set certain permissions in my Manifest or something?
Use delete() from SqliteDatabase - this returns the count of affected rows. E.g. delete(tablename, "name=?", new String[] { aString } )
Try this:
SQLiteStatement stmt = db.compileStatement("DELETE FROM " + getTableName() + "WHERE Name=?");
stmt.bindString(1, sListName);
stmt.execute();
if the column you're trying to have your key by is sListName you should have delete where sListName = '%s' unless you have another column that is called Name that you are trying to delete, but if that isn't your primary key you might end up getting two rows in your delete.
You can always use the emulator and an adb shell and just go run the sqlite3 shell command and try your SQL statements. If you type yours in and get 0 rows affected, you know you're statement is messed up, not your java.
If you're not using any built in CurorAdapters or ContentProviders your primary key does not need to be named _ID, it can be named whatever. As for the type, in SQLite3, it's really just a suggestion on how to cast it, you can put whatever data in whatever column you want.