使用 JQuery 读取文本文件并存储在数据库中
Read a text file with JQuery and store in DataBase
我有一个文件包含
1 : "Benz"
2 : "Bmw"
3 : "Porche"
4 : "Ferrari"
我想用 Jquery 读取它并将它们存储在 OpenDataBase 的本地数据库中,其中 1 将是步数,而 Benz 将是数据库中的另一个字段。
我的读取文件的代码
jQuery.get('file/words.txt', function(data) {
alert(data)
});
和我创建数据库的代码
var db = openDatabase( ’mydb’, ’1.0’,
’database for test’,
2 * 1024 * 1024
);
db.transaction(function (tx) {
tx.executeSql(’CREATE TABLE IF NOT EXISTS Car
(number INT, name VARCHAR(100))’);
});
我不知道如何分离数据并将它们放入数据库 javascript。
谢谢。
如果可以,请删除文本文件中的数字和引号,因为不需要它们(您的计数器会为您执行此操作)。
将读取文本文件的代码更改为:
var file = "file/words.txt";
function getFile(){
$.get(file, function(txt){
var lines = txt.responseText.split("\n");
for (var i = 0, len = lines.length; i < len; i++) {
save(lines[i]);
}
});
}
现在文件的每一行都有一个数组;如果你去掉了数字和引号,它应该只是车名。
for (var i = 0; i < lines.length; i++) {
tx.executeSql('INSERT INTO Car (id, name) VALUES (i, lines[i]);
}
如果您想保持文件原样,请更改以下行,如下所示:
var lines = txt.responseText.split(":");
现在数组中包含号码和车名(奇数是号码,偶数是车名)。
我们可能想去掉双引号(SQL 可能会引发错误):
lines.replace(/"/g, '').trim();
for (var i = 0; i < lines.length; i++) {
tx.executeSql('INSERT INTO Car (id, name) VALUES (i, lines[i+1])');
i++; // we iterate again because we want an odd number
// (we skip over one iteration as that'd be the car, and we want the next # instead).
}
这是你想要的代码:
// assume this is your data (I've added the newline as well)
var textData = '1 : "Benz" \n2 : "Bmw" \n3 : "Porche"\n4 : "Ferrari" ';
// turn data into array
var ary = textData.split('\n').map(function(v) {
return v.split(':').map(function(v2) {
// make sure we remove the quotes and spaces
return v2.replace(/"/g, '').trim();
});
})
// function to escape double quotes to prevent sql injection
function escapeSql(str) {
return (typeof str === 'number' ? str : str.replace(/"/g, '"'));
}
// open database
var db = openDatabase('mydb', '1.0', 'database for test', 2 * 1024 * 1024);
// create table
db.transaction(function(tx) {
tx.executeSql('create table if not exists Car(step, make)');
});
// insert data
db.transaction(function(tx) {
// loop through each item and insert the data, notice how we call escapeSql to prevent sql injection
ary.forEach(function(item) {
var sql = 'insert into Car(step, make) values(' + escapeSql(item[0]) + ', "' + escapeSql(item[1]) + '")';
tx.executeSql(sql);
});
});
var sql,
cars = [];
// read data from table
db.transaction(function(tx) {
tx.executeSql('select * from Car', [], function(tx, results) {
var len = results.rows.length;
for (var i = 0; i < len; i++) {
cars.push({
step: results.rows.item(i).step,
make: results.rows.item(i).make
});
}
console.log(cars);
}, null);
});
输出:
我有一个文件包含
1 : "Benz"
2 : "Bmw"
3 : "Porche"
4 : "Ferrari"
我想用 Jquery 读取它并将它们存储在 OpenDataBase 的本地数据库中,其中 1 将是步数,而 Benz 将是数据库中的另一个字段。
我的读取文件的代码
jQuery.get('file/words.txt', function(data) {
alert(data)
});
和我创建数据库的代码
var db = openDatabase( ’mydb’, ’1.0’,
’database for test’,
2 * 1024 * 1024
);
db.transaction(function (tx) {
tx.executeSql(’CREATE TABLE IF NOT EXISTS Car
(number INT, name VARCHAR(100))’);
});
我不知道如何分离数据并将它们放入数据库 javascript。
谢谢。
如果可以,请删除文本文件中的数字和引号,因为不需要它们(您的计数器会为您执行此操作)。
将读取文本文件的代码更改为:
var file = "file/words.txt";
function getFile(){
$.get(file, function(txt){
var lines = txt.responseText.split("\n");
for (var i = 0, len = lines.length; i < len; i++) {
save(lines[i]);
}
});
}
现在文件的每一行都有一个数组;如果你去掉了数字和引号,它应该只是车名。
for (var i = 0; i < lines.length; i++) {
tx.executeSql('INSERT INTO Car (id, name) VALUES (i, lines[i]);
}
如果您想保持文件原样,请更改以下行,如下所示:
var lines = txt.responseText.split(":");
现在数组中包含号码和车名(奇数是号码,偶数是车名)。 我们可能想去掉双引号(SQL 可能会引发错误):
lines.replace(/"/g, '').trim();
for (var i = 0; i < lines.length; i++) {
tx.executeSql('INSERT INTO Car (id, name) VALUES (i, lines[i+1])');
i++; // we iterate again because we want an odd number
// (we skip over one iteration as that'd be the car, and we want the next # instead).
}
这是你想要的代码:
// assume this is your data (I've added the newline as well)
var textData = '1 : "Benz" \n2 : "Bmw" \n3 : "Porche"\n4 : "Ferrari" ';
// turn data into array
var ary = textData.split('\n').map(function(v) {
return v.split(':').map(function(v2) {
// make sure we remove the quotes and spaces
return v2.replace(/"/g, '').trim();
});
})
// function to escape double quotes to prevent sql injection
function escapeSql(str) {
return (typeof str === 'number' ? str : str.replace(/"/g, '"'));
}
// open database
var db = openDatabase('mydb', '1.0', 'database for test', 2 * 1024 * 1024);
// create table
db.transaction(function(tx) {
tx.executeSql('create table if not exists Car(step, make)');
});
// insert data
db.transaction(function(tx) {
// loop through each item and insert the data, notice how we call escapeSql to prevent sql injection
ary.forEach(function(item) {
var sql = 'insert into Car(step, make) values(' + escapeSql(item[0]) + ', "' + escapeSql(item[1]) + '")';
tx.executeSql(sql);
});
});
var sql,
cars = [];
// read data from table
db.transaction(function(tx) {
tx.executeSql('select * from Car', [], function(tx, results) {
var len = results.rows.length;
for (var i = 0; i < len; i++) {
cars.push({
step: results.rows.item(i).step,
make: results.rows.item(i).make
});
}
console.log(cars);
}, null);
});
输出: