Home Database Mysql Tutorial DBM数据导入到mysql数据库方法

DBM数据导入到mysql数据库方法

Jun 07, 2016 pm 05:51 PM
mysql database

本文章分享一篇关于DBM数据导入到mysql数据库方法,有需要的同学可以参考一下本实例。

首先说明一下,我以前使用PERL,保存文件 用DBM,有5万多条记录,每条记录有15个字段。现在想用MYSQL,要吧记录导过来。
第一步,将所有的DBM记录导入到了一个文本文件,每个字段之间用TAB分开(就是"t"),每条记录换行结束。
第二部,将数据读入数组,并且打开数据库和定义预处理如下:

 代码如下 复制代码


$dbline=file("g:/allrecord.txt");  //将数据库记录读入到数组
$collen=array(8,50,80,20,20,30,3,20,1,1,20,35,35,35,2); #表里面每个字段的长度数值

$db=new i('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); #预定义插入记录

//


数据库有15个字段,为了方便导入,已经设定全部类型为CHAR,长度从1到100不等,具体等同$collen的数值;
并且没有任何字段设置UNIQUE或者PRIMARY KEY;
在命令行用INSERT命令插入记录成功
//

 代码如下 复制代码
foreach($dbline as $line) {
    $rec=("t",trim($line));
    //这里反复测试过,$rec数组有15个值,并且我都把它strval了
    //    for($j=0;$j         $rec[$j]=substr($rec[$j],0,$collen[$j]);
    }
    for($i=0;$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[10],$rec[11],$rec[12],$rec[13],$rec[14]);
     $s->execute();
}
$s->close();
$db->close();
?>


经过几个小时的调试,发现任何字串超过字段长度就不能添加,所以做了如下处理。
1) 先把每个记录的每个字段的长度进行了处理,代码如下:
 

 代码如下 复制代码
   for($j=0;$j         $rec[$j]=substr($rec[$j],0,$collen[$j]);
    }

2)再处理插入

 代码如下 复制代码
$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[10],$rec[11],$rec[12],$rec[13],$rec[14]);

     $s->execute();
这样就可以插入记录了。


目前剩下的问题是,任何带有汉字的记录,都不能插入(在命令行可以)。
查了资料,在命令行alter  database crm character set 'gb2312',在脚本查询前加入$db->query("SET names 'gb2312'")
都没用。
也没找到my.ini去改default-character-set=gb2312
难道php5.5暂时不支持中文?

MYSQL太多稀奇古怪的提示,解决不了问题,所有装上了SQLITE,在命令行导入文本记录,发现了问题:

1)某些字段中存在换行符,但是在任何文本编译器中不换行或者不显示。这样会在导入的时候,1行变成2行,列参数自然不对了。
解决方案: 用str_replace($line,char(13));把每行内的换行符替换掉,然后把所有记录存入tmp.txt

2)再进行命令行的导入

 代码如下 复制代码
sqlite3>.import tmp.txt customers
sqlite3> count(*) from cusomers;

57491

显示导入成功。

可见,批量导入的时候,一定要处理好那些隐藏的特殊字符。
由于时间关系,没有测试MYSQL的情况。晚上有空再确认一下同样方案是否可行。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP development practice: Use PHPMailer to send emails to users in the MySQL database PHP development practice: Use PHPMailer to send emails to users in the MySQL database Aug 05, 2023 pm 06:21 PM

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

Go language and MySQL database: How to separate hot and cold data? Go language and MySQL database: How to separate hot and cold data? Jun 18, 2023 am 08:26 AM

As the amount of data continues to increase, database performance has become an increasingly important issue. Hot and cold data separation processing is an effective solution that can separate hot data and cold data, thereby improving system performance and efficiency. This article will introduce how to use Go language and MySQL database to separate hot and cold data. 1. What is hot and cold data separation processing? Hot and cold data separation processing is a way of classifying hot data and cold data. Hot data refers to data with high access frequency and high performance requirements. Cold data

How to use MySQL database for time series analysis? How to use MySQL database for time series analysis? Jul 12, 2023 am 08:39 AM

How to use MySQL database for time series analysis? Time series data refers to a collection of data arranged in time order, which has temporal continuity and correlation. Time series analysis is an important data analysis method that can be used to predict future trends, discover cyclical changes, detect outliers, etc. In this article, we will introduce how to use a MySQL database for time series analysis, along with code examples. Create a data table First, we need to create a data table to store time series data. Suppose we want to analyze the number

How to perform incremental data backup of MySQL database using Go language How to perform incremental data backup of MySQL database using Go language Jun 17, 2023 pm 02:28 PM

As the amount of data increases, database backup becomes more and more important. For the MySQL database, we can use the Go language to achieve automated incremental backup. This article will briefly introduce how to use Go language to perform incremental backup of MySQL database data. 1. Install the Go language environment. First, we need to install the Go language environment locally. You can go to the official website to download the corresponding installation package and install it. 2. Install the corresponding library. The Go language provides many third-party libraries for accessing MySQL databases, among which the most commonly used ones are

To what extent can I develop MySQL database skills to be successfully employed? To what extent can I develop MySQL database skills to be successfully employed? Sep 12, 2023 pm 06:42 PM

To what extent can I develop MySQL database skills to be successfully employed? With the rapid development of the information age, database management systems have become an indispensable and important component in all walks of life. As a commonly used relational database management system, MySQL has a wide range of application fields and employment opportunities. So, to what extent do MySQL database skills need to be developed to be successfully employed? First of all, mastering the basic principles and basic knowledge of MySQL is the most basic requirement. MySQL is an open source relational database management

How to use MySQL database for image processing? How to use MySQL database for image processing? Jul 14, 2023 pm 12:21 PM

How to use MySQL database for image processing? MySQL is a powerful relational database management system. In addition to storing and managing data, it can also be used for image processing. This article will introduce how to use a MySQL database for image processing and provide some code examples. Before you begin, make sure you have installed a MySQL database and are familiar with basic SQL statements. Create a database table First, create a new database table to store the image data. The structure of the table can be as follows

How to make reliable MySQL database connection using Go language? How to make reliable MySQL database connection using Go language? Jun 17, 2023 pm 07:18 PM

With the large amount of data that needs to be stored and processed, MySQL has become one of the most commonly used relational databases in application development. The Go language is becoming more and more popular among developers due to its efficient concurrency processing and concise syntax. This article will lead readers to implement reliable MySQL database connection through Go language, allowing developers to query and store data more efficiently. 1. Several ways for Go language to connect to MySQL database. There are usually three ways to connect to MySQL database in Go language, which are: 1. Third-party library

MySQL database and Go language: How to perform data caching? MySQL database and Go language: How to perform data caching? Jun 17, 2023 am 10:05 AM

In recent years, the Go language has become increasingly popular among developers and has become one of the preferred languages ​​for developing high-performance web applications. MySQL is also a popular database that is widely used. In the process of combining these two technologies, caching is a very important part. The following will introduce how to use Go language to handle the cache of MySQL database. The concept of caching In web applications, caching is a middle layer created to speed up data access. It is mainly used to store frequently requested data to

See all articles