The method of importing DBM data into MySQL database

Source: Internet
Author: User
Tags character set

First of all, I used Perl to save files in the DBM database with more than 50,000 records and 15 fields per record. Now want to use MySQL, to the record guide over.
The first step, all the DBM records are imported into a text file, each field is separated by a tab ("T"), and the end of each line wraps.
The second section reads the data into an array and opens the database and defines preprocessing as follows:

  code is as follows copy code

<?php
$dbline =file ("G:/allrecord.txt"); //Read database records to array
$collen =array ( 8,50,80,20,20,30,3,20,1,1,20,35,35,35,2); #表里面每个字段的长度数值

$db =new mysqli (' localhost ', ' Me ', ' mypass ', ' allrecdb ');
if (Mysqli_connect_errno ()) {
    echo "Can not connect db!";
    exit;
}
$q->query ("SET names ' gb2312 '");

$q = INSERT into Customers VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);

$s = $db->prepare ($q); #预定义插入记录

//


Database has 15 fields, in order to facilitate the import, has been set to all types of char, length from 1 to 100, the specific equivalent of $collen value;
And there are no fields to set the unique or primary KEY;
Inserting a record with the insert command at the command line succeeded
//

The code is as follows Copy Code
foreach ($dbline as $line) {
$rec =explode ("T", trim ($line));
It's been tested over and over again, $REC array has 15 values, and I strval it.
for ($j =0; $j <count ($rec); $j + +) {
$rec [$j]=substr ($rec [$j],0, $collen [$j]);
}
for ($i =0; $i <count ($rec); $i + +) {
$rec [$i]=strval ($rec [$i]);
}
$s->bind_param ("sssssssssssssss", $rec [0], $rec [1], $rec [2], $rec [3], $rec [4], $rec [5], $rec [6], $rec [7], $rec [8],$ REC[9], $rec [ten], $rec [one], $rec [[a], $rec [], $rec [14]);
$s->execute ();
}
$s->close ();
$db->close ();
?>


After several hours of debugging, found that any string exceeds the length of the field can not be added, so do the following processing.
1 The length of each field of each record is processed first, the code is as follows:

The code is as follows Copy Code
for ($j =0; $j <count ($rec); $j + +) {
$rec [$j]=substr ($rec [$j],0, $collen [$j]);
}

2) re-processing insert

The code is as follows Copy Code
$s->bind_param ("sssssssssssssss", $rec [0], $rec [1], $rec [2], $rec [3], $rec [4], $rec [5], $rec [6], $rec [7], $rec [8],$ REC[9], $rec [ten], $rec [one], $rec [[a], $rec [], $rec [14]);

$s->execute ();
This allows you to insert a record.


The remaining problem now is that any record with Chinese characters cannot be inserted (at the command line).
After checking the data, on the command line ALTER DATABASE CRM character set ' gb2312 ', add the $db->query before the script query ("Set names ' gb2312 '")
is useless.
I didn't find My.ini to change default-character-set=gb2312.
Does php5.5 not support Chinese at the moment?

MySQL Too many strange hints, can not solve the problem, all loaded on the SQLite, the command line to import text records, found the problem:

1 There are line breaks in some fields, but they are not wrapped or displayed in any text compiler. This will be imported, 1 rows into 2 rows, the column parameters are naturally wrong.
Solution: Use Str_replace ($line, char (13)), replace line breaks in each line, and then deposit all records in Tmp.txt

2) To import the command line again

The code is as follows Copy Code
Sqlite3>.import Tmp.txt Customers
Sqlite3>select Count (*) from cusomers;

57491

Show import success.

Visible, batch import, must deal with those hidden special characters.
Because of the time relationship, there is no test of MySQL. Have a free night to confirm the feasibility of the same plan.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.