Storing data from an entire row, Android SQLite - java

So I have a method that allows me to get the id of a certain item by using a name i already have in an SQL Database. How would I go about getting the entire row of information and storing each item in its own variable.
Method that only works with ID
public Cursor getID(String name){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query = " SELECT " + COL1 + " FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = sqLiteDatabase.rawQuery(query, null);
return data;
}
And the method that gets the query and stores the result
Cursor data = mydb2.getID(name);
int itemId= -1;
while(data.moveToNext()){
itemId = data.getInt(0);
}
Using this method below how would i store all of the data in its own variable using this (or any other way to get data of entire row).
public Cursor rowData(String name){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query = " SELECT * FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = sqLiteDatabase.rawQuery(query, null);
return data;
}
I know this might be a dumb question, and I have tried looking at other questions, I have gotten this far I just don't know what to do next (I'm very new to Sq Lite databases and Android development)

You'd use something like :-
Cursor csr = instance_of_yourdbhelper.rowData();
while(csr.moveToNext()) {
long thisItemId = csr.getLong(csr.getColumnIndex(yourdbhelper.COL_1));
String thisName = csr.getString(csr.getColumnIndex(yourdbhelper.COL_2));
// etc for other columns
}
Notes
yourdbhelper is the class for your DatabaseHelper which is the class that extends SQLiteOpenHelper (the assumption is that the above methods are from such a class, as this is a common usage of SQLite)
instance_of_yourdbhelper is the instance of the DatabaseHelper class i.e. you may have yourdbhelper dbhlpr = new yourdbhelper(parameters);, in which case dbhlpr is the instance.
This just overwrites the same variables (thisItemId and thisName) for each row in the cursor, although there is perhaps the likliehood that the rowData method only returns one row.
You will likely encounter fewer errors using the getColumnIndex method as it returns the offset according to the column name. Miscalculated offsets is a frequent cause of problems.
You may wish to handle a no rows returned situation in which case you can use cursor.getCount(), which will return the number of rows in the cursor.

Related

Cannot get the max value in a SQLite database

