我的SQL語句查詢返回null。讓我瘋狂的是,我可以讓loadSchema工作並打印出每張表的所有表和列。但是我無法成功執行簡單的查詢。任何想法,爲什麼這可能會發生?Flex空應用程序SQLStatement返回null
public function db():void {
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN, openHandler);
dbFile = File.applicationStorageDirectory.resolvePath("DB.sql");
conn.open(dbFile);
}
private function openHandler(event:SQLEvent):void {
trace("The database was created successfully");
makeTables();
trace("Make Tables Called");
}
做桌子方法:
private function makeTables():void{
var createGraduateTable:SQLStatement = new SQLStatement();
createGraduateTable.sqlConnection = conn;
createGraduateTable.text = graduateLoader.data;
var createGraduateAgeTable:SQLStatement = new SQLStatement();
createGraduateAgeTable.sqlConnection = conn;
createGraduateAgeTable.text = graduateAgeLoader.data;
var createMarathonTable:SQLStatement = new SQLStatement();
createMarathonTable.sqlConnection = conn;
createMarathonTable.text = marathonLoader.data;
try {
createGraduateTable.execute();
createGraduateAgeTable.execute();
createMarathonTable.execute();
trace("Tables Created");
// Describe Database
conn.loadSchema(SQLTableSchema);
for each(var SQLTableStmt:SQLTableSchema in conn.getSchemaResult().tables){
trace("Table: " + SQLTableStmt.name);
for each (var SQLColm:SQLColumnSchema in SQLTableStmt.columns){
trace("Column name: " + SQLColm.name);
}
}
queryDB();
}catch(error:SQLError){
trace("Error message:", error.message);
trace("Details:", error.details);
}
}
我試着查詢使用監聽器的,並嘗試catch塊,但沒有任何工程。
queryDB方法:
public function queryDB(tableName:String = null):void{
var query:SQLStatement = new SQLStatement();
query.sqlConnection = conn;
query.text = "SELECT * FROM GRADUATE";
//query.addEventListener(SQLEvent.RESULT, queryResult);
//query.addEventListener(SQLErrorEvent.ERROR, queryError);
try{
query.execute();
var result:SQLResult = query.getResult();
if (result != null){
trace("Results: " + result.data);
}else{
trace("Result NULL");
}
}catch(e:SQLError){
}
}
private function queryResult(e:SQLEvent):void{
var result:SQLResult = e.target.getResult();
trace("Results: " + result.data);
conn.close();
}
private function queryError(e:SQLErrorEvent):void{
trace("Error message:", e.error.message);
trace("Query Error: " + e.error.details);
}
就像我說的,奇怪的是,當我執行這一點,我會打印出所有的表名和列正確,但result.data始終爲空。任何幫助是極大的讚賞。
以下是用於創建GRADUATE_AGE表的graduate_age.sql文件的前20行。
-- DROP TABLE IF EXISTS GRADUATE_AGE;
CREATE TABLE IF NOT EXISTS GRADUATE_AGE (
GPA REAL,
CREDITS_ATTEMPTED REAL,
CREDITS_PASSED REAL,
CURRENT_CREDITS REAL,
AGE REAL,
GENDER TEXT
);
INSERT INTO GRADUATE_AGE VALUES (2.7, 138.5, 138.5, 7, 25.74, 'M');
INSERT INTO GRADUATE_AGE VALUES (3.57, 146, 78, 9, 22.09, 'F');
INSERT INTO GRADUATE_AGE VALUES (3.63, 131, 131, 14, 23.21, 'M');
INSERT INTO GRADUATE_AGE VALUES (3.86, 152, 119, 14, 26.56, 'M');
INSERT INTO GRADUATE_AGE VALUES (3.03, 115.5, 115.5, 15, 26.3, 'M');
INSERT INTO GRADUATE_AGE VALUES (2.25, 39, 39, 16, 23.8, 'M');
INSERT INTO GRADUATE_AGE VALUES (0, 48, 0, 14, 22.45, 'M');
INSERT INTO GRADUATE_AGE VALUES (3.02, 63.5, 51.5, 14.5, 25.4, 'M');
INSERT INTO GRADUATE_AGE VALUES (3.14, 40, 40, 14, 32.93, 'M');
INSERT INTO GRADUATE_AGE VALUES (2.73, 62, 62, 14, 21.35, 'M');
INSERT INTO GRADUATE_AGE VALUES (3.39, 144, 81, 17, 24.32, 'M');
在迴應@ crooksy88我替換insert()方法調用queryDB()方法調用,並給它下面的聲明:
private function insert():void{
trace("Attempt Insert");
var insert:SQLStatement = new SQLStatement();
insert.sqlConnection = conn;
var insertStr:String = "";
insertStr += "INSERT INTO GRADUATE_AGE (GPA, CREDITS_ATTEMPTED, CREDITS_PASSED, CURRENT_CREDITS, AGE, GENDER) VALUES (2.7, 138.5, 138.5, 7, 25.74, 'M');";
insertStr += "INSERT INTO GRADUATE_AGE (GPA, CREDITS_ATTEMPTED, CREDITS_PASSED, CURRENT_CREDITS, AGE, GENDER) VALUES (3.57, 146, 78, 9, 22.09, 'F');";
insert.text = insertStr;
insert.addEventListener(SQLEvent.RESULT, quereyResult);
insert.addEventListener(SQLErrorEvent.ERROR, quereyError);
insert.execute();
}
private function insertResult(e:SQLEvent):void{
trace("Insert Complete: " + e.target.text);
queryDB();
}
我仍然得到空了我的結果。快把我逼瘋!它的確很愚蠢。
你能告訴我們實際的sql語句嗎?即graduateLoader.data,graduateAgeLoader.data和marathonLoader.data – crooksy88 2013-03-14 11:18:46
是的,我有一種感覺,那就是我的錯誤所在。但我認爲我所有的sql文件都是有效的sql和sqllite。我已更新帖子以顯示我的graduate_age.sql文件的前20行 – sdc 2013-03-14 20:34:38
我剛剛添加了以下方法: – sdc 2013-03-14 22:35:15