Saving data from db to excel

sacad

Member
Licensed User
Longtime User
Hi can someone maybe please help me?
I read many posts but could not find anything that work. I found a post that states that you can use officeworker but the link seems not to be working in order to download the library.

This is what I want to do:
I have a screen that capture information that is inputted in text boxes and then stored to a database using the dbutils functionality (dbutils.insertmaps)

I want to use this stored information to save it to an excel file and if new information is added, update the same excel spreadsheet.

Can someone please assist me to posts or with sample code of what the best approach will be in order to solve this.

Thank you some much
Regards
Sacad
 

sacad

Member
Licensed User
Longtime User
Looping through DB and saving to excel

Thank you Erel did see that one but thought you cannot modify a excel file already created

Will play around with it and if I struggle I will get back to you. :)

I tried this post but seems to me that the headers are only string values and it does not save any values that is in my db.
http://www.b4x.com/forum/basic4android-updates-questions/24990-database-db-csv.html#post144802

How do you loop through a database that you created through dbutils?
I want to select all my data that is stored in table and go through each record and then use that excel values to save each one using something like this you created:

For col = 0 To table1.NumberOfColumns - 1
For row = 0 To table1.Size - 1
Dim cell As WritableCell
cell.InitializeText(col, row + 1, table1.GetValue(col, row))
cell.SetCellFormat(rowsFormat)
sheet1.AddCell(cell)
Next
Next


Then you can just save the last cell value position in db and then select that value to continue to save data or go to next line?

Here is my code for this activity.
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim SQL As SQL

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
'My text boxes and spinner
Dim SpnPerson As Spinner
Dim txtAssignment As EditText
Dim txtDwelling As EditText
Dim Txtphone As EditText
Dim txtPhysical As EditText
Dim txtPSUNumber As EditText
Dim TxtQuestionare As EditText
Dim Button1 As Button
'Dim WebView1 As WebView
Dim WebView1 As WebView
Dim db As db 'Dims a variable to use for the db class
db.Initialize(Me) 'Initializes the db object for use, pass Me so the class will know the calling Activity
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
'Create DB
SQL.Initialize(File.DirRootExternal,"Food.db",True)
End If

'Do not forget to load the layout file created with the visual designer. For example:
'Load layout and add data to spinners
Activity.LoadLayout("SectionA")
SpnPerson.Add("1")
SpnPerson.Add("2")
SpnPerson.Add("3")
SpnPerson.Add("4")
SpnPerson.Add("5")
SpnPerson.Add("6")
SpnPerson.Add("7")
SpnPerson.Add("8")
SpnPerson.Add("9")
SpnPerson.Add("10")

'create my db attributes
Dim m As Map
m.Initialize
m.Put("ID",DButils.DB_TEXT)
m.Put("PSUNumber",DButils.DB_TEXT)
m.Put("Assignment",DButils.DB_TEXT)
m.Put("DwellingUnitNumber",DButils.DB_TEXT)
m.Put("DwellingID",DButils.DB_TEXT)
m.Put("Telephonenumber",DButils.DB_TEXT)
m.Put("TotalPersons",DButils.DB_TEXT)
m.Put("Questionarenumber",DButils.DB_TEXT)
DButils.CreateTable(SQL,"SectionA",m,"ID")


'ShowTableInWebView

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub SpnPerson_ItemClick (Position As Int, Value As Object)

End Sub
Sub Button1_Click
'After data is entered and the continue button is clicked it will save the data to database
Dim SectionAlist As List
SectionAlist.Initialize
Dim m As Map
m.Initialize
m.Put("ID",Null)
m.Put("PSUNumber",txtPSUNumber.Text)
m.Put("Assignment",txtAssignment.Text)
m.Put("DwellingUnitNumber",txtDwelling.Text)
m.Put("DwellingID",txtPhysical.Text)
m.Put("Telephonenumber",Txtphone.Text)
m.Put("TotalPersons",SpnPerson.SelectedItem)
m.Put("Questionarenumber",TxtQuestionare.Text)
SectionAlist.Add(m)
DButils.InsertMaps(SQL,"SectionA",SectionAlist)
End Sub

Thank you so much for all your help!
 
Upvote 0

sacad

Member
Licensed User
Longtime User
Thank Erel I do no know what you mean with code tags?????

We will do something like this
'Declare a new variable

Dim I as integer

I created a list and then do something like this

'Create a new list
Dim test as list

'Declare the list
test = DButils.ExecuteMemoryTable(SQL,"Select *from SectionA",Null,0)

Now I want to loop through the list and take each value and do something with it but when I get the results it gives me a funny java result. I checked with breakpoints and the values will show as 0=1, 1=Carmen etc but as soon as I do something like toastmessage.show(Result,true) the result does not show the value

This is what Ive done:

