1

My code below is supposed to automate insertion of a CSV into a database. Howvever, if the input file contains more than 10 lines, the database supposedly corrupts with the message, "database disk image is malformated".

$db->exec('BEGIN;');
while(!feof($data)) {
    $line = fgets($data);
    $tmp = split(",", $line);

    $query = "INSERT INTO calendar_dates VALUES (
            '$tmp[0]','$tmp[1]',$tmp[2])";

    $db->exec($query);
    if (!$results) {
        exit ("Error<br>");
    }

}

$db->exec('COMMIT;');
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
vital
  • 1,308
  • 3
  • 13
  • 28
  • Maybe up overload the database system? – Cole Tobin Jul 13 '13 at 20:00
  • I don't think so. In my EDIT, I detailed that with only 10 queries, the problem occured and I believe sqlite can handle much more... – vital Jul 13 '13 at 20:07
  • Maybe the query sting is too long? I really can't think of anything else. Can we see a file that would crash it? – Cole Tobin Jul 13 '13 at 21:21
  • The csv file is a simple calendar_dates.txt file in GTFS format (about 13000 rows of data). Besides, I imported this very same file into an sqlite database via command line and there is no problem at all, but I would like to automate this process in PHP so that I don't have to do it manually... – vital Jul 13 '13 at 22:25
  • 1
    Hmmm. That's really weird. I'd file a bug report. Pn the side, how would using PHP help you automate it? – Cole Tobin Jul 13 '13 at 22:53
  • I mean via a cron job launched on my web server... – vital Jul 13 '13 at 23:23
  • That makes sense. However, considering the bizarity of this situation, can't you automate it with a bash script? – Cole Tobin Jul 13 '13 at 23:28
  • Well, yes... But I have no idea how it works, could you give some links/tutorials about it? – vital Jul 13 '13 at 23:42
  • Sadly, I don't have much knowledge of bash. However, [this question](http://stackoverflow.com/questions/918886) might help. – Cole Tobin Jul 14 '13 at 00:38
  • What is your memory usage? Is it possible that your PHP code gets killed after exceeding memory limit? This could explain why db gets corrupted – mvp Jul 14 '13 at 05:47
  • 1
    I run a memory_get_usage at the beginning and at the end of my script. Here are the results: 639472 & 3212104. But, I remember that in the past, I already had memory trouble with another script and PHP mentioned it with some kind of "Out of memory" error. So I don't think that's the case here. – vital Jul 14 '13 at 14:18

0 Answers0