I am trying to get the max id of a table in SQLite in Android. This is how I get it in the database helper:
public int getMaxIncrementation() {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String result = "0";
try{
Cursor cursor = sqLiteDatabase.rawQuery("SELECT MAX(id) FROM " +TABLE_INCREMENTATION, new String[]{});
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndex(INCREMENTATION_ID));
}catch (Exception ex) {
Log.e("ex", ex.getMessage());
}
return Integer.parseInt(result);
};
This is how I created the table:
private static final String CREATE_TABLE_INCREMENTATION = "CREATE TABLE IF NOT EXISTS " + TABLE_INCREMENTATION
+ "("
+ INCREMENTATION_DATEADDED + " TEXT,"
+ INCREMENTATION_DIRECTION + " INTEGER,"
+ INCREMENTATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ INCREMENTATION_USER + " TEXT"
+ ")";
This is the snapshot of my table with data in it:
On debugging, I always get this error:
Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor
is initialized correctly before accessing data from it.
First, since you don't have in the query any ? placeholders then you don't need to pass any parameters to the sql statement, so the 2nd argument of rawQuery() should be null.
Also you must give an alias to the column that your query returns and use it to get the returned value (you can't use INCREMENTATION_ID because it is not the name of the column returned):
Cursor cursor = sqLiteDatabase.rawQuery("SELECT MAX(id) AS maxid FROM " +TABLE_INCREMENTATION, null);
if (cursor.moveToFirst())
result = cursor.getString(cursor.getColumnIndex("maxid"));
Or if you don't use an alias then use 0 as there is only 1 column returned:
Cursor cursor = sqLiteDatabase.rawQuery("SELECT MAX(id) FROM " +TABLE_INCREMENTATION, null);
if (cursor.moveToFirst())
result = cursor.getString(0);

Android SQLite error on cursor

I'm currently working on an android project and am stuck on this method. The cursor query works but anything after that does not. If I take out the 'returnCompany' method it runs so I'm guessing the error is inside the cursor query. The method is taking in a string parameter called 'companyNameParemeter'. FYI Android Studio is the text editor I'm using.
SQLiteDatabase db = this.getReadableDatabase();
String returnString = "";
Cursor cursor = db.rawQuery("Select * from " + CONTANT_INFO_AND_GPS_TABLE + " where " +
KEY_COMPANY_NAME + " = ? ", new String[] {companyNameParamater});
company returnCompany = new company(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
cursor.getString(2), cursor.getString(3),Double.parseDouble(cursor.getString(4) ),
Double.parseDouble(cursor.getString(5)));
/* returnString = ("Company is:\t" + returnCompany.getCompanyName() + ",\nContact name:\t" +
returnCompany.getContactName() + ",\tContact No:\t" + returnCompany.getContactNumber()
+ ",\nGPS:\tLat:\t"
+ returnCompany.getGPS_Latitude() + "\tLong:\t" + returnCompany.getGPS_Longitude() );*/
return returnString;
I checked LogCat and the only relevant error I could find was this
04-04 12:06:38.237 2032-1714/? E/GCoreUlr: Received null location result
Try using
cursor.moveToFirst()
Right AFTER creating and filling it with db.rawquery or wichever call you use to get your data from your DB,
It will make sure your cursor is pointing at the first element you received, i may be wrong but i remember it's always pointing at the end of the data it contains when you fill it.
The error you show doesn't reflect the error you would typically get. However the code you have shown is clearly wrong and would result in an error because you are trying read a row when you are positioned at the start position of the cursor (aka beforeFirstRow).
You have to move to a row before you can get the data. As you only appear to have a single row, then using the Cursor method moveToFirst will either position you at the first (only) row. If the move cannot be made (no rows) then like all the Cursor move????? methods (moveTolast, moveToNext etc) a boolean false is returned (true if the move was made) hence it's use in within the if.
SQLiteDatabase db = this.getReadableDatabase();
String returnString = "";
Cursor cursor = db.rawQuery("Select * from " + CONTANT_INFO_AND_GPS_TABLE + " where " + KEY_COMPANY_NAME + " = ? ", new String[] {companyNameParamater});
if (cursor.moveToFirst()) { //<<<< Move the cursor to a row
company returnCompany = new company(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
Double.parseDouble(cursor.getString(4)),
Double.parseDouble(cursor.getString(5)));
/* returnString = ("Company is:\t" + returnCompany.getCompanyName() + ",\nContact name:\t" +
returnCompany.getContactName() + ",\tContact No:\t" +
returnCompany.getContactNumber()
+ ",\nGPS:\tLat:\t"
+ returnCompany.getGPS_Latitude() + "\tLong:\t" +
returnCompany.getGPS_Longitude() );
*/
}
return returnString;
P.S. Double.parseDouble(cursor.getString(4)) could be cursor.getDouble(4)

Getting specific data by column using sqlite database

I couldn't give a good title to the problem I'm having, but basically I have two columns Name and MaxNum string and int respectively!
I need int method that makes a query which retrieves the MaxNum for specific Name that I give in parameter!
.. so if I have this data
ID | Name | MaxNum
0 | Mike | 50
1 | John | 40
2 | Jess | 30
..when I put Jess in as parameter it will return 30 !
My method so far.. but I can't use it since it doesn't return int value !
public void maxFromName(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "=" + name ;
db.execSQL(query);
}
You have to get the result using
Cursor
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "=" + name ;
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
return c.getInt(c.getColumnIndex("MaxNum"));
Update
Since you are facing the problem, I suggest you use the below query:
String query = Select MaxNum from azkar WHERE Name ='Jess';
after this use my code from Cursor c line, this will work.
Use rawQuery for getting data from SQLite. And from the cursor get integer value using getInt function.
Change your maxFromName function with the following.
public int maxFromName(String name){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "= '" + name +"'" ;
Cursor c = db.rawQuery(query, null);
if(c.getCount()!=0){
c.moveToFirst();
return c.getInt(c.getColumnIndex("MaxNum"))
}
return 0;
}
In the API it says the following in the description of execSQL():
Execute a single SQL statement that is NOT a SELECT or any other SQL
statement that returns data.
Use a query method that returns a Cursor type in order to retrieve results
The easiest way to get a single value from a database is with the stringForQuery() or longForQuery() helper functions:
public long maxFromName(String name) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT "+COLUMN_MAX+" FROM "+TABLE_AZKAR+" WHERE "+COLUMN_NAME+" = ?";
return DatabaseUtils.longForQuery(db, query, new String[]{ name });
}
(To prevent string formatting problems and SQL injection attacks, always use parameters instead of inserting string values directly into the query.)
You can use rawQuery
public ArrayList<String> maxFromName(String name){
ArrayList<String> maxNums = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE "
+COLUMN_NAME+ "=" + name ;
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
do { // if that kind of rows are more then one, this metod get all of them
maxNums.add(c.getInt(c.getColumnIndex("MaxNum")));
}
return maxNums;
}

