How to connect to MySQL with ionic?


#1

I am using a MySQl database (the data is in csv form)from Pythonanywhere and want to connect the database using an apps.
I am still learning ionic but want to know how to connect and view it using ionic?
Can it view from csv or must be in json?
I want to know if this is possible before I go deep into Ionic.
Thanks


#2

Nope. Ionic 2 is client side.


#3

It means there is no way i can query data which is in csv in mysql?
any suggestion?


#4

Or is it any other way like i send the data to different type of database so that i can request the data via ionic frame work easily?


#5

you can make a post to your server and get a json as response. a simple google can show you a lot of tutorial about it.


#6

Ionic is a client-side framework and is written in javascript or typescript. This two languages are not able to connect to a mysql datavase itself. You need for example a server-side API written in e.g PHP or Ruby, which gathers the data from the mysql database. Via the HTTP module of angular you can call your server now and receive the data!


#7

Thanks david,

Is it a cross site http request?
http://www.w3schools.com/angular/angular_sql.asp


#8

Besides, the http module form angular 2 only take json and can’t take for csv?


#9

Yes, the request to your server will be a cross-site one and you will need to put the correct stuff in the header of your request.

Access-Control-Allow-Origin: *

The problem of sending back csv-formatted text, is the parsing on the client side. I would just parse it on the server and send it back as a JSON Array/Object.


#10

Thanks David.
please let me know if the steps are correct

  1. convert MySQL data into json http://www.kodingmadesimple.com/2015/01/convert-mysql-to-json-using-php.html
  2. using Http request in http://www.w3schools.com/angular/angular_sql.asp

BTW, is the php file can be insert in ionic 2?Where should I put the file?
Besides, I want to query the data by searching the name of stocks (my database is stock name and price for 1000 stocks), i need to fetch the data in a whole(like the ecample in the link) or it can load one by one base on the query? Example, I search for AAPL, it will load for AAPL only? I a not sure a if a free MySQL database will allow high bandwidth if I fetch a large data.
Please help…thanks


#11

You should use JSON + http calls.


#12

Hi bkcollection,
I don’t have much experience in Ionic but i think i could help you->
step 1->get data from database in php
step 2-> make Rest API (you can use framework like apigility etc to save time .)
step 3 -> get your data in Ionic factory or where ever you want in Json form.


#13

Thanks Aabir for the introduction of apigility. The concept of using api instead of mysql database straight away is for the flexibility for any changes in the future without changes in frontend?
What inputs needed for apigility?
i still wonder how php,apigility integrate in ionic code.


#14

Let me be as clear as possible: They do not integrate.

You have your MySQL database on computer A (together with PHP or JS or Python or … that provide a Rest API to the MySQL db). And on a completely different computer - call it computer B - usually your mobile phone, you "consume"or query this data.

Computer A is called a server, because it serves data. It doesn’t care who consumes the data.
Computer B is called a client, it can (but doesn’t have to) consume data from some remote server. It displays stuff in a nice way to users and lets them interact with itself. <-- this is what Ionic does.

Ionic is in no way related to anything database. That is because you have to write different code for computer A & B each. If you pick Ionic, then computer B runs mainly on Javascript. But for computer A you have complete freedom of programming language. You can also pick Javascript, but Ionic can’t help you because Ionic is not for servers.

P.S.: Apigility is only one example out of many many that make your MySQL database into a Rest API.
Other examples:
(PHP) Symfony, Zend Framework, or simply just raw PHP
(JS) Express, Restify, Loopback, or simply just raw Node JS

P.P.S.: just for correctness’ sake: you don’t convert your MySQL database into a JSON file one single time and then just display the JSON file in Ionic. You program Ionic to send queries for specific elements of the database and then the content you asked for, gets converted temporarily to JSON. But you don’t save this JSON somewhere as a file - your server reads some entries form the database, converts them to JSON in your RAM and then it immediately send them to the client (your Ionic app). Only in very rare cases is it a good idea to convert the entire database to a JSON file.


#15

You are a little too early, native JSON support will be in the next version of mysql.

API is the way to go, I prefer RAILS over PHP because that what I been doing forever. Rails-api talks to mysql and i send my json queries into rails-api. You can use the Google Advance REST Client to practice your requests to the API backend before you struggle sending from IONIC


#16

hello everyone,
I want to connect my login form ( created by using Ionic 2 framework & angular 2) with my phpmyadmin database.
please help me fort hat