Importing a Text (CSV) file into a database

Importing a Text (CSV) file into a database

James Phillips
Vastuste arv 16
I have been trying to import a text file containing records into a database on 1.6 but have not had any success at all. What is the exact format the file will have to conform to to actually go into a database? It says CSV on the upload button, but I tried:

"first field","Second field"
"first field","Second field"

i.e. the fields seperated by a comma and defined by " with the records being seperated by a line break but it didn't work. I also tried without ", but that didn't work either. Does anybody have any ideas?

Best regards,

James Phillips

Keskmine hinnang: -
Vastuses James Phillips

Re: Importing a Text (CSV) file into a database

Don Hinkelman
Particularly helpful Moodlers pilt Plugin developers pilt
I am having the same problem!   Upload a csv file and get an error message, "check format".
Vastuses Don Hinkelman

Re: Importing a Text (CSV) file into a database

James Phillips
I am going to hopefully try to do this through phpmyadmin over the weekend. I also thought that the default field seperator in mysql for csv files was ";" (in spite of the name!) but I am probably wrong about this. 
Vastuses James Phillips

Re: Importing a Text (CSV) file into a database

James Phillips
Still having no joy with this. I can bodge a solution via phpmyadmin but that isn't really what I was after. 
Vastuses James Phillips

Re: Importing a Text (CSV) file into a database

Claudio Gammino

Hi all, I've got the same problems with csv import. Here is the error message:

Column 'dataid' cannot be null INSERT INTO mdl_data_records ( USERID, GROUPID, DATAID, TIMECREATED, TIMEMODIFIED, APPROVED ) VALUES ( 2, 0, null, 1152510889, 1152510889, 1 )

There are 3 textfields in the database and the content of the .csv-file is:

Topic,body,data
1,2,3

Any ideas? best regards

Vastuses Claudio Gammino

Re: Importing a Text (CSV) file into a database

rich holman

I've spent some time debugging this and here is what I've found.

file format I used

"fieldname1","fieldname2","fieldname3"
"data1","data2","data3"
"data4","data5","data6"

I then set the delimer to , and enclosure to " on the upload page.

Having then debugged the import.php script here's what happened

the records were read into local variables OK but

when it calls

if ($recordid = data_add_record($data->id, 0)) 

this does not return a valid $recordid and aborts adding the records.

So the problem lies on the data_add_record method that's as far as I've got.

