Announcement

Collapse
No announcement yet.

Importing a CSV file into SQL DB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Importing a CSV file into SQL DB

    I have a login system with an SQLite database and want to import a CSV file content with user numbers or password that are much, pls how do i archive this.

    Thanks in advance

    Kaase

  • #2
    Importing a CSV and populating a database from it is relatively easy if you follow these simple steps:
    1. Import the text file with TextFile.ReadToTable(). Each line will create one element in the table.
    2. Once you have the whole file imported, use DelimitedStringToTable() to split each line into its separate fields (user id, name, password, etc.).
    3. Now, for each line divided into the fields, use the proper SQL command to insert the values into the database.
    4. Loop to step 2 until there are no more lines to be processed.


    If you have so many lines that TextFile.ReadToTable() gives you memory issues, use pure Lua instead, such as io.lines(), like in this sample:

    Code:
    dofile "AutoPlay\\Scripts\\DelimitedStringFunctions.lua";
    Debug.ShowWindow();
    
    local nCount = 1;
    for sLine in io.lines(".\\AutoPlay\\Docs\\sample.csv") do
    	local tFields = DelimitedStringToTable(sLine, ",");
    	if (tFields ~= nil) then
    		-- here you would execute the INSERT in your database instead
    		-- but this code will just display the data as it is processed
    		Debug.Print("Line " .. nCount .. ":\r\n");
    		for i = 1, #tFields do
    			Debug.Print("Field " .. i .. ": " .. tFields[i] .. "\r\n");
    		end
    		Debug.Print("--------------------\r\n");
    		nCount = nCount + 1;
    	end
    end
    Ulrich

    Comment


    • #3
      Thanks Ulrich, i will prefer u to set me an example with the steps you indicated above 1 - 4, that will be easier for me to adopt im not conversant with pure lua at all.

      Kaase

      Comment

      Working...
      X