我的SQL语句查询返回null。令我发疯的是我可以让loadSchema工作并打印出每个表的所有表和列。但我无法成功执行简单的查询。任何想法为什么会这样?
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始终为null。非常感谢任何帮助。
以下是用于创建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, queryResult);
insert.addEventListener(SQLErrorEvent.ERROR, queryError);
insert.execute();
}
private function insertResult(e:SQLEvent):void{
trace("Insert Complete: " + e.target.text);
queryDB();
}
我的结果仍然是空的。快把我逼疯了!它必须是愚蠢的东西。
答案 0 :(得分:0)
您可能会更好地插入GRADUATE_AGE
而不是GRADUATE
;)
答案 1 :(得分:0)
可能的方法是首先使用一个非常简单的SQL语句来创建表并插入一条记录。如果可行,你可以在此基础上继续。
我首先尝试从SQL语句中删除所有下划线,并可能以小写形式命名表和列,以便更容易将它们与SQL关键字区分开。
在SQL语句中,有些事情似乎也不正确。
您正在创建一个名为GRADUATE_AGE的表,但尝试插入GRADUATE。
CREATE TABLE IF NOT EXISTS graduate (id INTEGER PRIMARY KEY AUTOINCREMENT, gap REAL, creditsAttempted REAL, creditsPassed REAL, currentCredits REAL, age INTEGER, gender TEXT)
INSERT INTO graduate (gap, creditsAttempted, creditsPassed, currentCredits, age, gender) VALUES (2.7, 138.5, 138.5, 7, 25.74, 'M')