1

Im looking for efficient method for inserting CSV data into SQLite database on remote server.

I have to use PHP. Im using SQLite3, so i have to use PDO (sqlite_query will not work).

CSV is on server side and it has 100000+ rows (50MB+ filesize).

Question: is there any method for PHP faster than this?

for (/* each row, 100.000 rows */)
{
    $a = 'something';
    $b = 'something';
    $query = $link->prepare("INSERT INTO user_info (a, b) VALUES (?, ?)");
    $query->bindParam(1, $a);
    $query->bindParam(2, $b);
    $query->execute();
}

My SQL file is in the same directory.

I've read about .import command, but i don't know how to use it with PDO (shall i use prepare? how file path should look like?).

Im new to PDO and SQLite.

4
  • Read it, then write it. Whats the problem? Commented Jan 9, 2013 at 14:01
  • possible duplicate of Bulk load data into sqlite? Commented Jan 9, 2013 at 14:24
  • @KingCrunch its not duplicate .. what if you need to filter invalid values ??? The link you gave can only work for trusted values Commented Jan 12, 2013 at 20:59
  • @Baba That would be something different, but the question doesn't contain a "filter", it contains oly a "inserting a CSV into SQLite database". Also the OP mentioned mentioned .import himself. If you are right, the OP should clarify his question, else it's a duplicate :) Commented Jan 12, 2013 at 23:08

2 Answers 2

2
$link->beginTransaction();

$query = $link->prepare("INSERT INTO user_info (a, b) VALUES (?, ?)");
for (/* each row, 100.000 rows */)
{
//variable stuff + execute query
}

$link->commit();

This holds the writes until after the loop - much faster. You'll need to create db object like this: $link = new PDO('sqlite:your.db');

edit: Typo/paste correction

Sign up to request clarification or add additional context in comments.

1 Comment

Also, it's worth noting that this may not produce the same speed-up on other DB systems - in fact, quite the opposite, since the DBMS will have to maintain a higher isolation level, and either acquire a lock or maintain a temporary version of the data.
0
+50

Well, I don't know if there is a bulk approach with SQLite that can be used via a PDO driver, but you are re-preparing the same statement on every loop.

This would probably be a touch more efficient:

$query = $link->prepare("INSERT INTO user_info (a, b) VALUES (?, ?)");
for (/* each row, 100.000 rows */)
{
    $a = 'something';
    $b = 'something';

    $query->bindParam(1, $a);
    $query->bindParam(2, $b);
    $query->execute();
}

2 Comments

Right. And what about arrays? I have array like this: $data[$row][$cell]. Can I bind array element somehow? I mean dynamic row index. I had problems with binding array elements in C# and MSSQL, and i didn't used array here.
I'm not sure what you mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.