Dim values() as string
values=test.get(0)

toastmessageshow(values,true)
msgbox(values,"This is a test")

I even tried just making values a string and not an array and still not working

What am I doing wrong?
 
Upvote 0

sacad

Member
Licensed User
Longtime User
Thank you Erel
Have not done arrays in a long while but now I know!
Thank you for the quick responses and excellent service!:sign0188:
 
Upvote 0

Devan

Member
Licensed User
Longtime User
H
Looping through DB and saving to excel

Thank you Erel did see that one but thought you cannot modify a excel file already created

Will play around with it and if I struggle I will get back to you. :)

I tried this post but seems to me that the headers are only string values and it does not save any values that is in my db.
http://www.b4x.com/forum/basic4android-updates-questions/24990-database-db-csv.html#post144802

How do you loop through a database that you created through dbutils?
I want to select all my data that is stored in table and go through each record and then use that excel values to save each one using something like this you created:

For col = 0 To table1.NumberOfColumns - 1
For row = 0 To table1.Size - 1
Dim cell As WritableCell
cell.InitializeText(col, row + 1, table1.GetValue(col, row))
cell.SetCellFormat(rowsFormat)
sheet1.AddCell(cell)
Next
Next


Then you can just save the last cell value position in db and then select that value to continue to save data or go to next line?

Here is my code for this activity.
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim SQL As SQL

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
'My text boxes and spinner
Dim SpnPerson As Spinner
Dim txtAssignment As EditText
Dim txtDwelling As EditText
Dim Txtphone As EditText
Dim txtPhysical As EditText
Dim txtPSUNumber As EditText
Dim TxtQuestionare As EditText
Dim Button1 As Button
'Dim WebView1 As WebView
Dim WebView1 As WebView
Dim db As db 'Dims a variable to use for the db class
db.Initialize(Me) 'Initializes the db object for use, pass Me so the class will know the calling Activity
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
'Create DB
SQL.Initialize(File.DirRootExternal,"Food.db",True)
End If

'Do not forget to load the layout file created with the visual designer. For example:
'Load layout and add data to spinners
Activity.LoadLayout("SectionA")
SpnPerson.Add("1")
SpnPerson.Add("2")
SpnPerson.Add("3")
SpnPerson.Add("4")
SpnPerson.Add("5")
SpnPerson.Add("6")
SpnPerson.Add("7")
SpnPerson.Add("8")
SpnPerson.Add("9")
SpnPerson.Add("10")

'create my db attributes
Dim m As Map
m.Initialize
m.Put("ID",DButils.DB_TEXT)
m.Put("PSUNumber",DButils.DB_TEXT)
m.Put("Assignment",DButils.DB_TEXT)
m.Put("DwellingUnitNumber",DButils.DB_TEXT)
m.Put("DwellingID",DButils.DB_TEXT)
m.Put("Telephonenumber",DButils.DB_TEXT)
m.Put("TotalPersons",DButils.DB_TEXT)
m.Put("Questionarenumber",DButils.DB_TEXT)
DButils.CreateTable(SQL,"SectionA",m,"ID")


'ShowTableInWebView

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub SpnPerson_ItemClick (Position As Int, Value As Object)

End Sub
Sub Button1_Click
'After data is entered and the continue button is clicked it will save the data to database
Dim SectionAlist As List
SectionAlist.Initialize
Dim m As Map
m.Initialize
m.Put("ID",Null)
m.Put("PSUNumber",txtPSUNumber.Text)
m.Put("Assignment",txtAssignment.Text)
m.Put("DwellingUnitNumber",txtDwelling.Text)
m.Put("DwellingID",txtPhysical.Text)
m.Put("Telephonenumber",Txtphone.Text)
m.Put("TotalPersons",SpnPerson.SelectedItem)
m.Put("Questionarenumber",TxtQuestionare.Text)
SectionAlist.Add(m)
DButils.InsertMaps(SQL,"SectionA",SectionAlist)
End Sub

Thank you so much for all your help!
I
Hi can someone maybe please help me?
I read many posts but could not find anything that work. I found a post that states that you can use officeworker but the link seems not to be working in order to download the library.

This is what I want to do:
I have a screen that capture information that is inputted in text boxes and then stored to a database using the dbutils functionality (dbutils.insertmaps)

I want to use this stored information to save it to an excel file and if new information is added, update the same excel spreadsheet.

Can someone please assist me to posts or with sample code of what the best approach will be in order to solve this.

Thank you some much
Regards
Sacad
Hi Sacad, Good evening. Just want to check with you. Did you manage to figure out how to convert SQL database into Excel. I have been trying for a few days, but still can't figure out. I manage to convert into .csv format. But i need to convert into excel format. Pls help me. Tq have a nice day.
 
Upvote 0
Top