Copy row from table to table

petrbury

Member
Licensed User
Longtime User
Hi everybody,
is there some quick way how to copy one row from one table to another ? I have a table with more than 30 collumns and need to copy some of rows to another table.
Now I have a loop where every cell of appropriate row is moved to relevant cell on the second table. It works OK, but very, very slow on my device.
So I wonder, if exists any better way to do it. Something like " Copy table1.row(n) to table2.row(y) ".
Thanks in advance
Petr
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Looking at all the table methods and properties I wouldn't think there is a better way, but I am not an expert. Maybe there is something wrong with your loop and best to post the relevant bit of code.

RBS
 

petrbury

Member
Licensed User
Longtime User
Well, this is part of my program. I don't think, there is something wrong in it, it works OK. There is only a lot of operation needed to copy rows to another table.
Petr
 

Attachments

  • Part.sbp
    959 bytes · Views: 305

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Looks OK to me, just 2 things I can think of:

1. tblSlov.RowCount
as this doesn't change in the first loop you could maybe put this count in a variable and use that instead, saving to do the count every time. I am not sure how much B4P is optimized and maybe it does this already under the bonnet, so in that case you won't gain anything.

2. If you doing a lot of rows then it might be an option to right the whole table to SQLite, do whatever needs doing in there and then write back to
the table. Or maybe if the table was based on SQLite data in the first place then you don't even need to write back to SQLite.

RBS
 

Cableguy

Expert
Licensed User
Longtime User
Looking at your code I got to ask...
Why use do while as a substitute for a For...next loop?

The code would be much cleaner..
Also, Why a seperate loop to delete the Row?

IE.

For x= 0 To Table.count
If table1.Cell("CollumnName",x)=TestValue Then
Table2.AddRow(Table1.Cell("CollumnName",x)
Table1.Remove(x)
X=X-1 ' This makes sure we came back 1 index value before the deleted ROW
Next
 

petrbury

Member
Licensed User
Longtime User
Thank you both, RBS and Paulo.
I'll try your suggestions. But I think, that the major part of time is spent on copying the row.
Because for every choosen row I must copy more then 30 columns and it means more than 30 operations
for every row ( or am I wrong ??, that's the main question ).
Petr
 

digitaldon37

Active Member
Licensed User
Longtime User
Thank you both, RBS and Paulo.
I'll try your suggestions. But I think, that the major part of time is spent on copying the row.
Because for every choosen row I must copy more then 30 columns and it means more than 30 operations
for every row ( or am I wrong ??, that's the main question ).
Petr

Do you need something like "select into"
Example: SQL SELECT INTO Statement

I don't think SQLite supports that. I'm sure you've probably looked at what is supported: Query Language Understood by SQLite
 

Cableguy

Expert
Licensed User
Longtime User
Because for every choosen row I must copy more then 30 columns and it means more than 30 operations
for every row ( or am I wrong ??, that's the main question ).
Petr

Not quite true..
Table.AddRow(([Value1, Value2,...])
Wich mean you can opy all values at once from one table to another
 

petrbury

Member
Licensed User
Longtime User
I understand you, but as I have written, I have more than 30 columns. So I was finding, if there is not simplier way to do it.
Maybe it could be some suggestion for Erel to add RowCopy and probably TableCopy command. I'll write it to the "Wish" section.
Petr
 

Scubaticus

Active Member
Licensed User
I agree with petrbury and also would like a simple way to copy data from one var to another, not only for tables, but also for structures.

@Cableguy
I use a temporary table to retrieve my sql results into and sometimes want a copy of that table on screen. I don't see how table.copy could help me as I also want to set the col types and not just put in the values.

And how to supply the values for the table.copy(val1, val2) ....
Do I have to create an array first? The values come from anothertable.cell(x,y) which are filled from a Cmd.ExecuteTable

I don't think my code would be clearer after bunches of for next loops just for a simpel copy .....

Modules are very nice, but without proper support for copying vars between them it's not as powerfull as it could be i.m.h.o

Well, lets see what future releases will bring!
 

Rioven

Active Member
Licensed User
Longtime User
sample

Hi petrbury,

I Just had an exercise...see sample attached.

This is just a simple table search from top to bottom of the table. It stops when found because I used do..loop.

I had a method on my old codes to search a big table by using 2 or more pointers, e.g. pointer 1 could begin at top and other begins from buttom, etc..inside the loop then searching could be faster.

This is excerpt from example...appreciate comments and improving this code...
B4X:
Sub SearchCopyRow(T1,Search,T2,Row2) 'Search item on every column of every Row of T1
Row1=-1
m=-1
   Do 
   m=m+1
      n=-1
      Do
      n=n+1
         If Search=Control(T1,table).Cell(cn(n),m) Then Row1=m
         
      Loop Until n=Control(T1,table).ColCount-1 OR Row1<>-1 'stops when item found at column
   
   Loop Until m=Control(T1,table).RowCount-1 OR Row1<>-1 'stops when item found at Rows

If Row1<>-1 Then
CopyRow(T1,Row1,T2,Row2)
End If

End Sub

B4X:
Sub CopyRow(T1,Row1,T2,Row2) ' T1=table1 , T2=table2
   For i=0 To Control(T1,table).ColCount-1
         Control(T2,table).Cell(cn(i),Row2)=Control(T1,table).Cell(cn(i),Row1)
         Control(T1,table).Cell(cn(i),Row1)="" 'clear cells
   Next
End Sub
 
Last edited:

petrbury

Member
Licensed User
Longtime User
Hi Rioven,

thank you for your response. Your idea of using two or more pointers for searching looks nice, I've joted it down for future.

Nowadays in my program I already don't use copying tables (because of slowness of my PPC). I've changed this part of program and now I operate only with temporary arrays, in which I have pointers to my source table. So instead of operating with table I operate with arrays and it is notably faster.

Btw. I found a workaround how to speed up copying of whole table. If the table is big, it is faster to save it and load again to target table. Maybe it helps somebody.

Petr
 

Kintara

Member
Licensed User
Longtime User
Copying Tables

The Idea of using arrays is tempting.

The thing about saving table1 and reloading it to table 2 is that table 2 will be a full copy of table 1. If table 2 was to be a copy of the filtered table then saving table 1 would negate the filter i.e. the full table would be saved.

Kinata :cool:
 
Top