先聲明我也是一名小白(或者說是“PHP愛好者”)//以下內(nèi)容僅供參考
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴(kuò)展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,創(chuàng)新互聯(lián)面向各種領(lǐng)域:建筑動畫等網(wǎng)站設(shè)計、成都全網(wǎng)營銷解決方案、網(wǎng)站設(shè)計等建站排名服務(wù)。
經(jīng)過多次分析可以用這幾種方案:
一、把復(fù)選內(nèi)容寫入配置文件(參考dedecms的數(shù)據(jù)庫)
二、把復(fù)選內(nèi)容寫入數(shù)據(jù)庫,使用時只需要在加載系統(tǒng)時把配置內(nèi)容加載然后就可以在任意位置使用。
1、首先介紹一下將String類型轉(zhuǎn)為Date類型的方法。需要導(dǎo)入java.text.SimpleDateFormat類。下面舉一個例子,比如有一個字符串 “2018-08-24“,想要轉(zhuǎn)為Date類型,代碼如圖所示。
2、下面我們驗(yàn)證一下。是否轉(zhuǎn)化成功,打印一下轉(zhuǎn)化后的時間類型的毫秒數(shù),如果可以打印出結(jié)果,說明轉(zhuǎn)化成功,代碼如圖所示。
3、通過上面的代碼可以看出,String類型已經(jīng)成功轉(zhuǎn)換為Date類型了,注意一點(diǎn),使用SimpleDateFormat需要捕獲ParseException異常。如圖所示。
4、以將當(dāng)前時間轉(zhuǎn)化為字符串為例,獲取當(dāng)前時間可以使用Date nowDate = new Date(),如圖所示。
5、時間類型轉(zhuǎn)化為String類型,可以使用SimpleDateFormat的format方法,非常的簡單,下面我們看一下代碼并打印一下轉(zhuǎn)化后的時間字符串,如圖所示。
數(shù)組吧,直接把數(shù)組轉(zhuǎn)字符串啊
implode() 函數(shù)返回由數(shù)組元素組合成的字符串。(適合一維數(shù)組)
$arr = array('Hello', 'World', 'I', 'love', 'Shanghai');
1 echo implode(" ",$arr);//加空格
the result : Hello World I love Shanghai
2 echo implode(",",$arr);//加逗號
the result : Hello,World,I,love,Shanghai
轉(zhuǎn)換數(shù)組為字符串后插入數(shù)據(jù)庫就可以了。
serial_number.txt的示例內(nèi)容:
serial_number.txt:
DM00001A11 0116,
SN00002A11 0116,
AB00003A11 0116,
PV00004A11 0116,
OC00005A11 0116,
IX00006A11 0116,
創(chuàng)建數(shù)據(jù)表:
create table serial_number(
id int primary key auto_increment not null,
serial_number varchar(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
php代碼如下:
$conn = mysql_connect('127.0.0.1','root','') or die("Invalid query: " . mysql_error());
mysql_select_db('test', $conn) or die("Invalid query: " . mysql_error());
$content = file_get_contents("serial_number.txt");
$contents= explode(",",$content);//explode()函數(shù)以","為標(biāo)識符進(jìn)行拆分
foreach ($contents as $k = $v)//遍歷循環(huán)
{
$id = $k;
$serial_number = $v;
mysql_query("insert into serial_number (`id`,`serial_number`)
VALUES('$id','$serial_number')");
}
備注:方法有很多種,我這里是在拆分txt文件為數(shù)組后,然后遍歷循環(huán)得到的數(shù)組,每循環(huán)一次,往數(shù)據(jù)庫中插入一次。
再給大家分享一個支持大文件導(dǎo)入的
?php
/**
* $splitChar 字段分隔符
* $file 數(shù)據(jù)文件文件名
* $table 數(shù)據(jù)庫表名
* $conn 數(shù)據(jù)庫連接
* $fields 數(shù)據(jù)對應(yīng)的列名
* $insertType 插入操作類型,包括INSERT,REPLACE
*/
function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType='INSERT'){
if(empty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('";
else $head = "{$insertType} INTO `{$table}`(`".implode('`,`',$fields)."`) VALUES('"; //數(shù)據(jù)頭
$end = "')";
$sqldata = trim(file_get_contents($file));
if(preg_replace('/\s*/i','',$splitChar) == '') {
$splitChar = '/(\w+)(\s+)/i';
$replace = "$1','";
$specialFunc = 'preg_replace';
}else {
$splitChar = $splitChar;
$replace = "','";
$specialFunc = 'str_replace';
}
//處理數(shù)據(jù)體,二者順序不可換,否則空格或Tab分隔符時出錯
$sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替換換行
$sqldata = $specialFunc($splitChar,$replace,$sqldata); //替換分隔符
$query = $head.$sqldata.$end; //數(shù)據(jù)拼接
if(mysql_query($query,$conn)) return array(true);
else {
return array(false,mysql_error($conn),mysql_errno($conn));
}
}
//調(diào)用示例1
require 'db.php';
$splitChar = '|'; //豎線
$file = 'sqldata1.txt';
$fields = array('id','parentid','name');
$table = 'cengji';
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
/*sqlda ta1.txt
1|0|A
2|1|B
3|1|C
4|2|D
-- cengji
CREATE TABLE `cengji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
*/
//調(diào)用示例2
require 'db.php';
$splitChar = ' '; //空格
$file = 'sqldata2.txt';
$fields = array('id','make','model','year');
$table = 'cars';
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
/* sqldata2.txt
11 Aston DB19 2009
12 Aston DB29 2009
13 Aston DB39 2009
-- cars
CREATE TABLE `cars` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`make` varchar(16) NOT NULL,
`model` varchar(16) DEFAULT NULL,
`year` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
*/
//調(diào)用示例3
require 'db.php';
$splitChar = ' '; //Tab
$file = 'sqldata3.txt';
$fields = array('id','make','model','year');
$table = 'cars';
$insertType = 'REPLACE';
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
/* sqldata3.txt
11 Aston DB19 2009
12 Aston DB29 2009
13 Aston DB39 2009
*/
//調(diào)用示例3
require 'db.php';
$splitChar = ' '; //Tab
$file = 'sqldata3.txt';
$fields = array('id','value');
$table = 'notExist'; //不存在表
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
//附:db.php
/* //注釋這一行可全部釋放
?
?php
static $connect = null;
static $table = 'jilian';
if(!isset($connect)) {
$connect = mysql_connect("localhost","root","");
if(!$connect) {
$connect = mysql_connect("localhost","Zjmainstay","");
}
if(!$connect) {
die('Can not connect to database.Fatal error handle by /test/db.php');
}
mysql_select_db("test",$connect);
mysql_query("SET NAMES utf8",$connect);
$conn = $connect;
$db = $connect;
}
?
//*/
.
-- 數(shù)據(jù)表結(jié)構(gòu):
-- 100000_insert,1000000_insert
CREATE TABLE `100000_insert` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
100000 (10萬)行插入:Insert 100000_line_data use 2.5534288883209 seconds
1000000(100萬)行插入:Insert 1000000_line_data use 19.677318811417 seconds
//可能報錯:MySQL server has gone away
//解決:修改my.ini/my點(diǎn)吸煙 f max_allowed_packet=20M