Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Location
    The Netherlands
    Posts
    475

    Insert Table data in SQLite database

    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
    Last edited by sside; 10-01-2004 at 12:33 PM.

  2. #2
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Code:
    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

  3. #3
    Join Date
    Dec 2003
    Location
    The Netherlands
    Posts
    475
    Perfect Worm

    THANKS A LOT

  4. #4
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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.

  5. #5
    Join Date
    Dec 2003
    Location
    The Netherlands
    Posts
    475
    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

Similar Threads

  1. SQLite Database Browser - New Version!
    By Intrigued in forum AutoPlay Media Studio 5.0
    Replies: 9
    Last Post: 11-10-2009, 06:14 AM
  2. SQLite data to HTML file
    By Intrigued in forum AutoPlay Media Studio 5.0
    Replies: 16
    Last Post: 10-23-2004, 03:19 AM
  3. sqlite database question
    By dulux1309 in forum AutoPlay Media Studio 5.0
    Replies: 5
    Last Post: 04-08-2004, 12:33 PM
  4. Email Input Text ??
    By octane6228 in forum AutoPlay Media Studio 5.0
    Replies: 25
    Last Post: 02-21-2004, 03:59 PM
  5. insert a lot of data in a table
    By coffeeworm in forum AutoPlay Media Studio 5.0
    Replies: 12
    Last Post: 12-05-2003, 12:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts