2014年5月17日星期六

StringBuffer sqlite

yesterday sent a ArrayList how to get rid of ; [ ] and , in the post, God used a big way , replaced StringBuffer, successfully export the table contents
public static StringBuffer searchstudents (String xueh) {
StringBuffer stringBuffer = new StringBuffer ();
try
{
createOrOpenDatabase ();
String sql = "select * from students where stu_xueh = '" + xueh + "'";
Cursor cur = sld.rawQuery (sql, new String [] {});
while (cur.moveToNext ())
{
stringBuffer.append (" Name :"). append (cur.getString (0) + " ; \ n ");
stringBuffer.append (" Sex :"). append (cur.getString (1) + " ; \ n ");
stringBuffer.append (" Hometown :"). append (cur.getString (2) + " ; \ n ");
stringBuffer.append (" nation :"). append (cur.getString (3) + " ; \ n ");
stringBuffer.append (" Professional :"). append (cur.getString (4) + " ; \ n ");
stringBuffer.append (" Department :"). append (cur.getString (5) + "\ n");
stringBuffer.append (" Class :"). append (cur.getString (6) + " ; \ n ");
stringBuffer.append (" Student ID :"). append (cur.getString (7) + "\ n");
stringBuffer.append (" Date of birth :"). append (cur.getString (8) .. + "-") append (cur.getString (9) + "-") append (cur.getString (10));
}
cur.close ();
closeDatabase ();
}
catch (Exception e)
{
e.printStackTrace ();
}
return stringBuffer;
}
is in studengts table inside information found through xueh
Then I built a table kecheng
String sql2 = "create table if not exists kecheng (banji varchar2 (20), zhouyiyi varchar2 (20 ) ) " ;
want banji check kecheng exterior things , methods of operation and the students exactly the same table
public static StringBuffer searchkecheng (String banji) {
StringBuffer stringBuffer = new StringBuffer ();
try
{
createOrOpenDatabase ();
String sql = "select * from kecheng where banji = '" + banji + "'";
Cursor cur = sld.rawQuery (sql, new String [] {});
while (cur.moveToNext ())
{
stringBuffer.append (cur.getString (0) + "\ n");
stringBuffer.append (cur.getString (1) + "\ n");
}
cur.close ();
closeDatabase ();
}
catch (Exception e)
{
e.printStackTrace ();
}
return stringBuffer;
}
But to
while (cur.moveToNext ())
{
stringBuffer.append (cur.getString (0) + "\ n");
stringBuffer.append (cur.getString (1) + "\ n");
}
cur.close ();
closeDatabase (); this cycle time, skip directly to stringbuffer assignment to cur.close ( ) ; led me stringbuffer fail to get value , I checked a few hours , the operation mode is obviously exactly the same twice , why not get a second time that cycle , seeking great God help us to see
------ Solution --------------------------------------------
cycle is not into very simple, that is based on your pass in this banji not find data , you can copy the sql go to tools investigation,
remember that banji the value you pass as a condition select * from kecheng where banji = (banji value you pass ) ,
If you can not find the problem , then it is not your data in the program . Best to be a non-empty closed before another judge to close the resource .
if (cur! = null) {
cur.close ();
}
------ For reference only --------------------------------- ------
first pass will be successful , have no access to cycle a second time , a temporary matter, do first with . . .


没有评论:

发表评论