Hope this helps the developer find the bug (I'm using moodle 1.6 latest vers)

Rich

p.s. Also the link for the help button on the upload page has a typo is is currently

help.php?module=data&file=importcvs.html

but it should be

help.php?module=data&file=importcsv.html

Vastuses rich holman

Re: Importing a Text (CSV) file into a database

rich holman

Ok found 2 errors with import php

line 123 has been corrected to if ($recordid = data_add_record($data, 0)) {

line 143   has been corrected to $currentfield->update_content($recordid, $value, $name);

attched is a corrected file which now allows me to upload csv file with a comma as the delimeter and each record seprated by a new line.  like this

fieldname1, fieldname2, fieldname3
data1,data2,data3
data4,data5,data6

Hope this helps

Rich

p.s. the file needs to go into /mod/data directory

 

 

 

 

 

 

Vastuses rich holman

Re: Importing a Text (CSV) file into a database

Just Dave
Hi Rich,

Thanks for fixing this issue, however the fix does not seem to work for me sad

I am running Moodle 1.5.3 with the database module. I replaced my import.php file with the version you attached previously. I am trying to import a csv file which looks like:

field1,field2,field3

but I get the following error:

Warning: require_once(C:\moodle\moodle/mod/data/field//field.class.php) [function.require-once]: failed to open stream: No such file or directory in C:\moodle\moodle\mod\data\import.php on line 139

Fatal error: require_once() [function.require]: Failed opening required 'C:\moodle\moodle/mod/data/field//field.class.php' (include_path='.;C:\moodle\php\pear\') in C:\moodle\moodle\mod\data\import.php on line 139

I've compared my origional import.php with your import.php and line 139 of your import.php(                        require_once($CFG->dirroot.'/mod/data/field/'.$field->type.'/field.class.php');) is exactly the same. So I'm not sure why it can't find the field.class.php files anymore.

I am running 1.5.3 on Windows XP with Apache 2. I tried noticed in the error message the path read C:\moodle\moodle/mod/data/field//field.class.php which seems wrong. I tried removing a '/' and also replacing the forward slashes with backslashes but the error message stayed the same.

I hope this makes sense. Sorry but my php is not great, still learning.

Thanks for taking the time to read this naeratus

Cheers,
Dave
Vastuses Just Dave

Re: Importing a Text (CSV) file into a database

rich holman

Hi Dave

When this happened to me it was because of a single error in the csv file and it was hard to find, (an extra comma on one line I think). The error forced the script to look up a field type that did not exist and consequently failed.

Thoroughly check you source file and if that doesn't do it, email it to me and I'll take a look. You'll have to let me know the field types you used so I can recreate.

Rich

Vastuses Just Dave

Re: Importing a Text (CSV) file into a database

rich holman

Dave

I've looked at this again and I still think its the csv file because in your error message you have (notice the double slah after field)

C:\moodle\moodle/mod/data/field//field.class.php

doesn't look right, if a field is a textarea it would like this

C:\moodle\moodle/mod/data/field/textarea/field.class.php

or for a url

C:\moodle\moodle/mod/data/field/url/field.class.php

etc etc

the php script fills in this bit of info when it is run and is dependent on the contents of the csv file being absolutely perfect.

If you still believe the csv file is right then email it to me and I'll try and recreate.

I'm surprised the script doesn't trap this error better, maybe the author could modify it so it reports the line number in the csv file where this occurs.  

Rich 

 

 

Vastuses rich holman

Re: Importing a Text (CSV) file into a database

Just Dave
Hi Rich,

Thanks for the speedy reply. You were absolutely right, there was an error in my CSV file! I can't believe I misread the instructions earlier. I did not include the field names on the first line of my csv file. After I added them, she imported perfectly.

Thanks so much for pointing this out, however I have run into another small problem kurvastab When I delete entries, I am recieving this error:

Warning: unlink(C:\moodle\moodledata/1/moddata/data/2/6/33) [function.unlink]: No such file or directory in C:\moodle\moodle\lib\filelib.php on line 355

Warning: unlink(C:\moodle\moodledata/1/moddata/data/2/5/33) [function.unlink]: No such file or directory in C:\moodle\moodle\lib\filelib.php on line 355

Warning: unlink(C:\moodle\moodledata/1/moddata/data/2/4/33) [function.unlink]: No such file or directory in C:\moodle\moodle\lib\filelib.php on line 355

Record deleted

When I look at that path, it doesn't seem correct. I have checked my moodle config.php and the pathnames and slashes all seem OK. This is all on a Windows XP box with Apache 2 by the way. I then installed the same database module onto a test portal I have set up on Apache on a linux server. I set up another test database but again when I try to delete an entry I recieve the same error(even though the path looks correct):

Warning: unlink(/var/www/moodledata/3/moddata/data/2/50/5) [function.unlink]: No such file or directory in /var/www/moodle/lib/filelib.php on line 355

Warning: unlink(/var/www/moodledata/3/moddata/data/2/49/5) [function.unlink]: No such file or directory in /var/www/moodle/lib/filelib.php on line 355

Warning: unlink(/var/www/moodledata/3/moddata/data/2/47/5) [function.unlink]: No such file or directory in /var/www/moodle/lib/filelib.php on line 355

The records are being deleted, this error message just bothers me a little. Am I missing something obvious again?

Again, thanks for your time naeratus

Cheers,
Dave

P.S. I just recieved your last attached import.php, thanks. I tried this but I still recieve these messages apon deleting. The import still works though.
Vastuses Just Dave

Re: Importing a Text (CSV) file into a database

rich holman

Dave

I've not played with the delete.php script before, a quick scan of the code would suggest that for this to happen the file has previously been deleted.

Has the file been deleted?

it might be worth putting something like

echo $fullfile;  in the script before the call to unlink to see what path you get. If you get a path then check the file exists.

I tried to recreate but mine seems to work fine (I'm on 1.6)

Rich

Vastuses rich holman

Re: Importing a Text (CSV) file into a database

Just Dave
Thanks Rich. I don't think I've deleted the file previously.

I tried to locate delete.php in the /mod/data directory file but failed. There are delete.php files in the my admin and course directory but I don't think these are the right ones.

Which delete.php file do you mean?

Cheers,
Dave

Vastuses rich holman

Re: Importing a Text (CSV) file into a database

Claudio Gammino

Hi,

importing csv file to database on moodle 1.6 is still working. But importing text with german umlaut or characters like 'öäüß' don't work. Must the csv file have a specific character set like 'UTF-8' or 'Unicode'? I've tried many combinations, without result.

Thank's for ideas in advance.

Claudio

Vastuses James Phillips

Re: Importing a Text (CSV) file into a database

Pieter van der Hijden

As admin I tried to upload a CSV file. I used Excel to build the file and checked that the first line contained a "record" with the field names as used by the Moodle database. The field types used are text, number, radio, text field, url. I indicated the ; was my field delimiter (the default in Excel CSV export).

Nevertheless, I got the following error message:

Warning: fopen(/tmp/phpLEWc5Q): failed to open stream: Permission denied in /home/www/user/isaga/public_html/mod/data/import.php on line 105

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/user/isaga/public_html/mod/data/import.php on line 106

Warning: fclose(): supplied argument is not a valid stream resource in /home/www/user/isaga/public_html/mod/data/import.php on line 107

No entry was saved. Please check the format of the uploaded file.