Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:50:02+00:00 2026-05-22T00:50:02+00:00

I have about 14000 rows of comma separated values that I am trying to

  • 0

I have about 14000 rows of comma separated values that I am trying to insert into a sqlite table using PHP PDO, like so:

<?php
// create a PDO object
$dbh = new PDO('sqlite:mydb.sdb');

$lines = file('/csv/file.txt'); // import lines as array
foreach ($lines as $line) {
    $line_array = (','$line); // create an array of comma-separated values in each line
    $values = '';
    foreach ($line_array as $l) {
        $values .= "'$l', ";
    }
    substr($values,-2,0); // get rid of the last comma and whitespace
    $query = "insert into sqlite_table values ($values)"; // plug the value into a query statement
    $dbh->query($query); // run the query
}

?>

This query takes a long time, and to run it without interuption, I would have to use PHP-CLI.
Is there a better (faster) way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:50:03+00:00Added an answer on May 22, 2026 at 12:50 am

    You will see a good performance gain by wrapping your inserts in a single transaction. If you don’t do this SQLite treats each insert as its own transaction.

    <?php
    // create a PDO object
    $dbh = new PDO('sqlite:mydb.sdb');
    
    // Start transaction
    $dbh->beginTransaction();
    $lines = file('/csv/file.txt'); // import lines as array
    foreach ($lines as $line) {
        $line_array = (','$line); // create an array of comma-separated values in each line
        $values = '';
        foreach ($line_array as $l) {
            $values .= "'$l', ";
        }
        substr($values,-2,0); // get rid of the last comma and whitespace
        $query = "insert into sqlite_table values ($values)"; // plug the value into a query statement
        $dbh->query($query); // run the query
    }
    // commit transaction
    $dbh->commit();
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During onCreate() I'm trying to insert about 20 rows into the SQLite database (these
I have a Concept table of about 1000 rows that I would like to
The Problem I have a table that's about 2 million rows (at 115 MB)
I have a DataGridView in a Winforms app that has about 1000 rows (unbound)
I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
I have a ListView that reads data from sqlite with SimpleCursorAdapter and the table
I have question about database optimizing, indexing. I have table that called projects and
I have a website that lets people view rows in a table (each row
I have a titles table of about 14000 records, with a float field average_rating
I have about 14000 rows added through a script with ID ( auto_increment )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.