PDA

View Full Version : local database


p0p0
02-19-2009, 08:31 AM
hye all..

i have one question here.i have created one local database AIR application on my desktop.the hard part is to connect my database with my application.idon't know how to do it.can someone suggest solution for me??FYI i create the database using one tool call "Lita" a SQLite Database Administration. and my application is build using flex.

thanks so much

CreITve
02-19-2009, 06:32 PM
Don't know about direct connection through binary sockets or smth like that, but if you simply need to execute some database queries you can use xml. Your flex/air application send POST http request (through HTTPService) to the local http server, which is processed by php script, and this php script query the database. If it is INSERT query, it will perfom it, if it is SELECT query, it will return raw xml which can be processed by your flex/air app.
Here is my example (i'm newbie so sorry if code seems to be non-optimised):
mxml file(application):
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="597" height="431" title="Kefir's Tweeties" titleAlignment="center"
borderStyle="none" cornerRadius="3"
initialize="getMessages.send()"
backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#164F59, #01CBEF]">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var messageData:ArrayCollection;
private function resultHandler(event:ResultEvent):void{
messageData=event.result.messages.message;

}


]]>
</mx:Script>
<mx:HTTPService id="getMessages" url="http://localhost/twitter/index.php" result="resultHandler(event)"/>
<mx:HTTPService id="sendMessages" url="http://localhost/twitter/send.php" method="POST">
<mx:request xmlns="">
<author>
{authorField.text}
</author>
<text>
{messageField.text}
</text>
</mx:request>
</mx:HTTPService>
<mx:DataGrid id="messages" dataProvider="{messageData}" width="90%" height="198" horizontalCenter="3" y="10" color="#004CD6"/>
<mx:TextArea x="32.95" y="248" width="537.4" id="messageField" height="73"/>
<mx:Label x="32.95" y="216" text="Сообщение:" width="125" fontFamily="Verdana" fontSize="16" fontWeight="bold"/>
<mx:TextInput x="32.95" y="356" text="anonymous" id="authorField" focusIn="if(authorField.text=='anonymous'){ authorField.text=''}"/>
<mx:Button x="223" y="340" label="Отправить" click="if(messageField.text!='')
{sendMessages.send(); getMessages.send(); messageField.text=''}; "
fillAlphas="[1.0, 1.0, 1.0, 1.0]" fillColors="[#FFFFFF, #FFFFFF, #00E4FF, #067ADC]"/>
<mx:Label x="32.95" y="324" text="Ник:" fontFamily="Verdana" fontSize="16" fontWeight="bold"/>
</mx:WindowedApplication>


Here is index.php file:
<?php
mysql_connect("localhost", "user", "pass") or die ("А вот хрен. Нет такой базы");
mysql_select_db("twitter") or die ("Нет такой базы");

$query="SELECT * FROM main";
$result=mysql_query($query);

echo "<?xml version='1.0' ?>";
echo "<messages>";
while($row=mysql_fetch_array($result)){
echo "<message>";
echo "<text>";
echo $row['message'];
echo "</text>";
/*echo "<id>";
echo $row['id'];
echo "</id>"; */
echo "<author>";
echo $row['user'];
echo "</author>";
echo "</message>";
}
echo "</messages>";
mysql_close();
?>
Here is send.php file:
<?php
mysql_connect("localhost", "user", "pass") or die ("А вот хрен. Нет такой базы");
mysql_select_db("twitter") or die ("Нет такой базы");

$text = $_POST['text'];
$author = $_POST['author'];

mysql_query("INSERT INTO main (message,user) VALUES ('$text','$author')");
mysql_close();
?>