cc.sys.localStorage is a good solution for stogare data with key-value. But there are anyway to use SQLite or some Database manager in Cocos creator ?
Anyone has an idea ?
I tried to add some javascripts like https://github.com/kripken/sql.js/ but it’s not working
hi there
I use Javascript to access PHP and PHP to store data server side, not sure if it’s a good practice but it works.
cheers
You can store JSONs in local storage:
cc.sys.localStorage['player'] = JSON.stringify( {level : 1, score : 100} )
var playerState = JSON.parse(cc.sys.localStorage['player'])
Having a local database seems like an overkill for most purposes I can come up with Creator
It is a great idea, storage data in json string is a good way that I had not thought of before.
please get me a exemple
for web version i tested before, you can use any js db engine like nedb and etc …
but for native version thats not work , i think cc.localstorage now use sqlite3 wrap
if im wrong please correct
hello
sorry for the late reply, but here’s a very simple example (this will only work for web, not native)
helloPHP.zip (679.8 KB)
note that in this example upload.php is i the root folder (see build/web-desktop) you can however put it in subfolders as well.
upload.php code:
<?php
$data = file_get_contents('php://input'); //get json object from POST
$file ='data.json'; //filename of your json file
file_put_contents($file,$data); //save data to file
echo "upload successful"; //responsetext
?>
to access this from cocos you’ll need
saveData: function(){
var testData={name:"testDataName",arr:["test","data","array"]};
var e=cc.loader.getXMLHttpRequest();
var sendData=JSON.stringify(testData);
e.onreadystatechange=function(){
if(4==e.readyState&&200==e.status){
alert(e.responseText);
}
};
e.open("POST","upload.php",false);
e.setRequestHeader("Content-type","application/x-www-form-urlencoded");
e.send(sendData);
},
to save data. in order to retrieve it you can just access the json file on your server directly:
loadData: function(){
var e=cc.loader.getXMLHttpRequest();
var self=this;
e.onreadystatechange=function(){
if(4===e.readyState&&(200===e.status||0===e.status)){
self.dataObject=JSON.parse(e.responseText);
self._load();
}
};
e.open("get","data.json",false);
e.send(false);
},
hope this helps.
cheers