数据导入 MySQL -> 值与无值

我有以下问题:


我解析了一个 CSV 文件,然后数据应该会自动上传到 MySQL 数据库。现在的问题是我有数据类型“varchar”或“decimal”。在stackoverflow,我已经得到了帮助,如果数据类型是“十进制”,那么记录中只有数字。


记录 (rec) 如下所示:



[ { examplename1: 'example1',

    examplename2: 'example2',

    examplename3: 'example3',

    examplename4: 'example4',

    examplename5: 'example5',

    examplename6: 'example6',

    examplename7: 'example7',

    examplename8: 'example8',

    examplename9: 'example9',

    examplename10: 'example10',

    examplename11: 'example11',

    examplename12: 'example12',

    Diff_In_Hauswaehrung: -103600, 

    examplename14: 'example14',

    Marktwert_NPV: -111146.16,

    examplename16: 'example16' },

  { examplename1: 'example1',

    examplename2: 'example2',

    examplename3: 'example3',

    examplename4: 'example4',

    examplename5: 'example5',

    examplename6: 'example6',

    examplename7: 'example7',

    examplename8: 'example8',

    examplename9: 'example9',

    examplename10: 'example10',

    examplename11: 'example11',

    examplename12: 'example12',

    Diff_In_Hauswaehrung: 53851.33, 

    examplename14: 'example14',

    Marktwert_NPV: 47328.16,

    examplename16: 'example16' } ]



你可以看到在“Diff_In_Hauswaehrung”和“Marktwert_NPV”中有一个没有''的数字。


这些也在控制台中以黄色显示。


现在我想检查是否有这样的数字或只有一个带有 '' 的字符串,如果有一个数字,那么我不希望在 SQL 查询中的这个值周围没有“”。


现在我有一个 forEach 循环,其中所有值都用“”上传...但没有区别...keysY 只是 rec 的键。



 rec.forEach(entry => {


                    values = `"${entry[keysY[0]]}"`


                    for (var i = 1; i < keysY.length; i++) {

                        values += `,"${entry[keysY[i]]}"`

                    }


                    //console.log(values)


                    var sql = `INSERT INTO ${richtigername} (${namen}) VALUES (${values})`;


                    connection.query(sql, [values], (error, response) => {


                    console.log(error || response);


我希望你可以帮助我 :)


拉风的咖菲猫
浏览 131回答 2
2回答

肥皂起泡泡

要检查值是否包含有效数字,请将其强制转换为Number. 如果该值不是数字,则该转换的结果为false,否则为number。接下来,使用isNaN. 如果结果为假,则它是一个数字。为了简化这个,!预先添加一个来否定最终结果。下面是一个例子:!isNaN(Number("12.2"))// returns true (valid number)!isNaN(Number("asdf12"))// returns false (no valid number)!isNaN(Number(12))// returns true (valid number)所以在你的情况下,这应该可以解决问题:for (var i = 1; i < keysY.length; i++) {&nbsp; &nbsp; if(!isNaN(Number(${entry[keysY[i]]}) {&nbsp; &nbsp; &nbsp; &nbsp; // it's a number, no quotes:&nbsp; &nbsp; &nbsp; &nbsp; values += `,${entry[keysY[i]]}`;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // it's not a number (NaN) add quotes:&nbsp; &nbsp; &nbsp; &nbsp; values += `,"${entry[keysY[i]]}"`;&nbsp; &nbsp; }}

沧海一幻觉

但我有空列,我用 typeof 解决它,因为空列是“字符串”..使用 isNaN(Number() 也将空列显示为“false”但必须为“true”或仅与 Number () 空列不是 NaN..rec.forEach(entry => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof entry[keysY[0]] == "string") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values = `"${entry[keysY[0]]}"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values = entry[keysY[0]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 1; i < keysY.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof entry[keysY[i]] == "string") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values += `,"${entry[keysY[i]]}"`;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values += `,${entry[keysY[i]]}`;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }仅当我想要将它放在引号中或在前面放一个逗号时才需要 $ 和 `。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript