js中请求URL获得json数据,将数据解析并建表插入

//打开数据库
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
//html请求数据
function testXmlHttpRequest() {
var xmlRequest = ajaxFunction();
//获取IP 端口号
// var uname = document.getElementById('username').value;
//window.alert(xmlRequest);
//2.注册客户端的回调函数
xmlRequest.onreadystatechange = function() {
//3.判断服务器响应的状态值
if(xmlRequest.readyState == 4) {
//获取服务器端发送的信息
var messageFromServer = xmlRequest.responseText;
document.getElementById('displayUsername').innerHTML =
"" + messageFromServer + "";


// var obj = new Function("return" + messageFromServer)(); //转换后的JSON对象
var objxx = {
user_db: [{
id: '001',
loginname: 'yang',
cname: 'yangsi',
password: 'yang123'
}, {
id: '002',
loginname: 'zhang',
cname: 'zhangsi',
password: 'zhang123'
}],
member_db: [{
a: '001',
b: 'zhang',
c: '花火',
d: 'zhang123'
}, {
a: '001',
b: 'zhang',
c: '二月红',
d: 'zhang123'
}]
};


// json字符串的解析
// var show = obj.data.upload;
// document.getElementById('displayData').innerHTML =
// "" + show + "";
// for(var i = 0; i < 10; i++) { //模拟多条json数据
// //假数据
// var id = "001";
// var loginname = "yang";
// var cname = "杨博伟";
// var password = "yang123";
//
// //插入数据
//
// db.transaction(function(tx) {
// tx.executeSql('CREATE TABLE IF NOT EXISTS user_db (id text,loginname text,cname text,password text)');
// tx.executeSql('insert into user_db values(?,?,?,?)', [id, loginname, cname, password]);
// });
//
// }
parseJsonaddtable(objxx); //将数据解析并建表插入
//读一张用户表数据,form显示
var list = document.getElementById("list");
db.transaction(function(tx) {
//如果数据表不存在,则创建数据表 
tx.executeSql('create table if not exists user_db(id text,loginname text,cname text,password text)', []);
//查询所有联系人记录 
tx.executeSql('select * from user_db order by id desc', [], function(tx, rs) {
if(rs.rows.length > 0) {
var result = "";
result += "";
for(var i = 0; i < rs.rows.length; i++) {
var row = rs.rows.item(i);


//拼装一个表格的行节点 
result += "";
}
list.innerHTML = result;
} else {
list.innerHTML = "添加联系人";
}
});
});


}
}
//4.open方法指定请求服务器的url地址,拼接IP,端口号
var url = "http://server.jeasonlzy.com/OkHttpUtils/upload";


xmlRequest.open("GET", url, true);
//5.发送
xmlRequest.send(null);
}


function parseJsonaddtable(objxx) {
for(var key in objxx) { //一级键
//key 数据表名
var tablename = key;
var str1 = "";
var str2 = "";
var jsonarr = objxx[key]; //json对象[]
for(var index in jsonarr) {
var sqlstr1 = "";
var sqlstr2 = "";
var subjson = jsonarr[index]; //json数据的具体一个json对象


var num = 0;
var key_values = new Array();
for(var key in subjson) {
//遍历对象键
//key 对象的每一个键
// var subkey = key;


key_values[num] = subjson[key]; //对象的每一个键对应的值
num = num + 1;
//xuyaoqu掉逗号
sqlstr1 = sqlstr1 + key + " text,";
sqlstr2 = sqlstr2 + "?,";


}
//' (name text,phone text,company text,createtime INTEGER)'
//去逗号sqlstr1 sqlstr2
str1 = sqlstr1.substring(0, sqlstr1.length - 1);
sqlstr1 = "";
str2 = sqlstr2.substring(0, sqlstr2.length - 1);
sqlstr2 = "";
//插入表数据


// alert('create table if not exists ' + tablename + '(' + str1 + ')');
// alert('insert into ' + tablename + ' values(' + str2 + ')' + ',' + key_values.toString());
db.transaction(function(tx) { //如果数据表不存在,则创建数据表 

tx.executeSql('CREATE TABLE IF NOT EXISTS ' +tablename+ ' (' + str1 + ')');
tx.executeSql('insert into ' + tablename + ' values(' + str2 + ')', key_values);

});
str1 = "";
str2 = "";
}


}
}


//1.创建XmlHttpRequest对象(浏览器不同则创建的方法不同)
function ajaxFunction() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch(e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
return xmlHttp;
}
//登录验证
//获取输入值
//分别在数据库查询条件查询,返回结果results不为空


/*function gotonewye() {
window.location.href = "index01.html";
}*/


function isOkmethod() {


var Usename = document.getElementById("usenames").value;
var PassWord = document.getElementById("passwords").value;


db.transaction(function(tx) {


//如果数据表不存在,则创建数据表 
tx.executeSql('create table if not exists user_db(id text,loginname text,cname text,password text)', []);
//查询所有联系人记录 
tx.executeSql('select * from user_db where loginname=? and password=?', [Usename, PassWord], function(tx, rs) {
var len = rs.rows.length;
if(len > 0) {
alert("验证成功!");
window.location.href = "menu.html";
} else {
alert("验证失败!");
window.location.href = "loginError.html";
}
});
});


}

你可能感兴趣的:(js中请求URL获得json数据,将数据解析并建表插入)

序号ID登录名巡检人密码
" + (i + 1) + "" + row.id + "" + row.loginname + "" + row.cname + "" + row.password + "