我正试图在我的移动Flex应用程序中将xml转换为sqlite
应填充应用程序中的列表
但是我收到了这些错误:
SQLError: 'Error #3115: SQL Error.', details:'no such table: 'words'', operation:'execute', detailID:'2013'
at flash.data::SQLStatement/internalExecute()
at flash.data::SQLStatement/execute()
at dao::WordsDAO/findByTerm()[C:\Users\sayydo1\Adobe Flash Builder 4.6\sayydo1\src\dao\WordsDAO.as:32]
这是我的WordsDAO.as代码:
package dao
{
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import dao.Words;
import mx.collections.ArrayCollection;
public class WordsDAO
{
private var _sqlConnection:SQLConnection;
public function get sqlConnection():SQLConnection
{
if (_sqlConnection)
return _sqlConnection;
openDatabase(File.documentsDirectory.resolvePath("EZDemo.db"));
return _sqlConnection;
}
public function findByTerm(searchKey:String):ArrayCollection
{
var sql:String = "SELECT * FROM words WHERE term LIKE '%"+searchKey+"%'";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[1] = searchKey;
stmt.execute();
var result:Array = stmt.getResult().data;
if (result)
{
var list:ArrayCollection = new ArrayCollection();
for (var i:int=0; i<result.length; i++)
{
list.addItem(processRow(result[i]));
}
return list;
}
else
{
return null;
}
}
public function create(words:Words):void
{
trace(words.term);
var sql:String =
"INSERT INTO words (id, term, defin, term1, defin1) " +
"VALUES (?,?,?,?,?)";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[0] = words.id;
stmt.parameters[1] = words.term;
stmt.parameters[2] = words.defin;
stmt.parameters[3] = words.term1;
stmt.parameters[4] = words.defin1;
stmt.execute();
words.loaded = true;
}
protected function processRow(o:Object):Words
{
var words:Words = new Words();
words.id = o.id;
words.term = o.term == null ? "" : o.term;
words.defin = o.defin == null ? "" : o.defin;
words.term1 = o.term1 == null ? "" : o.term1;
words.defin1 = o.defin1 == null ? "" : o.defin1;
words.loaded = true;
return words;
}
public function openDatabase(file:File):void
{
var newDB:Boolean = true;
if (file.exists)
newDB = false;
_sqlConnection = new SQLConnection();
_sqlConnection.open(file);
if (newDB)
{
createDatabase();
populateDatabase();
}
}
protected function createDatabase():void
{
var sql:String =
"CREATE TABLE IF NOT EXISTS words ( "+
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"term VARCHAR(50), " +
"defin VARCHAR(250), " +
"term1 VARCHAR(50), " +
"term1 VARCHAR(50), " +
"defin1 VARCHAR(250))";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.execute();
}
protected function populateDatabase():void
{
var file:File = File.applicationDirectory.resolvePath("assets/words.xml");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
for each (var emp:XML in xml.words)
{
var words:Words = new Words();
words.id = emp.id;
words.term = emp.term;
words.defin = emp.defin;
words.term1 = emp.term1;
words.defin1 = emp.defin1;
create(words);
}
}
}
}
Words.as:
package dao
{
import mx.collections.ArrayCollection;
import flash.utils.ByteArray;
[Bindable]
public class Words
{
public var loaded:Boolean = true;
public var id:int;
public var term:String;
public var defin:String;
public var term1:String;
public var defin1:String;
}
}
我希望这足以澄清我的问题。
编辑了更多信息...
答案 0 :(得分:1)
我只是在这里猜测,但是因为你的代码只检查DB文件的存在,可能是因为它之前是错误创建的(可能是因为下面提到的重复列问题),即没有表是当下?这将导致您遇到的错误。
您的代码还存在一些其他问题,一旦修复后,您的代码段就可以正常使用(使用虚拟数据)。
您的代码尝试两次创建term1
列。
"defin VARCHAR(250), " +
"term1 VARCHAR(50), " +
"term1 VARCHAR(50), " + // remove this row
"defin1 VARCHAR(250))";
这通常会引发错误并导致表未被创建。
您正在索引1
上定义一个未命名的参数。
stmt.parameters[1] = searchKey;
由于数字索引从0
开始,因此单独应该抛出错误,但是当使用正确的索引时它也会引发错误,因为查询中没有参数。
您的代码应该更像这样:
var sql:String = "SELECT * FROM words WHERE term LIKE ?";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters[0] = '%' + searchKey + '%';
// ...
有关预准备陈述的更多信息,请参阅http://help.adobe.com/.../flash/data/SQLStatement.html#parameters。