i need help with sql,

116 views
Skip to first unread message

christo garces

unread,
Nov 17, 2015, 3:35:22 AM11/17/15
to DroidScript
Hi

i am able to write data to a sql db, but my question is as follows...

i have text fields say txt1,txt2,txt3

in my select statement how can i retrieve data and push it to those txt fields


and secondly if i try write a spinner textfield to the db it gives me a spannable error

Chris Hopkin

unread,
Nov 17, 2015, 4:36:37 AM11/17/15
to DroidScript
Hi Christo

Have you looked at the Database sample? It shows you how to make a select query and set the results into a Text control. You could modify this to write each field from the result into separate Text controls.

Can you post your code that demonstrates the error with the spinner text?

Thanks

Chris

christo garces

unread,
Nov 17, 2015, 6:52:15 AM11/17/15
to DroidScript
hi 
regards the first issue il gv an example

txt1 = app.CreateText(""); //SAY APPLES
txt2 = app.CreateText(""); //SAY BANANAS
 ive created a db and have written these values to the db....checked and all is good

now i wana at another point in the app load these values from the db.
db.ExecuteSql("SELECT * FROM mydb WHERE blabla.......
i want apples to appear in txt1 and bananas in txt2

the second issue

txt1 = app.CreateSpinner("1,2,3,4,5,6,7");

if i try write this value into the db, when i run debug it gives me a spannable error

christo garces

unread,
Nov 17, 2015, 6:53:53 AM11/17/15
to DroidScript
do you perhaps have an example of how i could modify the code to receive data to a few text fields

christo garces

unread,
Nov 18, 2015, 1:21:07 AM11/18/15
to DroidScript
hi does anybody have any help?

christo garces

unread,
Nov 18, 2015, 1:29:36 AM11/18/15
to DroidScript
hi chris

im a newbie to droidscript, im from a vb background, so im just not understanding the process. ive used the code that youve assisted another user with, but the issue is that it returns all of the fields in one string eg 1,draw,great, i need to put the 1 into its own text field, same as the draw and the great

Steve Garman

unread,
Nov 18, 2015, 1:55:17 AM11/18/15
to DroidScript
If you want help with your spinner problem, you need to post the code you are using to try to insert it into the database

If you have a comma separated list, one way to split it into different textboxes would be

var txt1,txt2,txt3;
function OnStart()
{
    var lay = app.CreateLayout( "linear", "VCenter,FillXY" );    

    txt1 = app.CreateText( "1" );
    lay.AddChild( txt1 );
    txt2 = app.CreateText( "2" );
    lay.AddChild( txt2 );
    txt3 = app.CreateText( "3" );
    lay.AddChild( txt3 );
    
    app.AddLayout( lay );
    var s ="1,draw,great";
    showVals(s);
}

function showVals(s)
{
    var arr = s.split(",");
    txt1.SetText(arr[0]);
    txt2.SetText(arr[1]);
    txt3.SetText(arr[2]);
}

christo garces

unread,
Nov 18, 2015, 2:10:55 AM11/18/15
to DroidScript
tx steve i will post the code snippets for both my issues as son as i get home. thank you very much

christo garces

unread,
Nov 18, 2015, 4:44:35 AM11/18/15
to DroidScript
function OnStart()
{

db =app.OpenDatabase("/sdcard/school");

db.ExecuteSql("CREATE TABLE IF NOT EXISTS studrec"+
"(id integer primary key, "+
"firstname text, "+
"surname text, "+
"idnumber text, "+
"class text, "+
"teacher text)");


layit = app.CreateLayout( "Absolute" );
app.ShowPopup("Student Personal Details");
    btn = app.CreateButton( "Next",0.1,0.1 ,"Alum");
btn.SetOnTouch(fwdstud);
btn.SetPosition(0.5,0.9);
btn1 = app.CreateButton( "Back",0.1,0.1 ,"Alum");
btn1.SetOnTouch(fwdstud);
btn1.SetPosition(0.4,0.9);
btn2 = app.CreateButton( "Save",0.1,0.1 ,"Alum");
btn2.SetOnTouch(fwdstud);
btn2.SetPosition(0.6,0.9);
btn3 = app.CreateButton( "Remove",0.1,0.1 ,"Alum");
btn3.SetOnTouch(fwdstud);
btn3.SetPosition(0.3,0.9);
text = app.CreateText(" First Name",0.1,0.05);
text.SetPosition(0.1,0.2);
txt1 = app.CreateTextEdit("",0.2,0.07);

txt1.SetPosition(0.2,0.2);
txt2 = app.CreateText("Surname",0.1,0.1);
txt2.SetPosition(0.1,0.3);
txt3 = app.CreateTextEdit("",0.2,0.07);
txt3.SetPosition(0.2,0.3);
txt4 = app.CreateText("ID Number",0.1,0.1);
txt4.SetPosition(0.1,0.4);
txt5 = app.CreateTextEdit("",0.2,0.07);
txt5.SetPosition(0.2,0.4);
txt6 = app.CreateText("Grade",0.1,0.1);
txt6.SetPosition(0.1,0.5);
txt7 = app.CreateSpinner("1,2,3,4,5,6,7,8,9,10,11,12" ,0.2,0.07);
txt7.SetPosition(0.2,0.5);
//spn1.SetOnChange(nil);
txt8 = app.CreateText("Teacher",0.1,0.1);
txt8.SetPosition(0.1,0.6);
txt9 = app.CreateTextEdit("",0.2,0.07);
txt9.SetPosition(0.2,0.6);
txt10 = app.CreateText("Ethnicity",0.1,0.1);
txt10.SetPosition(0.1,0.7);
txt11 = app.CreateSpinner("Black,White,Indian,Coloured",0.2,0.07);
txt11.SetPosition(0.2,0.7);
txt12 = app.CreateText("Religion",0.1,0.1);
txt12.SetPosition(0.1,0.8);
txt13 = app.CreateSpinner("Christian,Hindu,Tamil,Mormon,Catholic,Atheist,Other,None",0.2,0.07);
txt13.SetPosition(0.2,0.8);
txt14 = app.CreateText("Address",0.1,0.1);
txt14.SetPosition(0.5,0.2);
txt15 = app.CreateTextEdit("",0.2,0.07);
txt15.SetPosition(0.6,0.2);
txt16 = app.CreateTextEdit("",0.2,0.07);
txt16.SetPosition(0.6,0.3);
txt17 = app.CreateTextEdit("",0.2,0.07);
txt17.SetPosition(0.6,0.4);
txt18 = app.CreateText("Disabilities",0.1,0.1);
txt18.SetPosition(0.5,0.5);
txt19 = app.CreateSpinner("Yes,No",0.2,0.07);
txt19.SetPosition(0.6,0.5);
txt20 = app.CreateText("Parent Name",0.1,0.1);
txt20.SetPosition(0.5,0.6);
txt21 = app.CreateTextEdit("",0.2,0.07);
txt21.SetPosition(0.6,0.6);
txt22 = app.CreateText("Contact No",0.1,0.1);
txt22.SetPosition(0.5,0.7);
txt23 = app.CreateTextEdit("",0.2,0.07);
txt23.SetPosition(0.6,0.7);
txt24 = app.CreateText("Alternate No",0.1,0.1);
txt24.SetPosition(0.5,0.8);
txt25 = app.CreateTextEdit("",0.2,0.07);
txt25.SetPosition(0.6,0.8);


layit.AddChild(text);
layit.AddChild(txt2);
layit.AddChild(txt1);
layit.AddChild(txt3);
layit.AddChild(txt4);
layit.AddChild(txt5);
layit.AddChild(txt6);
layit.AddChild(txt7);
layit.AddChild(txt8);
layit.AddChild(txt9);
layit.AddChild(txt10);
layit.AddChild(txt11);
layit.AddChild(txt12);
layit.AddChild(txt13);
layit.AddChild(txt14);
layit.AddChild(txt15);
layit.AddChild(txt16);
layit.AddChild(txt17);
layit.AddChild(txt18);
layit.AddChild(txt19);
layit.AddChild(txt20);
layit.AddChild(txt21);
layit.AddChild(txt22);
layit.AddChild(txt23);
layit.AddChild(txt24);
layit.AddChild(txt25);

    layit.AddChild( btn );
layit.AddChild ( btn1 );
layit.AddChild ( btn2 );
layit.AddChild ( btn3 );
   app.AddLayout( layit );

}

function fwdstud()
{
item1=txt1.GetText();
item2=txt3.GetText();
item3=txt5.GetText();
item4=txt7.GetText();
item5=txt9.GetText();

db.ExecuteSql("INSERT INTO studrec"+
"(firstname, "+
"surname, "+
"idnumber, "+
"class, "+
"teacher)"+
"VALUES (?,?,?,?,?)",
[item1,
item2,
item3,
item4,
item5],null,OnError );

retr()



}
function OnError()
{
app.Alert( "Error: " + msg );   
    console.log( "Error: " + msg );  
}

function retr()  
{  
   
   
  db.ExecuteSql( "SELECT * from studrec WHERE surname = 'garcez' ",[],OnR)

}  

function OnR(results)
{
var s = "";
 var len = results.rows.length;
for(var i = 0;i < len;i++)
{
var item = results.rows.item(i)
s = getPropertyString(item) + "\n";
txt1.SetText(s);
txt3.SetText(s);
}
    }   
   





function getPropertyString( object )
{
return Object.keys(object).map( function(key) { return object [key]; } ).join(",");
}

Steve Garman

unread,
Nov 18, 2015, 10:45:48 AM11/18/15
to DroidScript
Are you getting errors when you run that code?

If so, what do the errors say and what do you have to do to produce them?

reza azer

unread,
Nov 18, 2015, 2:04:27 PM11/18/15
to DroidScript
Hi Christo

I tryed your code.I found no errors.it works

regards

Steve Garman

unread,
Nov 19, 2015, 1:05:49 AM11/19/15
to DroidScript
Perhaps this code will give you some idea what is going on when you read from the database.

function OnStart()
{
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );    

    txt1 = app.CreateText( "1",-1,-1,"Multiline" );
    lay.AddChild( txt1 );
    txt2 = app.CreateText( "2",-1,-1,"Multiline" );
    lay.AddChild( txt2 );
    txt3 = app.CreateText( "3",-1,-1,"Multiline" );
    lay.AddChild( txt3 );
    btn=app.CreateButton("Read");
    btn.SetOnTouch(doRead);
    lay.AddChild(btn);
    
    //Add layout to app.    
    app.AddLayout( lay );
    db =app.OpenDatabase("/sdcard/school");
}

