emm... 差不多就是先拿到navicat中數(shù)據(jù)庫表的CREATE語句培慌,然后生成對應(yīng)的INSERT語句覆劈。用于日常開發(fā)中生成一些簡單的數(shù)據(jù)使用追城。幾萬條數(shù)據(jù)一般還是沒問題的。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>根據(jù)navicat的create語句隨機(jī)生成insert語句</title>
<script type="text/javascript">
function generateInsertClums() {
// 獲取文本內(nèi)容
var createStr=document.getElementById('getCreateStr').value;
var colum = document.getElementById('insertNum').value;
// 分析處理create語句
var tableName = createStr.substring(createStr.indexOf('`')+1, createStr.indexOf('`', createStr.indexOf('`')+1));
// 獲取需要分析的字符范圍
var startIndex = createStr.indexOf('(');
var lastIndex = createStr.lastIndexOf(')');
// 遍歷獲取名字/類型鍵值對
var resultMapper = {};
var tagIsLeft = true;
var leftPos = -1;
var rightPos = -1;
var clomName = 'fuck';
for(var i = startIndex; i < lastIndex; i++) {
// 獲取key
if (createStr[i]=='`') {
if (tagIsLeft) {
if (createStr[i-1] != ' ' || createStr[i-2] !=' ') {
continue;
}
leftPos = i;
tagIsLeft = false;
} else {
rightPos = i;
tagIsLeft = true;
}
if (tagIsLeft) {
clomName = createStr.substring(leftPos+1, rightPos);
// 獲取type,此時leftPos為左開始點(diǎn)厘熟,rogitPos為右結(jié)束點(diǎn),然后思路為獲取結(jié)束點(diǎn)后第一個空格和第二個空格之間的內(nèi)容
var spaceLeft = rightPos+1;
var spaceRight = createStr.indexOf(' ', spaceLeft+1);
// 將數(shù)據(jù)加入mapper
resultMapper[clomName] = createStr.substring(spaceLeft+1, spaceRight);
}
}
}
// return resultMapper;
// for(var i = 0; i < colum; i ++) {
// document.getElementById('insertResult').value += getInsertCloms(resultMapper, tableName)+"\r\n";
// }
document.getElementById('insertResult').value += getInsertCloms(resultMapper, tableName, colum)+"\r\n";
}
function getInsertCloms(resultMapper,tableName, insertNum) {
var insertStr = "INSERT INTO " + tableName;
var paraStr = getInsertParaStr(resultMapper);
// var valStr = getInsertValStr(resultMapper);
var valStr = "";
// console.log(valStr);
for(var i = 0; i < insertNum; i++) {
valStr+=getInsertValStr(resultMapper);
valStr+="\r\n,";
}
valStr=valStr.substring(0,valStr.length-1);
insertStr += " "+paraStr+" VALUES "+valStr+";";
return insertStr;
}
// 獲取insert語句的字段值(除去主鍵id)
function getInsertParaStr(resultMapper) {
var paraStr = "(";
// 遍歷resultMapper生成參數(shù)數(shù)據(jù)
for(var i in resultMapper) {
if (i=="id") {
continue;
}
paraStr+=i+",";
}
paraStr=paraStr.substring(0,paraStr.length-1);
paraStr+=")";
return paraStr;
}
// 獲取insert語句的隨機(jī)value值(除去主鍵id)
function getInsertValStr(resultMapper) {
var valStr = "(";
// 遍歷resulet生成模板數(shù)據(jù)
for(var i in resultMapper) {
if (i=="id") {
continue;
}
// 在此處添加新的數(shù)據(jù)類型
// console.log(resultMapper[i]);
if (resultMapper[i].indexOf('int')>-1) {
// console.log(getRandomIntVal(5, 0));
if (i=="state" || i=="enabled" || i=="meter_type") {
valStr+=0;
} else {
valStr+=getRandomIntVal(5, 0);
}
} else
if(resultMapper[i].indexOf('varchar')>-1) {
// console.log(getRandomStringVal(12));
if (i.indexOf('name')>-1) {
valStr+="'"+getRandomChineseVal(5)+"'";
} else {
valStr+="'"+getRandomStringVal(5)+"'";
}
} else
if (resultMapper[i].indexOf('date')>-1) {
// console.log(getRandomDateVal("yyyy-MM-dd"));
valStr+="'"+getRandomDateVal("yyyy-MM-dd")+"'";
} else
if (resultMapper[i].indexOf('decimal')>-1) {
// console.log(getRandomIntVal(5, 2));
valStr+=getRandomIntVal(5, 2);
} else {
return "ERROR!!!!!!";
}
valStr+=",";
}
valStr=valStr.substring(0,valStr.length-1);
valStr+=")";
return valStr;
}
// 獲取特定位數(shù)的int值
function getRandomIntVal(bit, pot) {
var power = 1;
for(var i = 0; i < bit; i++) {
power *= 10;
}
power = Math.random()*power;
return power.toFixed(pot);
}
function getRandomStringVal(bit) {
bit = bit || 5;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < bit; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
function getRandomChineseVal(bit) {
bit = bit || 5;
var $chars = '上海轉(zhuǎn)運(yùn)中心蕪湖杭州武漢洛陽海鹽東莞青島千島湖順豐圓通韻達(dá)中通天天快遞';
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < bit; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
function getRandomDateVal(dateFormat) {
// 產(chǎn)生隨機(jī)整數(shù)包含上限值與下限值
// function random(lower, upper) {
// return Math.floor(Math.random() * (upper - lower+1)) + lower;
// }
var year = Math.ceil(Math.floor(Math.random() * (2019 - 2018+1)) + 2018);
var month = Math.ceil(Math.floor(Math.random() * (12 - 1+1)) + 1);
var day = Math.ceil(Math.floor(Math.random() * (28 - 1+1)) + 1);
if (dateFormat == "yyyy-MM-dd") {
return year+"-"+month+"-"+day;
}
if (dateFormat == "yyyy-MM") {
return year+"-"+month;
}
}
</script>
</head>
<body>
create語句:
<textarea name="getCreateStr" id="getCreateStr" rows="4" cols="100"></textarea>
<br>
生成數(shù)據(jù)條數(shù):
<input type="text" name="insertNum" id = "insertNum"></input>
<button type="submit" onclick="generateInsertClums()">生成按鈕</button>
<br>
<textarea name="insertResult" id="insertResult" rows="10" cols="100"></textarea>
<p>
科技與信息中心-產(chǎn)品一部-轉(zhuǎn)運(yùn)中心智能化部-季兆倫?
</p>
</body>
</html>