PDA

View Full Version : Insert Table data in SQLite database


sside
10-01-2004, 01:29 PM
Hallo everybody

I'm trying to insert data from simple text file into sqlite database. I have 3 text files and i want to insert their contents into a database. The way how i do it, it works but the trouble is that the bigger the content is, more lines of code. This is how it looks like.
***********************************
file1.txt file2.txt file3.txt
item1-1 item2-1 item3-1
item1-2 item2-2 item3-2
item1-3 item2-3 item3-3
***********************************
***********************************
tbl_item1 = TextFile.ReadToTable("AutoPlay\\Docs\\1.txt");
tbl_item2 = TextFile.ReadToTable("AutoPlay\\Docs\\2.txt");
tbl_item3 = TextFile.ReadToTable("AutoPlay\\Docs\\3.txt");

db = SQLite.Open("AutoPlay\\Docs\\1.db");

SQLite.QueryToTable(db, "INSERT INTO collection (item1, item2, item3) VALUES ("..Enclose(tbl_item1[1])..", "..Enclose(tbl_item2[1])..", "..Enclose(tbl_item3[1])..")");
SQLite.QueryToTable(db, "INSERT INTO collection (item1, item2, item3) VALUES ("..Enclose(tbl_item1[2])..", "..Enclose(tbl_item2[2])..", "..Enclose(tbl_item3[2])..")");
SQLite.QueryToTable(db, "INSERT INTO collection (item1, item2, item3) VALUES ("..Enclose(tbl_item1[3])..", "..Enclose(tbl_item2[3])..", "..Enclose(tbl_item3[3])..")");
************************************
My question is ; is it possible to loop it somehow in order not to writte so much lines of code, some shorter way i mean. The content of each text file has to be inserted to the proper column.
I have tried this;
************************************
for i, v in tbl_item1 do
SQLite.QueryToTable(db, "INSERT INTO collection (item1) VALUES ("..Enclose(v)..")");
end
************************************
but the data is inserted in very ugly way. Where the data of one column ends there starts the data of the second column and so on.


Thank you

Worm
10-01-2004, 01:40 PM
for i=1, Table.Count(tbl_item1) do
--don't need query to table because you
--are not returning a recordset
SQLite.Query(db, "INSERT INTO collection (item1, item2, item3) VALUES ("..Enclose(tbl_item1[i])..", "..Enclose(tbl_item2[i])..", "..Enclose(tbl_item3[i])..")");
end

sside
10-01-2004, 01:59 PM
Perfect Worm

THANKS A LOT :yes

Worm
10-01-2004, 02:04 PM
Be careful though. That code is making the assumption that all the text files will be return tables that all have the same number of rows. If any one of the text files are longer or shorter, it will either error, or you will be missing some data.

sside
10-01-2004, 02:11 PM
Thanks again Worm

That's the case. The text files have the same number of rows. That was my main problem that the data in all 3 text files had to be syncron.

It works perfect in my occassion

Thanks again

Sside