0

I have been trying to achieve this, but it is not working. what im trying to do is that i have created a mysql database, and i want it that data to be displayed in my mobile app with the help of nodeJS. Database connectivity has been established and the data from the database is shown when i run the nodeJS file in npm terminal. My question is, can i make that data display in my cordova app.

Here is how my db.js page looks

var mysql = require('mysql');
  
var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "testdb"
});
    
con.connect(function(err) {
    if (err) throw err;
    con.query("SELECT * FROM animallist", function (err, result, fields) {
      if (err) throw err;
      console.log(result);
  });
});

when i run this db.js in npm terminal, the output is shown below. The output is correct.

enter image description here

but can i display this data into the html file from cordova app in the data div. and this is what my html page in cordova looks like

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
</head>
<body>
 <div id="data"></div>
</body>
</html>

I went through a lot many posts on the internet, but i was unable to find a solution, Please if anyone can help.

Thanks in advance, Ivan Paul

2
  • Is your objective to import this data into a native (iOS/Android/Windows) Cordova app? Commented Feb 2, 2018 at 15:07
  • Actually Yes, because i guess it would be very useful for me in making any apps in the future as node.JS is pretty easy and fast. Commented Feb 2, 2018 at 15:27

1 Answer 1

1

As there is no NodeJS runtime/MySQL DB available in a native mobile (iOS/Android/Windows UWP) Cordova environment, to display this data in a Cordova app you'll need to export the data from the MySQL DB on your desktop machine then import it into a SQLite DB on the target mobile OS.

You could use mysql2sqlite to convert the MySQL dump to a SQLite .db file, then import that file into a native SQLite database at runtime using the cordova-sqlite-ext plugin and following the instructions regarding pre-populated databases.

Sign up to request clarification or add additional context in comments.

1 Comment

You didn't understand Dave's answer, if you have a database that won't change, as you can't use MySQL directly in Cordova, you have to convert it once to SQLite and import it into your app, so you can query it directly. If the database is online and will change, then you have to use webservices to get the information from the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.