如何使用appcelerator钛和PHP将数据从sqlite数据库插入远程mysql数据库

时间:2013-02-14 07:33:30

标签: php mysql sqlite appcelerator-mobile

我尝试使用以下代码。但它没有用。我有一个临时的sqllite表,我需要将所有数据从临时数据库插入到远程mysql服务器。

var url = "http://bmcagro.com/manoj/insertopinion.php";
    var xhr = Ti.Network.createHTTPClient({
           onload: function(e) {
           // this.responseText holds the raw text return of the message (used for JSON)
           // this.responseXML holds any returned XML (used for SOAP web services)
           // this.responseData holds any returned binary data
           Ti.API.debug(this.responseText);
           var json = this.responseText;  
           var response = JSON.parse(json); 

           if (response.logged == "true") {
             var newtoast = Titanium.UI.createNotification({
                          duration: 1000,
                          message: "Inserted"
           });
           newtoast.show();
           } else {  
                   var toast = Titanium.UI.createNotification({
                   duration: 2000,
                   message: "False"
           });
           toast.show();
    }
    },
    onerror: function(e) {
        Ti.API.debug(e.error);
         var toast = Titanium.UI.createNotification({
                   duration: 2000,
                   message: "Error in Connection!!"
       });
       toast.show();
    },
    timeout:5000 });

    xhr.open("POST", url);
    xhr.send({names: names});   
 });

,php代码是

<?php
    $con = mysql_connect("MysqlSample.db.8189976.hostedresource.com","MysqlSample","xszZ@123ddlj");  
    if (!$con) {
        echo "Failed to make connection.";
        exit;
    }
    $db = mysql_select_db("MysqlSample",$con);
    if (!$db) {
        echo "Failed to select db.";
        exit;
    }  
    $names = $_POST['names'];
    foreach ($names as $name) {
    mysql_query("INSERT INTO seekopinion(uid,gid,opiniondescription,date,postedto) VALUES (" + $name.gid + "," + $name.tempid + "," + $name.gid + ",NOW()," + $name.gid + ")");
    }
    if($query)   {  
         $sql = "SELECT * FROM MysqlSample.seekopinion";  
         $q= mysql_query($sql); 
         $row = mysql_fetch_array($q);       
         $response = array(  
            'logged' => true, 
            'seekopinion' => $row['seekopinion']        
         );
        echo json_encode($response);  
     } else {  
             $response = array(  
                'logged' => false,  
                'message' => 'User with same name exists!!'  
            );  
        echo json_encode($response);    
    }
?>  

实际上我是php和钛的初学者......任何人都会帮助我。

1 个答案:

答案 0 :(得分:2)

最后我找到了出路.... 我在appcelerator中使用分隔符“ - ”将整行更改为字符串,然后将参数传递给php代码...使用explode将代码拆分,然后使用for循环插入

用于将sqllite数据库中的表发布到mysql数据库的appcelerator代码..

postbutton.addEventListener('click', function(e) 
{
var names = [];
var datarow ="";
var db = Ti.Database.open('weather');
var rows = db.execute('SELECT tempid,gid,name,email FROM postedto');
while (rows.isValidRow())
{
datarow=datarow+"-"+rows.fieldByName('tempid')
rows.next();
}
db.close();
var params = {  
                 "uid": Ti.App.userid,       
                 "opiniondescription": question2.text, 
                 "database": datarow.toString()
                   };  

var url = "http://asdf.com/as/asd.php";
var xhr = Ti.Network.createHTTPClient({
           onload: function(e) {
           // this.responseText holds the raw text return of the message (used for JSON)
           // this.responseXML holds any returned XML (used for SOAP web services)
           // this.responseData holds any returned binary data
           Ti.API.debug(this.responseText);
           var json = this.responseText;  
           var response = JSON.parse(json); 
if (response.logged ==true) 
{
var seekopinion11=require('seekopinion2');
var seekop11 = new seekopinion11();
var newWindow = Ti.UI.createWindow({
                //fullscreen : true,
                backgroundImage : 'images/background.jpg',
                });
                newWindow.add(seekop11);
                newWindow.open({
                //animated : true
});
}
else  
{  
   var toast = Titanium.UI.createNotification({
               duration: 2000,
               message: response.message
   });
   toast.show();
}  
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT:   " + this.responseText);
Ti.API.debug("ERROR:  " + e.error);
var toast = Titanium.UI.createNotification({
            duration: 2000,
            message: "There was an error retrieving data.Please try again"
   });
   toast.show();
},
timeout:5000
});

 xhr.open("GET", url);
 xhr.send(params);
});

使用explode打破字符串的php代码

  <?php  
$con = mysql_connect("MysqlSample.db.hostedresource.com","MysqlSample","xszZ@");  
if (!$con)  
{  
    echo "Failed to make connection.";  
    exit;  
}  
$db = mysql_select_db("MysqlSample",$con);  
if (!$db)  
{  
    echo "Failed to select db.";  
    exit;  
}  
$uid= $_GET['uid'];  
$opiniondescription= $_GET['opiniondescription']; 
$database= $_GET['database']; 
$insert = "INSERT INTO seekopinion(uid,opiniondescription,date) VALUES ('$uid','$opiniondescription',NOW())";  
$query= mysql_query($insert);  
$rows = explode("-", $database);
$arrlength=count($rows);
for($x=0;$x<$arrlength;$x++)
{
$insert = "INSERT INTO seekopinionuser(sid,postedto) VALUES ((SELECT MAX(sid) FROM seekopinion),$rows[$x])";  
$query= mysql_query($insert);  
}
    if($query)  
    {  
     $sql = "SELECT s.sid,s.opiniondescription,s.uid,u.postedto FROM seekopinion  s left join seekopinionuser u on s.sid=u.sid WHERE uid=$uid AND s.sid=(SELECT MAX(sid) FROM seekopinion) ";  
     $q= mysql_query($sql); 
     $row = mysql_fetch_array($q);       
     $response = array(  
        'logged' => true, 
        'opiniondescription' => $row['opiniondescription'],
        'uid' => $row['uid'] ,
        'sid'=>$row['sid']

        );  
    echo json_encode($response);  

    }  
    else  
    {  
         $response = array(  
        'logged' => false,  
        'message' => 'Seek opinion insertion failed!!'  
    );  
    echo json_encode($response);    
    }  
?>