5/27/2010 8:51:23 AM
Title:
SQlite error
I am getting this error when i experimented with SQlite databases.
Error: Operation is not permitted when the SQLStatement.sqlConnection property is not set.
Pls help
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
preinitialize="openDatabaseConnection()" fontSize="12" backgroundColor="#FFFFFF" width="600" height="700">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.data.SQLConnection;
import flash.events.SQLEvent;
import flash.filesystem.File;
private var conn:SQLConnection;
private var sqlConnection:SQLConnection;
private var initComplete:Boolean = false;
private var sqlStat:SQLStatement;
public function openDatabaseConnection():void
{
// create new sqlConnection
Alert.show("Db created");
sqlConnection = new SQLConnection();
sqlConnection.addEventListener(SQLEvent.OPEN, onDatabaseOpen);
sqlConnection.addEventListener(SQLErrorEvent.ERROR, errorHandler);
// get currently dir
var dbFile:File = File.applicationStorageDirectory.resolvePath("sampleDB.db");
// open database,If the file doesn't exist yet, it will be created
sqlConnection.openAsync(dbFile);
Alert.show("Db created");
}
// connect and init database/table
private function onDatabaseOpen(event:SQLEvent):void
{
// init sqlStatement object
sqlStat = new SQLStatement();
sqlStat.sqlConnection = conn;
var sql:String = "CREATE TABLE IF NOT EXISTS user (" +
" id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT, " +
" password TEXT" +
")";
sqlStat.text = sql;
sqlStat.addEventListener(SQLEvent.RESULT, statResult);
sqlStat.addEventListener(SQLErrorEvent.ERROR, createError);
sqlStat.execute();
}
private function statResult(event:SQLEvent):void
{
// refresh data
var sqlresult:SQLResult = sqlStat.getResult();
if(sqlresult.data == null){
getResult();
return;
}
datafiled.dataProvider = sqlresult.data;
}
// get data
private function getResult():void{
var sqlquery:String = "SELECT * FROM user"
excuseUpdate(sqlquery);
}
private function createError(event:SQLErrorEvent):void
{
Alert.show("Error code:" + event.error.code);
Alert.show("Details:", event.error.message);
}
private function errorHandler(event:SQLErrorEvent):void
{
// Alert.show("Error code:", event.error.code);
Alert.show("Details:", event.error.message);
}
// update
private function excuseUpdate(sql:String):void{
sqlStat.text = sql;
sqlStat.execute();
}
// insert
private function insertemp():void{
var sqlupdate:String = "Insert into user(id,name,password) values('" +
nam.text + "','" + password.text + "')";
debug.text += sqlupdate+"\n"
excuseUpdate(sqlupdate)
}
// delete
private function deleteemp():void{
var sqldelete:String = "delete from user where id='" +
datafiled.selectedItem.id +
"'";
excuseUpdate(sqldelete);
debug.text+=sqldelete+"\n"
}
]]>
</mx:Script>
<mx:TextArea x="21" y="10" width="402" height="179" id="debug"/>
<mx:DataGrid x="21" y="197" id="datafiled">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="id"/>
<mx:DataGridColumn headerText="name" dataField="name"/>
<mx:DataGridColumn headerText="password" dataField="password"/>
</mx:columns>
</mx:DataGrid>
<mx:Form x="21" y="471">
<mx:FormItem label="name">
<mx:TextInput id="nam"/>
</mx:FormItem>
<mx:FormItem label="password">
<mx:TextInput id="password"/>
</mx:FormItem>
</mx:Form>
<mx:Button x="300" y="503" label="add" click="insertemp()"/>
<mx:Button x="300" y="533" label="delete" click="deleteemp()"/>
</mx:WindowedApplication>
5/27/2010 10:31:19 AM
it might not be able to resolve the database file path properly , for testing change it to
File.desktopDirectory.resolvePath("myDb.db");
the file will be stored on the desktop
Kiney
Points: 600
Posts:0
5/27/2010 10:43:16 AM
check these links for sqlite connection in flash
http://shardulbartwal.wordpress.com/2008/04/14/adobe-air-and-sqlite-connectivity/
http://www.garyrgilbert.com/tutorials/air/beginner/asyncdbconnect.cfm