function doRead()
{   
  db.ExecuteSql( "SELECT * from studrec WHERE surname = 'garcez' ",[],OnR)

}  

function OnR(results)
{
   var s1 = "",s2="",s3="",item;
   var len = results.rows.length;
   for(var i = 0;i < len;i++)
   {
      item = results.rows.item(i)
      s1 += item.firstname + "\n";
      s2 += item.surname + "\n";
      s3 += item.idnumber + "\n";
   } 
   txt1.SetText(s1);
   txt2.SetText(s2);
   txt3.SetText(s3);
}   

christo garces

unread,
Nov 19, 2015, 1:13:11 AM11/19/15
to DroidScript
hi steve

Thank you very much for that piece of code you gave me, it has sorted my problem with retrieving data from db.

if i run the code i get spe.txt.spannablestring cannot be cast to java.lang.string , the error occurs because of the spinner, but im not sure why?

christo garces

unread,
Nov 19, 2015, 1:17:58 AM11/19/15
to DroidScript
hi

thats so strange, if you run in debug mode and change the value in the spinner, i get a spannable error

Steve Garman

unread,
Nov 19, 2015, 1:23:37 AM11/19/15
to DroidScript
Is there a line of your code that is highlighted when you get that error?

If so, what is the exact text of that line of code?

christo garces

unread,
Nov 23, 2015, 3:35:02 AM11/23/15
to DroidScript
spn.gettext()

warning spn.gettext failed (android.text.spannablestring cannot be cast to java.lang.string)
Reply all
Reply to author
Forward
0 new messages