0

I just setup an SQL Server Database and initially I created a table called "Contacts" and added 2 records to it in MS Server Management Studio manually. Then I have the code below to test it to see if I can add a new record to it thru my website using JavaScript. I have run the code & get no errors, but it also does not add a new record to the Database? Can someone look at this and see if they see anything to why this will not work?

I have googled it, but have not been able to find any solution.

  <!-- Open Database & add to it -->      
  <script type="text/javascript">
      var db = openDatabase('C:/Program Files/Microsoft SQL 
               Server/MSSQL14.SQLEXPRESS/MSSQL/DATA/SFF-Database.mdf', 
                '1.0', 'Test DB', 8 * 1024 * 1024);
      var msg;

       db.transaction(function(tx) {
      //  tx.executeSql('CREATE TABLE IF NOT EXISTS CONTACTS (FirstName, 
                         LastName, Address, City, State, PostalCode)');
      tx.executeSql('INSERT INTO CONTACTS VALUES (3, "Jeff", "Williams", 
                      "32568 Williams Street NW", "Portland", "OR", 
                       "93445")');
      msg = '<p>Log message created and row inserted.</p>';
              document.querySelector('#status').innerHTML = msg;
       })

      db.transaction(function(tx) {
         tx.executeSql('SELECT * FROM CONTACTS', [], function(tx, results) 
      {
      var len = results.rows.length,
        i;
      msg = "<p>Found rows: " + len + "</p>";
      document.querySelector('#status').innerHTML += msg;

      for (i = 0; i < len; i++) {
        msg = "<p><b>" + results.rows.item(i).FirstName + " " + 
            results.rows.item(i).LastName + " " + 
            results.rows.item(i).Address + " " + 
            results.rows.item(i).City + " " + results.rows.item(i).State +
            " " + results.rows.item(i).PostalCode + "</b></p>";
        document.querySelector('#status').innerHTML += msg;
        }
      }, null);
    });
  </script>

Why is it that there are no error's? yet it will not add a record into the Database.

Ry-
  • 218,210
  • 55
  • 464
  • 476
JRse
  • 93
  • 2
  • 11
  • 2
    What library are you using ? Where does `openDatabase` come from ? are you sure you can access the local file system freely ? shouldn't this be in `nodeJS` and not `html` ? – kigiri Jun 25 '19 at 16:03
  • Kigirl, I am new to SQL Server and working with this sort of Database. Many questions that i'm not sure how to answer, but appreciate the response. I am learning as I go.. I took this code right out of the following site: https://www.tutorialspoint.com/html5/html5_web_sql.htm. – JRse Jun 25 '19 at 16:19
  • 2
    Oh i see, so I'm very sorry to announce that you should not use this tutorial as it teaches you deprecated information, `web sql` was deprecated more than 10 years ago and should never be used. If you want to store data in the browser you can use `localStorage` if you need an external db I recommand somethink like firebase which is easy to begin with and free to host. – kigiri Jun 25 '19 at 16:23
  • 1
    Kigirl, Thank you for letting me know, I will certainly take a different path. – JRse Jun 25 '19 at 16:41
  • do you need to do a commit? – John Jun 25 '19 at 18:41

1 Answers1

0

Can someone look at this and see if they see anything to why this will not work?

This is not SQL Server compatible code:

CREATE TABLE IF NOT EXISTS

There are other StackOverflow topics covering this topic, if you search or google that command.

CREATE TABLE IF NOT EXISTS equivalent in SQL Server

Zorkolot
  • 1,899
  • 1
  • 11
  • 8