Moodle mobile problem: "Error writing to database" when login?

Moodle mobile problem: "Error writing to database" when login?

by Lovrty Archer -
Number of replies: 6

hi  i would like to enable moodle mobile service.

1. enable and setting it on web page

2. install Android app

3. input site address, username and password.  but when click "LOG IN" on my cellphone, it shows "Error writing to database"

may i know where goes wrong with it? Thanks



Average of ratings: -
In reply to Lovrty Archer

Re: Moodle mobile problem: "Error writing to database" when login?

by Ken Task -
Picture of Particularly helpful Moodlers

You'll have to check error logs of the server hosting the moodle and share back what you find there.   Otherwise, it's anyone's guess cause 'error writing to database' could indicate many problems.

Suggest turning on debugging in Moodle to see if you can get more information.

'SoS', Ken

In reply to Ken Task

回复: Re: Moodle mobile problem: "Error writing to database" when login?

by Lovrty Archer -
Thanks for your kind reply.
1. i find my website do not have the token of moodle mobile service.
2. enable debug and display it.
3. debug info is here


Debug info: Data too long for column 'privatetoken' at row 1
INSERT INTO mdl_external_tokens (token,externalserviceid,tokentype,userid,contextid,creatorid,timecreated,validuntil,privatetoken) VALUES(?,?,?,?,?,?,?,?,?)
[array (
0 => 'c2a227a705b27359be6817a1f94c8dd4',
1 => '1',
2 => 0,
3 => '2',
4 => 1,
5 => '2',
6 => 1593576738,
7 => 0,
8 => 'EG07vXC02AIKfsIJd0PskfF7ql0vygjRPGuzxPVNPA3C33GuGvJYnZtcNk3j4yoILYtc',
)]
Error code: dmlwriteexception

Stack trace:
line 489 of /lib/dml/moodle_database.php: dml_write_exception thrown
line 1331 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database->query_end()
line 1377 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->insert_record_raw()
line 778 of /lib/externallib.php: call to mysqli_native_moodle_database->insert_record()
line 86 of /admin/webservice/tokens.php: call to external_generate_token()




In reply to Lovrty Archer

Re: 回复: Re: Moodle mobile problem: "Error writing to database" when login?

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

The problem is that the site is trying to insert a 68-character long string:

 8 => 'EG07vXC02AIKfsIJd0PskfF7ql0vygjRPGuzxPVNPA3C33GuGvJYnZtcNk3j4yoILYtc',

into a 64-character wide column:

| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| privatetoken      | varchar(64)  | YES  |     | NULL    |                |

Before Moodle 3.8 privatetoken was an MD5 hash which is 32 characters long. From Moodle 3.8 onwards it generates a 64-character long string in lib/externallib.php:external_generate_token():

    $newtoken->privatetoken = random_string(64);
    $DB->insert_record('external_tokens', $newtoken);

Which version of Moodle is this? Can you check the above line hasn't been changed?

In reply to Leon Stringer

回复: Re: 回复: Re: Moodle mobile problem: "Error writing to database" when login?

by Lovrty Archer -

hi, many thanks for your help.

1. checked my moodle site version, it's 3.82+ (Build: 20200312).

2. table column in mysql as follows, may i know what should i do? thanks.


In reply to Lovrty Archer

Re: 回复: Re: 回复: Re: Moodle mobile problem: "Error writing to database" when login?

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Please also check the the source code file lib/externallib.php so see if the token length in the function external_generate_token() has been changed. It's probably line 777:

 776     // Generate the private token, it must be transmitted only via https.
 777     $newtoken->privatetoken = random_string(64);
 778     $DB->insert_record('external_tokens', $newtoken);

What does it say in your file, is it 64?

In reply to Leon Stringer

回复: Re: 回复: Re: 回复: Re: Moodle mobile problem: "Error writing to database" when login?

by Lovrty Archer -
thank you very much, checked it in lib/externallib.php, it's okay.
but remind that i have modified similar function in moodlelib.php
then get back into default, and problem solved.
Thank you, Leon Stringer.