Converting a value in an sqlite query in android/java -
i having problem querying database has multiple tables. there 2 tables.
table 1:
student id (primary key)
first name
last name
school id (an int: numbered 1-3 3 schools listed in table 2)
table 2:
school_id (primary key)
school name (a string)
since there 3 entries in table 2 match school id value table 1 output actual school name listed in table 2.
here section of code have far.. when student id entered.. return result such "24 smith john 3" 3 read bayside corresponding value in table 2.
public string getdata(string snumber) { sqlitedatabase db = helper.getwritabledatabase(); string[] columns = {databasehelper.student_id, databasehelper.student_first_name, databasehelper.student_last_name, databasehelper.student_school}; cursor cursor=db.query(databasehelper.student_table, columns, databasehelper.student_id+" = '"+snumber+"'", null, null, null, null); stringbuffer buffer = new stringbuffer(); while(cursor.movetonext()){ int index1=cursor.getcolumnindex(databasehelper.student_id); int index2=cursor.getcolumnindex(databasehelper.student_first_name); int index3=cursor.getcolumnindex(databasehelper.student_last_name); int index4=cursor.getcolumnindex(databasehelper.student_school); string stext = cursor.getstring(index1); string sfirstname = cursor.getstring(index2); string slastname = cursor.getstring(index3); string sschool = cursor.getstring(index4); buffer.append(stext + " " + slastname + " " +sfirstname+ " " +sschool+ "\n" ); } return buffer.tostring(); }
use inner join
select t1.idstudent, t1.firstname, t1.lastname, t2.name table1 t1 inner join table2 t2 on t2.id = t1.idschool t1.idschool = id_t2;
warning
table1 = name of student table
table2 = name of school table
idschool= name of school primary key
or
you can use orm
http://satyan.github.io/sugar/
https://github.com/satyan/sugar
https://www.youtube.com/watch?v=9vxil7gkntc
i sugar, try it
Comments
Post a Comment