How to fetch user details from a SQLite database having two tables? - android

I am trying to fetch the user details in my Navigation drawer. I have two different tables, one has username and course already stored and the other has id and email of the user taken during registration. I want to display the user name, id, course and email. I do have a user class with id, email and password for registering but not name and course. How should i fetch data from this class?
The below method is for fetching the username from table having same id. I want to display this on navigation drawer. How should i call the function and display the result?
public String getUserNameFromSID(String sid) {
String rv = "";
String whereclause = KEY_SID + "=?";
String[] whereargs = new String[]{sid};
SQLiteDatabase db = this.getReadableDatabase();
Cursor csr = db.query(TABLE_USERINFO,null,whereclause,whereargs,null,null,null);
if (csr.moveToFirst()) {
rv = csr.getString(csr.getColumnIndex(KEY_NAME));
}
csr.close();
return rv;

You can use a JOIN, (I believe from previous questions that SID is in both tables so this is what you would use as the basis for the JOIN).
So you could use something like SELECT * FROM userinfo JOIN userreg ON userinfo.sid = userreg.sid
Note that just using the column name sid would be ambiguous and hence the need to prefix the column with the table name. As it stands you will have to sid columns in the result (this shouldn't be an issue as they will both be the same value but not specifying an alias using AS cab be problematic for columns with the same name).
To utilise the query convenience method the JOIN needs to be part of the 1st (table) parameter.
So the above would be coded as :-
public String getUserNameFromSID(String sid) {
String rv = "";
String joinclause = " JOIN " + TABLE_USERREG + " ON " + TABLE_USERINFO + "." + KEY_SID + " = " + TABLE_USERREG + "." + KEY_SID; //<<<<<<<<<< ADDED
String whereclause = KEY_SID + "=?";
String[] whereargs = new String[]{sid};
SQLiteDatabase db = this.getReadableDatabase();
Cursor csr = db.query(TABLE_USERINFO + joinclause,null,whereclause,whereargs,null,null,null); //<<<<<<<<<< CHANGED
if (csr.moveToFirst()) {
rv = csr.getString(csr.getColumnIndex(KEY_NAME));
}
csr.close();
return rv;
}
Note the above is in-principle code as such
variable names and identifiers may not reflect the actual code and thus may need to be changed.
the code has not been run or tested and may therefore contain some minor errors.
The above only returns the username, you cannot return multiple distinct values, you would therefore need to return an object e.g. a User object to encompass the multiple values. You would get the values to set the object in a similar way as rv is set above.
Additional re comment :-
The thing is, that i would need to pass the Sid parameter to the
function which i can only do in the register/login page. How then can
i pass this value to the navigation page?
Instead of pages the term used for Android is Activities. You can pass data from one activity to another called/started/invoked activity using Intent Extras after instantiating an Intent in preparation for the call. That is if the activity is to be directly called from the parent activity (registration activity in your case).
If for example the registration activity were called from an initial activity and returns from that activity then you still use an intent to return the values, but the registration activity should be called/started/invoked using startActivityForResult. The activity starting the registration activity should override the onActivityResult method. The registration activity sets intent extras and uses setResult to indicate the result code and then intent to be returned.
There's plenty of examples on Stack overflow e.g. Sending data back to the Main Activity in Android

Related

Sql Query to retrieve a particular data from particular column and row in android?

I want to fetch phone number linked to particular email in the database. I am not able to find the query for it or how
public String getContactNumber(String email){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER + " FROM " + TABLE_USER + " WHERE " + email + " = " + COLUMN_USER_EMAIL;
Cursor cursor = db.rawQuery(query,null);
//What to put here to extract the data.
String contact = cursor.getString(get);
cursor.close();
return contact;
}
to extract the data. Completely a beginner
Try this ..
public List<String> getMyItemsD(String emailData) {
List<String> stringList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT COLUMN_USER_MOBILE_NUMBER FROM " + USER_TABLE_NAME + " WHERE email= " + emailData;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
String name = (c.getString(c.getColumnIndex("Item_Name")));
stringList.add(name);
c.moveToNext();
}
}
return stringList;
}
public String getContactNumber(String email){
String contact = "";
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER + " FROM " + TABLE_USER + " WHERE " + email + " = " + COLUMN_USER_EMAIL;
Cursor cursor = db.rawQuery(query,null);
if(cursor.getCount()>0) {
cursor.moveToNext();
contact = cursor.getString(cursor.getColumnIndex(COLUMN_USER_MOBILE_NUMBER));
}
//What to put here to extract the data.
cursor.close();
return contact;
}
From this method you get phone number value of that email which you pass any other method easily.
I'd suggest the following :-
public String getContactNumber(String email){
String contact = "NO CONTACT FOUND"; //<<<<<<<<<< Default in case no row is found.
SQLiteDatabase db = this.getWritableDatabase(); //<<<<<<<<<< Generally getReadable gets a writable database
String[] columns_to_get = new String[]{COLUMN_USER_MOBILE_NUMBER};
String whereclause = COLUMN_USER_EMAIL + "=?";
String[] whereargs = new String[]{email};
Cursor cursor = db.query(TABLE_USER,columns_to_get,whereclause,whereargs,null,null,null);
//What to put here to extract the data.
if (cursor.moveToFirst()) {
contact = csr.getString(csr.getColumnIndex(COLUMN_USER_MOBILE_NUMBER));
}
cursor.close();
return contact;
}
The above does assumes that there will only be 1 row per email (which is most likely).
Explanations
A default value is set so that you can easily tell if an invalid/non-existent email is passed (you'd check the return value if need be (might be easier to simply have "" and check the length as a check)).
getReadableDatabase has been replaced with getWritableDatabase as unless there are issues with the database a writable database will be returned, as per :-
Create and/or open a database. This will be the same object returned
by getWritableDatabase() unless some problem, such as a full disk,
requires the database to be opened read-only. In that case, a
read-only database object will be returned. If the problem is fixed, a
future call to getWritableDatabase() may succeed, in which case the
read-only database object will be closed and the read/write object
will be returned in the future.
getReadableDatabase
Note no real problem either way;
The recommended query method has been used instead of the rawQuery method. This has distinct advantages, it builds the underlying SQL and also offers protection against SQL injection (just in case the email passed is input by a user).
this version of the method takes 7 parameters :-
The table name as a string
The columns to be extracted as an array of Strings (aka String array). null can be all columns.
The where clause less the WHERE keyword with ?'s to represent arguments (see next). null if no WHERE clause.
The arguments to be applied (replace ?'s 1 for 1) as a String array. null if none or no WHERE clause.
The GROUP BY clause, less the GROUP BY keywords. null if no GROUP BY clause.
The HAVING clause, less the HAVING keyword. null if no HAVING clause.
The ORDER BY clause, less the ORDER BY keywords. null if no ORDER BY clause.
SQLiteDatabase - query
- Note there are 4 query methods (see link for the subtle difference, I believe this is the most commonly used)
The data extraction is the new code. When a Cursor is returned it is at a position BEFORE THE FIRST ROW, so you need to move to a valid row. So the moveToFirst* method is suitable (note that if a move cannot be made by a move method that it will return false, hence how you can say if (cursor.moveToFirst())). The data is then extracted from the appropriate column use the **getString method, which takes an int as an argumnet for the column offset (0 in this case). However, using hard coded values can lead to issues so the getColumnIndex method is used to get the offset according to the column name (-1 is returned if the named column is not in the Cursor).

sql always return the same data

In my project, I use SQL (SQLite since its android) to save my data.
I encountered some odd problem:
In my application, I have three tabs and in order to know which data belong to which tab I have in my chart a column for each tab. the number in the column (Integer) represent if and how many of that data suppose to be in this tab.
So, when the tab initialized, it reads from the chart, and using the relevant column, it can tell which data needs to be read and how many.
When I retrieve data from the server (the database of the server is not related to the problem at hand), I check if the data is new or that I already have a similar one in my SQL DB. If its new, I add it to the SQL chart and put 1 in the relevant column. If it already exists, it checks if the data in the SQL is updated (it update the data if necessary) and add 1 to the relevant column (the same column it puts 1 in it in case the data is not in the SQL DB).
now here's my problem:
when it reads from the SQL DB to see the number in the column, so it can add 1 to it and then update the chart, it always return 1 regardless the actual number that in the column in that moment (I know its not really 1 because when I read from the DB in the same column from other places in my app it does read the actual number).
Since in other places in my app it doesn't happen I tried to see what is the difference between the places but I did not find anything wrong (the only difference is that I used WritableDatabase instead of just ReadableDatabase in that time but that should not be an issue as far as I know, at least not in such case).
the code where the problem occurs (the problem is with COL_CART):
writable = db.getWritableDatabase();
Cursor cursor = writable.query(
ShopContract.ShopChart.TABLE_NAME,
new String[]{ShopContract.ShopChart.COL_NAME, ShopContract.ShopChart.COL_PRICE, ShopContract.ShopChart.COL_PIC, ShopContract.ShopChart.COL_CART},
ShopContract.ShopChart.COL_PROD_ID + " = '" + product.getProd_id() +"'",
null,
null,
null,
null
);
if(cursor.moveToFirst()){
//check if the data match, if not, replace.
if(!cursor.getString(cursor.getColumnIndex(ShopContract.ShopChart.COL_NAME)).equals(product.getName())){
ContentValues values = new ContentValues();
values.put(ShopContract.ShopChart.COL_NAME,product.getName());
update(values);
}
if(cursor.getDouble(cursor.getColumnIndex(ShopContract.ShopChart.COL_PRICE)) != product.getPrice()){
ContentValues values = new ContentValues();
values.put(ShopContract.ShopChart.COL_PRICE,product.getPrice());
update(values);
}
if(!cursor.getString(cursor.getColumnIndex(ShopContract.ShopChart.COL_PIC)).equals(product.getPicture())){
ContentValues values = new ContentValues();
values.put(ShopContract.ShopChart.COL_PIC,product.getPicture());
update(values);
}
//check how many there are already in cart (already in the cursor) and update it to be ++
Log.d(TAG, "run: the number in col_cart is: " + cursor.getInt(cursor.getColumnIndex(ShopContract.ShopChart.COL_CART)));
int inCart = cursor.getInt(cursor.getColumnIndex(ShopContract.ShopChart.COL_CART)) + 1; // because of the new product we just added
Log.d(TAG, "run: inCart = " + inCart);
ContentValues values = new ContentValues();
values.put(ShopContract.ShopChart.COL_CART,inCart);
Log.d(TAG, "run: change cart");
update(values);
edit:
here is the update method code (the writable is being initialized in the code above):
private SQLiteDatabase writable;
private void update(ContentValues values){
writable.update(ShopContract.ShopChart.TABLE_NAME, values, ShopContract.ShopChart.COL_PROD_ID + " = '" + product.getProd_id() +"'",null);
}

Obtaining table names for sqlite SQL join in android

I have a need to join standard android's tables (like contacts and call log) using SQL. It is possible using the rawQuery or query methods of SQLiteDatabase class. But for the methods to work properly I need to know table names that I can provide in a raw SQL query.
Example. I want to execute query like this:
SELECT * FROM Contacts as c INNER JOIN Call_Log as l ON c.number=l.number
I know how to get field names (like CallLog.Calls.NUMBER), but I don't know how to get the name of a standard table that every android has. It is possible to hardcode the name, but the way with something like CallLog.TABLE_NAME looks much more reliable. So, where can I find an analogue of CallLog.TABLE_NAME?
Your asking for a lot of info, but this is a good summation of how to access the contacts table and how to create your own SQL table and update it with information you get from other tables.
To do any type of search of the Contacts Provider, your app must have READ_CONTACTS permission. To request this, add this element to your manifest file as a child element of :
<uses-permission android:name="android.permission.READ_CONTACTS" />
To do any type of search of the Call Log, your app must have READ_CALL_LOG permission. To request this, add this element to your manifest file as a child element of :
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Code below on how to access Phone Call History
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.
This technique tries to match a search string to the name of a contact or contacts in the Contact Provider's ContactsContract.Contacts table. You usually want to display the results in a ListView, to allow the user to choose among the matched contacts.
Saving data to a database is ideal for repeating or structured data, such as contact information. This class assumes that you are familiar with SQL databases in general and helps you get started with SQLite databases on Android. The APIs you'll need to use a database on Android are available in the android.database.sqlite package.
One of the main principles of SQL databases is the schema: a formal declaration of how the database is organized. The schema is reflected in the SQL statements that you use to create your database. You may find it helpful to create a companion class, known as a contract class, which explicitly specifies the layout of your schema in a systematic and self-documenting way.
A contract class is a container for constants that define names for URIs, tables, and columns. The contract class allows you to use the same constants across all the other classes in the same package. This lets you change a column name in one place and have it propagate throughout your code.
A good way to organize a contract class is to put definitions that are global to your whole database in the root level of the class. Then create an inner class for each table that enumerates its columns.
public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// make the constructor private.
private FeedReaderContract() {}
/* Inner class that defines the table contents */
public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
}
}
Once you have defined how your database looks, you should implement methods that create and maintain the database and tables. Here are some typical statements that create and delete a table:
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_SUBTITLE + TEXT_TYPE + " )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
Just like files that you save on the device's internal storage, Android stores your database in private disk space that's associated application. Your data is secure, because by default this area is not accessible to other applications.
A useful set of APIs is available in the SQLiteOpenHelper class. When you use this class to obtain references to your database, the system performs the potentially long-running operations of creating and updating the database only when needed and not during app startup. All you need to do is call getWritableDatabase() or getReadableDatabase().
To use SQLiteOpenHelper, create a subclass that overrides the onCreate(), onUpgrade() and onOpen() callback methods. You may also want to implement onDowngrade(), but it's not required.
For example, here's an implementation of SQLiteOpenHelper that uses some of the commands shown above:
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
To access your database, instantiate your subclass of SQLiteOpenHelper:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
Put Information into a Database
Insert data into the database by passing a ContentValues object to the insert() method:
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);
// Insert the new row, returning the primary key value of the new row
long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);
The first argument for insert() is simply the table name.
The second argument tells the framework what to do in the event that the ContentValues is empty (i.e., you did not put any values). If you specify the name of a column, the framework inserts a row and sets the value of that column to null. If you specify null, like in this code sample, the framework does not insert a row when there are no values.
To read from a database, use the query() method, passing it your selection criteria and desired columns. The method combines elements of insert() and update(), except the column list defines the data you want to fetch, rather than the data to insert. The results of the query are returned to you in a Cursor object.
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedEntry._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_SUBTITLE
};
// Filter results WHERE "title" = 'My Title'
String selection = FeedEntry.COLUMN_NAME_TITLE + " = ?";
String[] selectionArgs = { "My Title" };
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
To look at a row in the cursor, use one of the Cursor move methods, which you must always call before you begin reading values. Generally, you should start by calling moveToFirst(), which places the "read position" on the first entry in the results. For each row, you can read a column's value by calling one of the Cursor get methods, such as getString() or getLong(). For each of the get methods, you must pass the index position of the column you desire, which you can get by calling getColumnIndex() or getColumnIndexOrThrow(). For example:
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedEntry._ID)
);

SQLite Database Error(Glitch in bridge between two tables)

I have two tables as USER_TABLE where i have all the user details as username,firstname etc and similarly i have another table named RESUME_TABLE where i have aim,degree,university etc.
The columns common to both are KEY_USERID(created dynamically) and i use this id to match the two tables.
Question :
I register a user and his details gets stored in the USER_TABLE and say his KEY_USERID automatically generated(primary key) is 1 and then i create a resume for him in the RESUME_TABLE using the same KEY_USERID 1. I pass on the KEY_USERID obtained from the first table as a argument while inserting and retrieving the resume contents from the RESUME_TABLE.
Say, i do this again for another user thereby generating KEY_USERID 2 but i fill only the
USER_TABLE and the RESUME_TABLE IS EMPTY.
When i repeat this again filling both the tables, then the RESUME_TABLE is not able to fetch the data cause the second user did not fill the details in RESUME_TABLE and that acts as a glitch while retrieving the KEY_USERID for the 3rd user.
How do i rectify this ?
CODE :
DBAdapter.java :
public String get_user_id(String name) {
String[] column = new String[]{KEY_USERID};
String where = KEY_USERNAME + "=?";
Cursor c = mDB.query(DATABASE_TABLE, column, where,new String[]{""+ name + ""}, null, null,null);
String user_id = "";
int iuser_id = c.getColumnIndex(KEY_USERID);
if (c != null)
c.moveToFirst();
user_id = user_id + c.getString(iuser_id) + "\t";
c.close();
return user_id;
}
ViewResume.java
if(get_from_create!=null)
{
if(get_from_create.hasExtra("UserId_Create"))
{
String user_id_create = get_from_create.getExtras().getString("UserId_Create"); // This is where i get the user id from the first table which is created dynamically.
dbAdapter.open();
Log.i("In_View Resume_from_create",user_id_create);
objective.setText(dbAdapter.getObjective(user_id_create));
degree.setText(dbAdapter.getDegree(user_id_create));
passed_out.setText(dbAdapter.getPassedOut(user_id_create));
university.setText(dbAdapter.getUniversity(user_id_create));
field.setText(dbAdapter.getField(user_id_create));
years_of_experience.setText(dbAdapter.getyears_of_experience(user_id_create));
areas_of_interest.setText(dbAdapter.getareas_of_interest(user_id_create));
dbAdapter.close();
}
NOTE:
KEY_USERID is auto incremented only for USER_TABLE and not for RESUME_TABLE.
You can't have two automatic keys referring same elements on different tables.
You should only have automatic primary key in USER_TABLE. KEY_USERID can not be automatic, it must alwys be matched with USER_TABLE.KEY_USERID - effectively, a foreign key.
Furthermore, to understand why you are getting wrong id in code, you must post get_from_create function.

Odd database error: Empty values?

I'm creating an activity that views a record and displays it's data to a user to modify. After everything is done just click a button to save the data back into the database. However, once saving time comes around I get an unusual "java.lang.IllegalArgumentException: Empty values" error. I think it's a SQL code error on my end, but I don't know enough about SQL to find it. Can I get a couple of eyes to see what I did wrong?
Thanks
~Aedon
Here is where the save button is declared and the listener is set. mBoundService is the service that hosts the database and it's calls. DevicesTable is a class that holds the table and column names. DevicesTable.gt_fields[0] is autoincrementID and then serial, name, and first seen columns
public void init() {
mDevName = (EditText)findViewById(R.id.dv_name);
mCurReads = (TextView)findViewById(R.id.dv_readings);
mSave = (Button)findViewById(R.id.dv_save);
mSave.setOnClickListener(new OnClickListener() {
#Override public void onClick(View arg0) {
mBoundService.updateRecord(DevicesTable.gt_name, mId, DevicesTable.g_fields[2], mDevName.getText().toString());
}
});
}
And this is the database call itself.
/**
* Updates a record at the given table with the given record.
* #param table The table to update
* #param id The id of the actual record. Do not pass the incorrect id!
* #param column The column in which the data is changing
* #param data The data that is to be changed to
*/
public void updateRecord(String table, String id, String column, String data) {
mDB.update(table, null, "SET " + column + " = '" + data + "',", new String[]{"id=" + id});
}
You need to pass in a ContentValues object to tell the database what values go in what columns in the updated row(s). The strings in the third and fourth arguments make up a WHERE clause that indicate what row(s) should be updated. For example:
ContentValues values = new ContentValues();
values.put("foo", 123);
values.put("bar", 456);
db.update("some_table", values, "id=789", null);
This would be equivalent to issuing the query
UPDATE some_table SET foo = 123, bar = 456 WHERE id = 789
Your code attempts to stuff the entire query into the WHERE clause, and update() stops you in your tracks because the values argument is null.

Categories

Resources