2012-09-17 49 views
0

我想在我正在開發的android應用程序中對SQLite數據庫進行插入。 我有一個帶有一些字段的HTML表單,我可以從這些字段獲取信息。 當我嘗試進行INSERT時,出現錯誤:'錯誤處理SQL:未定義'。 任何人都可以幫助我嗎?我是一個新手。 下面是我使用的代碼: HTML表單無法將數據插入到Android上的SQLite數據庫

<form name="editNoteForm" id="editNoteForm" method="post" action="index.html"> 
    <label for="primeiroNome">1&#186; Nome</label> 
    <input type="text" name="primeiroNome" id="primeiroNome"> 

    <label for="utlimoNome">&Uacute;ltimo Nome</label> 
    <input type="text" name="ultimoNome" id="ultimoNome"> 

    <label for="numeroTelefone">Telefone</label> 
    <input type="number" name="numeroTelefone" id="numeroTelefone"> 

    <input type="submit" onclick="onSubmit();" id="editFormSubmitButton" value="Save Note"> 

</form> 

JavaScript代碼

function onSubmit() { 
     db.transaction(addRowDB, errorCB, successCB); 
     alert('Done'); 
    } 

    function addRowDB(tx) { 
     var primeiroNome = document.getElementById('primeiroNome').value; 
     var ultimoNome = document.getElementById('ultimoNome').value; 
     var numeroTelefone = document.getElementById('numeroTelefone').value; 
     tx.executeSql('INSERT INTO DEMO (id, nome, ultimo, telefone) VALUES (null, primeiroNome, ultimoNome, numeroTelefone)'); 

    } 

感謝。

+0

是否使用一些JavaScript界面​​上的WebView? –

回答

0
tx.executeSql('INSERT INTO DEMO (id, nome, ultimo, telefone) VALUES (null, primeiroNome, ultimoNome, numeroTelefone)'); 

將插入litteral值 「primeiroNome」 ......

你需要插入變量primeiroNome ...(與級聯(+),例如)

+0

仍然不起作用。不需要介於''之間嗎? –

+0

發佈您更改的內容 – njzk2

0

我的價值觀發現問題。這真的是''。所以我尋找一個函數,它把它們放在變量前後,並且它工作。代碼這個人是:

function sqlstr(s) { 
    return "'"+s.replace(/'/g, "''")+"'"; 
} 

    function addRowDB(tx) { 
     var primeiroNome = document.getElementById('primeiroNome').value; 
     var ultimoNome = document.getElementById('ultimoNome').value; 
     var numeroTelefone = document.getElementById('numeroTelefone').value; 
     tx.executeSql('INSERT INTO DEMO (id, nome, ultimo, telefone) VALUES (null, ' + sqlstr(primeiroNome) + ', ' + sqlstr(ultimoNome) + ', ' + sqlstr(numeroTelefone) + ')'); 


    } 

感謝您的幫助:)

相關問題