I got this code in one of my methods. It should check if the string is already in the database and if it is the db entry gets updated. If it is not there I catch the exception and just create the entry.
String s="hallo";
try {
Cursor c1 = dba.fetchSubject(s);
dba.updateSubject(c1.getLong(c1.getColumnIndex(DbAdapter.KEY_ROWID)), s, t, r);
} catch (SQLException e){
dba.createSubject(s, t, r);
}
The problem is fetchSubject allway throws an Exception.
I use a similar method to get a db entry by a rowId instead of a String, which is working:
public Cursor fetchSubject(long rowId) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SUBJECT, KEY_TEACHER, KEY_ROOM}, KEY_ROWID
+ "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The method called on top:
public Cursor fetchSubject(String subject) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SUBJECT, KEY_TEACHER, KEY_ROOM}, KEY_SUBJECT
+ "=" + subject, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The error:
sqlite returned: error code = 1, msg = no such column: hallo
I don't know where my error is. Isn't it possible to use a string at the selection parameter?
Try adding single quotes around hallo
String s = "'hallo'";
Essentially what you're doing is telling your query: select blah blah blah from TABLE where subject = hallo. Since hallo isn't surrounded by quotes, it is trying to find a variable / column with that name. When we surround it with quotes, we are saying that we are looking for the string within these quotes, rather than a variable.
Think of it like normal programming..
String s = hallo;
is completely different than
String s = "hallo";
Related
I am a beginner in Android. I created a database with unique id, name, email etc. I want to retrieve the specific data from the database using the unique id. I don't know what to do. I read the previous answers but didn't get any idea from that.
At first you should read SQL Database
Below code is DEMO
public String getSpecificResult(int getID) // you should pass getID from your Class
{
SQLiteDatabase db = this.getWritableDatabase();
String get_Status = "0";
String selectQuery = "SELECT your_coloum_ FROM "+ TABLE_NAME + " WHERE " + YOUR_UNIQUE_KEY + " = '"+ getID +"'";
Cursor c = db.rawQuery(selectQuery, null);
if (c != null && c.moveToFirst()) {
get_Status = c.getString(0); // Return 1 selected result
}
db.close();
return get_Status;
}
Try this..
pass ur unique_id to get the contents.. alter names according to your table.
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_NUMBER}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
when i perfom search method its given me run time error
error:- 05-04 14:04:00.227: E/AndroidRuntime(4559): Caused by:
android.database.sqlite.SQLiteException: no such column: ww (code 1): ,
while compiling: SELECT DISTINCT faculty, deparment, name, officeNumber,
phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name=ww
I need to search on teacher name when user enter the name and display all information about this teacher such as name ,faculty,deparment,phone,email,officenumber,officehour
thiss getRecord method in DBAdapter.java
public Cursor getRecord(String n1) throws SQLException
{
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "=" + n1, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
and this search method in Information.java
public void search(){
db.open();
Cursor c = db.getRecord(n);
if (c.moveToFirst()){
t1.setText(c.getString(0));
t2.setText(c.getString(1));
t3.setText(c.getString(2));
t4.setText(c.getString(3));
t5.setText(c.getString(4));
t6.setText(c.getString(5));
t7.setText(c.getString(6));
}
else
Toast.makeText(this, "this Dr not found", Toast.LENGTH_LONG).show();
db.close();
}
this Oncreate method in DBAdapter.java
public void onCreate(SQLiteDatabase db)
DATABASE_CREATE =
"create table if not exists teacherInfo (teacherNumber INTEGER primary key autoincrement,"
+ "name TEXT not null, faculty TEXT,deparment TEXT,officeNumber INTEGER,officeHour TEXT, emailAddress TEXT,phoneNumber TEXT, location INTEGER );";
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "= ?", new String[] { n1 }, null, null, null, null);
Strings have to be in quotes.
The problem is that you missed the quotes of the string!
Better use the String arguments like that:
public Cursor getRecord(String n1) throws SQLException
{
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc,namec,officeNumberc,Phonec,emailAddressc,officeHourc},
namec + "= ? ",
new String[] {ww}, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
By that way you avoid any errors and also string special characters are auto escaped!
Your query isn't sanitized. It should be:
SELECT DISTINCT faculty, deparment, name, officeNumber,
phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name="ww"
You are entering wrong query. Change it to ->
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},"name = " + n1, null, null, null, null, null);
I'm now getting problem about how to convert capitalize word to lowercase in SQLite for Android.
public Cursor fetchFunctions(String searchword) throws SQLException {
Cursor mCursor =
mDb.query(true, FUNCTIONS_TABLE, new String[] {
FUNS_WORD, FUNS_BODY}, FUNS_WORD + "='" + searchword + "'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
I want to conver like LOWER(FUNS_WORD) + "='" + searchword + "'" unfortunately, getting syntax error. What I want is I want to convert all of data in FUNS_WORD to lowercase type.
You'll have to use rawQuery
public Cursor fetchFunctions(String searchword) throws SQLException {
Cursor mCursor =
mDb.rawQuery("SELECT FUNS_WORD, FUNS_BODY FROM FUNCTIONS_TABLE WHERE LOWER(FUNS_WORD) = '" + searchword + "'", null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
You can use the LIKE keyword in place of =. The LIKE keyword is case insensitive and can be used in your case without % sign.
There is a SQL injection vulnerably in the query '" + searchword + "'. This could allow an attacker to compromise the database.
Cursors should be wrapped in try/finally blocks to ensure they are always closed upon completion.
public Cursor fetchFunctions(String searchword) throws SQLException {
Cursor cursor = null;
try {
cursor = mDb.rawQuery("SELECT FUNS_WORD, FUNS_BODY FROM FUNCTIONS_TABLE WHERE LOWER(FUNS_WORD) = ?", new String[] { searchword });
if (cursor != null) {
cursor.moveToFirst();
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return cursor;
}
When i try to search in row POINTA (text data type in SQLite) and I compare it to a String the program stops. This is the code:
public Cursor getpoints(String start,String end) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_PRIM,
NAME,
POINTA,
POINTA_LANG,
POINTA_LAT,
POINTB,
POINTB_LANG,
POINTB_LAT
},
POINTA +"=" +start,//here is the problem
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
...
Same problem as this, you need single quotes around your start String, i.e.
POINTA + "='" + start + "'",
I am trying to do a check if the User-name entered by the user exists in the DB.
public Cursor checkUsername(String username) throws SQLException {
Cursor mCursor = db.query(true, TABLE_USERS, new String[] { ID,
KEY_NAME, KEY_USERNAME}, KEY_USERNAME + "="
+ username, null, null, null, null, null);
if (mCursor != null) {
return true;
}
return false;
}
When i return true or false i am getting an Error saying
Type mismatch: cannot convert from boolean to Cursor
i just want to return true or false from the DBAdaptor back to the Activity.
Your function returns a Cursor
public Cursor checkUsername()
Either change it to return a boolean, or return a cursor.
Try reformatting your sql query and provide the 'where' clause as one of the parameters in the database call :
public Cursor getRoute(long rowIndex)
{
String where = KEY_ID + "=" + rowIndex;
return db.query(TBL_ROUTES, null, where, null, null, null, null);
}
Also, remember to close your cursor when you are finished with it, otherwise you will get other exceptions.