How to check whether there is a record satisfying some condition in a sqlite table

I have the following code
public int getIngCount(String id,String ingrediant) {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS2 + " WHERE id = "+id+ " AND ingrediant = "+ingrediant;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
// return count
return cursor.getCount();
}
But when there is no records satisfying the condition, it causing an exception (no such column) instead of returning 0.
pls help me to find a solution. Thanks in advance.
ingrediant should be treated as a string literal and not as an identifier such as a column name. In SQL you use single quotes for string literals. It's a better idea to use ? placeholders for binding literal values, though:
String countQuery = "SELECT * FROM " + TABLE_CONTACTS2 + " WHERE id = "+id+ " AND ingrediant = ?";
Cursor cursor = db.rawQuery(countQuery, new String[] { ingrediant });
Also, don't call any cursor methods after closing it.

Android SQLite SELECT Query

I have taken String value from a EditText and set it inside SELECT QUERY after WHERE condition
As
TextView tv = (TextView) findViewById(R.id.textView3);
EditTextet2 et = (EditText) findViewById(R.id.editText1);
String name = et.getText().toString();
Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE name = '"+name+"'", null);
c.moveToNext();
tv.setText(c.getString(c.getColumnIndex("email")));
But it doesn't work. Any suggestions?
Try trimming the string to make sure there is no extra white space:
Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE TRIM(name) = '"+name.trim()+"'", null);
Also use c.moveToFirst() like #thinksteep mentioned.
This is a complete code for select statements.
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT column1,column2,column3 FROM table ", null);
if (c.moveToFirst()){
do {
// Passing values
String column1 = c.getString(0);
String column2 = c.getString(1);
String column3 = c.getString(2);
// Do something Here with values
} while(c.moveToNext());
}
c.close();
db.close();
Try using the following statement:
Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE name = ?", new String[] {name});
Android requires that WHERE clauses compare to a ?, and you then specify an equal number of ? in the second parameter of the query (where you currently have null).
And as Nambari mentioned, you should use c.moveToFirst() rather than c.moveToNext()
Also, could there be quotations in name? That could screw things up as well.
Here is the below code.
String areaTyp = "SELECT " +AREA_TYPE + " FROM "
+ AREA_TYPE_TABLE + " where `" + TYPE + "`="
+ id;
where id is the condition on which result will be displayed.
public User searchUser(String name) {
User u = new User();
SQLiteDatabase db = this.getWritableDatabase(); //get the database that was created in this instance
Cursor c = db.rawQuery("select * from " + TABLE_NAME_User+" where username =?", new String[]{name});
if (c.moveToLast()) {
u.setUsername(c.getString(1));
u.setEmail(c.getString(1));
u.setImgUrl(c.getString(2));
u.setScoreEng(c.getString(3));
u.setScoreFr(c.getString(4));
u.setScoreSpan(c.getString(5));
u.setScoreGer(c.getString(6));
u.setLevelFr(c.getString(7));
u.setLevelEng(c.getString(8));
u.setLevelSpan(c.getString(9));
u.setLevelGer(c.getString(10));
return u;
}else {
Log.e("error not found", "user can't be found or database empty");
return u;
}
}
this is my code to select one user and one only
so you initiate an empty object of your class then
you call your writable Database
use a cursor in case there many and you need one
here you have a choice Use : 1-
if (c.moveToLast()) { } //it return the last element in that cursor
or Use : 2-
if(c.moveToFirst()) { } //return the first object in the cursor
and don't forget in case the database is empty you'll have to deal with that in my case i just return an empty object
Good Luck
Detailed answer:
String[] selectionArgs = new String[]{name};
Cursor c = db.rawQuery(
"SELECT * FROM " + tabl1 +
" WHERE " + name + " = ? ", selectionArgs
);
selectionArgs : this takes the 'name' you desire to compare with, as argrument.
Here note "A Cursor object, which is positioned before the first entry of the table you refer to".
So,to move to first entry :
c.moveToFirst();
getColumnIndex(String ColumnName) : this returns the zero-based column index for the given column name.
tv.setText(c.getString(c.getColumnIndex("email")));
In case, you want to go searching through multiple rows for a given name under 'name' column then use loop as below:
if (cursor.moveToFirst()){
do{
////go traversing through loops
}while(cursor.moveToNext());
}
This should solve the problem.

Categories

Resources